Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/skills/browser #114

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ wikipedia==1.4.0
wolframalpha==3.0.1
word2number==1.1
xmltodict==0.12.0
glom==20.11.0
34 changes: 23 additions & 11 deletions src/jarvis/jarvis/skills/collection/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import urllib.request
import subprocess
import webbrowser
import json

from bs4 import BeautifulSoup as bs
from glom import glom, Coalesce

from jarvis.skills.skill import AssistantSkill

Expand Down Expand Up @@ -68,17 +70,28 @@ def open_in_youtube(cls, voice_transcript, skill):
tags = cls.extract_tags(voice_transcript, skill['tags'])
for tag in tags:
reg_ex = re.search(tag + ' (.*)', voice_transcript)

try:
if reg_ex:
search_text = reg_ex.group(1)
base = "https://www.youtube.com/results?search_query={0}&orderby=viewCount"
r = requests.get(base.format(search_text.replace(' ', '+')))
page = r.text
soup = bs(page, 'html.parser')
vids = soup.findAll('a', attrs={'class': 'yt-uix-tile-link'})
video = 'https://www.youtube.com' + vids[0]['href'] + "&autoplay=1"
cls.console(info_log="Play Youtube video: {0}".format(video))
search_text = reg_ex.group(1)
base = "https://www.youtube.com/results?search_query={0}&orderby=viewCount"
r = requests.get(base.format(search_text.replace(' ', '+')))
page = r.text
reg_ex = re.search("var ytInitialData = (.*);<\/script>", page)
yt_initial_data = json.loads(reg_ex.group(1))
yt_spec = ('contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents', [
Coalesce(('itemSectionRenderer.contents', [{
'url': Coalesce('radioRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url',
'videoRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url', default=None),
'title': Coalesce("radioRenderer.title.simpleText",
"videoRenderer.title.accessibility.accessibilityData.label", default="Unknown")
}]), default=None),
], lambda x: [elem for elem in x[0] if elem['url'] != None])
# glom with yt_specs returns a nested result-list, lamnda-func filters the list
yt_results = glom(yt_initial_data, yt_spec)
if not yt_results:
cls.response(f"I could not find a video searching for {search_text}")
else:
video = f"https://youtube.com{yt_results[0]['url']}"
cls.console(info_log=f"Now playing: {yt_results[0]['title']}")
subprocess.Popen(["python", "-m", "webbrowser", "-t", video], stdout=subprocess.PIPE, shell=False)
except Exception as e:
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
Expand Down Expand Up @@ -175,4 +188,3 @@ def _search_on_google(cls, term):
except Exception as e:
cls.console(error_log="Error with the execution of skill with message {0}".format(e))
cls.response("Sorry I faced an issue with google search")