Skip to content
New issue

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

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

Already on GitHub? # 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 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
4 changes: 2 additions & 2 deletions src/jarvis/jarvis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ROOT_LOG_CONF = {
'version': 1,
'root': {
'level': 'INFO',
'level': 'DEBUG',
'handlers': ['file'],
},
'handlers': {
Expand Down Expand Up @@ -68,7 +68,7 @@

DEFAULT_GENERAL_SETTINGS = {
'assistant_name': 'Jarvis',
'input_mode': InputMode.TEXT.value,
'input_mode': InputMode.VOICE.value,
'response_in_speech': False,
}

Expand Down
37 changes: 24 additions & 13 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,20 +70,30 @@ 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))
subprocess.Popen(["python", "-m", "webbrowser", "-t", video], stdout=subprocess.PIPE, shell=False)
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:
raise Exception(f"No YT-video was found for '{search_text}'.")
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))
cls.console(error_log="Error with the execution of skill with message: {0}".format(e))
cls.response("I can't find what do you want in Youtube..")

@classmethod
Expand Down Expand Up @@ -175,4 +187,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")