Skip to content

Commit

Permalink
Add parse_entity to filter out Youtube-URL
Browse files Browse the repository at this point in the history
  • Loading branch information
keessonnema committed Sep 15, 2017
1 parent 305a3ad commit 029c190
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
19 changes: 11 additions & 8 deletions kezbot.py
Expand Up @@ -12,6 +12,7 @@
from config import Config
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram.ext.dispatcher import run_async
from telegram import MessageEntity
from strings import MatchPattern, YoutubePattern, strips, split, RemoveWords, \
KeepWords, StringRegex, run_strings

Expand All @@ -20,8 +21,11 @@

@run_async
def getify(_bot, update):
api = Config.YOUTUBE_API_KEY # Youtube API
yt_url = update.effective_message.text
api = Config.YOUTUBE_API_KEY
get_text = update.effective_message.parse_entities\
(types=[MessageEntity.URL, MessageEntity.TEXT_LINK])
yt_url = ''.join(list(
[y if t.type == MessageEntity.URL else t.url for t, y in get_text.items()][0]))
pattern = MatchPattern

if re.match(pattern, yt_url, re.I):
Expand All @@ -30,15 +34,16 @@ def getify(_bot, update):
yt_id = ' '.join(re.findall(pattern, yt_link, re.MULTILINE | re.IGNORECASE))

if not yt_id:
update.effective_message.reply_text("This is not a Youtube-URL! \nTry again.")
update.effective_message.reply_text("This is not a valid Youtube-URL! \nTry again.")
else:
url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={0}&key={1}"\
.format(yt_id, api)
title = ujson.loads(requests.get(url).text)
title = title['items'][0]['snippet']['title'] # get title from Youtube

if not any(e in title for e in strips):
update.effective_message.reply_text('This is not a valid song, try a different url')
update.effective_message.reply_text('This is not a valid song :('
'\nTry a different link or search for another song.')
else:
# update.effective_message.reply_text("You've searched for: \n♫ {0}. \n\n"
# "Let me find it on Spotify!".format(title))
Expand All @@ -58,8 +63,6 @@ def getify(_bot, update):
new_list[0] = first.split(sep, 1)[0]
new_list[0] = first.split(sep2, 1)[0]

print(new_list)

spotify_token = util.prompt_for_user_token(Config.username, Config.scope)

if spotify_token:
Expand Down Expand Up @@ -89,7 +92,7 @@ def getify(_bot, update):
else:
update.effective_message.reply_text\
("I can't find this track on Spotify :( "
"Try a different link or search for another song.")
"\nTry a different link or search for another song.")
else:
update.effective_message.reply_text("This is not a song. Try some music :)")
else:
Expand Down Expand Up @@ -127,7 +130,7 @@ def get_ip(_bot, update):

def main():
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
level=logging.WARNING)
token = Config.API_KEY
updater = Updater(token)
handler = updater.dispatcher.add_handler
Expand Down
6 changes: 3 additions & 3 deletions strings.py
Expand Up @@ -2,10 +2,10 @@
YoutubePattern = r'(?:https?:\/\/)?(?:[0-9A-Z-]+\.)?(?:youtube|youtu|youtube-nocookie)\.' \
r'(?:com|be)\/(?:watch\?v=|watch\?.+&v=|embed\/|v\/|.+\?v=)?([^&=\n%\?]{11})'

strips = [' - ', '- ', ' -', ': ', ' : ', ' :', ' – ']
split = ' - |- | -|: | : | :| – '
strips = [' - ', '- ', ' -', ' – ']
split = ' - |- | -| – '

RemoveWords = ' ft| feat|lyrics|lyric'
RemoveWords = ' ft| feat|lyrics|lyric|vs'
KeepWords = r'\b(remix|edit|rmx)\b'
StringRegex = r'\[[^\]]*\]|\(\d+\)|“.*?”|".*?"|[.]|[&]|[,]'

Expand Down

0 comments on commit 029c190

Please sign in to comment.