Warning, file /plasma-bigscreen/soundcloud-voice-application/__init__.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 # -*- coding: utf-8 -*-
0002 
0003 from __future__ import unicode_literals
0004 import requests
0005 from os.path import dirname
0006 import sys
0007 import youtube_dl
0008 import json
0009 from adapt.intent import IntentBuilder
0010 from bs4 import BeautifulSoup
0011 from mycroft.skills.core import MycroftSkill
0012 from mycroft.messagebus.message import Message
0013 import threading
0014 import collections
0015 
0016 __author__ = 'aix'
0017 searchlst = {}
0018 soundlst = []
0019 searchlstobject = []
0020 current_song = ""
0021  
0022 class SoundcloudSkill(MycroftSkill):
0023     def __init__(self):
0024         super(SoundcloudSkill, self).__init__(name="SoundcloudSkill")
0025         self.process = None
0026         self.genMusicSearchList = None
0027 
0028     def initialize(self):
0029         self.load_data_files(dirname(__file__))
0030         
0031         self.add_event('soundcloud-audio-player.aiix.home', self.showHome)
0032 
0033         soundcloud = IntentBuilder("SoundcloudKeyword"). \
0034             require("SoundcloudKeyword").build()
0035         self.register_intent(soundcloud, self.soundcloud)
0036         
0037         soundcloudpause = IntentBuilder("SoundcloudPauseKeyword"). \
0038             require("SoundcloudPauseKeyword").build()
0039         self.register_intent(soundcloudpause, self.soundcloudpause)
0040         
0041         soundcloudresume = IntentBuilder("SoundcloudResumeKeyword"). \
0042             require("SoundcloudResumeKeyword").build()
0043         self.register_intent(soundcloudresume, self.soundcloudresume)
0044         
0045         soundcloudnext = IntentBuilder("SoundcloudNextKeyword"). \
0046             require("SoundcloudNextKeyword").build()
0047         self.register_intent(soundcloudnext, self.soundcloudnext)
0048         
0049         soundcloudprevious = IntentBuilder("SoundcloudPreviousKeyword"). \
0050             require("SoundcloudPreviousKeyword").build()
0051         self.register_intent(soundcloudprevious, self.soundcloudprevious)
0052         
0053         self.gui.register_handler('aiix.soundcloud-audio-player.next', self.soundcloudnext)
0054         self.gui.register_handler('aiix.soundcloud-audio-player.previous', self.soundcloudprevious)
0055         self.gui.register_handler('aiix.soundcloud-audio-player.playtitle', self.soundplayselection)
0056 
0057     def showHome(self, message):
0058         self.gui.clear()
0059         self.displayHome()
0060         
0061     def displayHome(self):
0062         self.gui.show_page("homepage.qml")
0063         
0064     def search(self, text):
0065         global soundlst
0066         global current_song
0067         query = text
0068         url = "https://soundcloud.com/search/sounds?q=" + query
0069         response = requests.get(url)
0070         soup = BeautifulSoup(response.content, 'html.parser')
0071         for link in soup.find_all('h2'):
0072             for x in link.find_all('a'):
0073                r = x.get('href')
0074                countOfWords = collections.Counter(r)
0075                result = [i for i in countOfWords if countOfWords[i]>1]
0076                if "/" in result:
0077                  soundlst.append("https://soundcloud.com" + x.get('href'))
0078         
0079         if len(soundlst) != 0:
0080             genMusicUrl = soundlst[0]
0081             self.genMusicSearchList = threading.Thread(target=self.getSearchListInfo, args=[soundlst])
0082             self.genMusicSearchList.start()
0083             return genMusicUrl
0084         else:
0085             self.speak("No Song Found")
0086             return False
0087         
0088     def soundcloud(self, message):
0089         self.stop()
0090         global soundlst
0091         global current_song
0092         soundlst.clear()
0093         utterance = message.data.get('utterance').lower()
0094         utterance = utterance.replace(
0095             message.data.get('SoundcloudKeyword'), '')
0096         aud = self.search(utterance)
0097         urlvideo = aud
0098         if urlvideo is not False:
0099             ydl_opts = {}
0100             with youtube_dl.YoutubeDL(ydl_opts) as ydl:
0101                 info_dict = ydl.extract_info(urlvideo, download=False)
0102                 audio_url = info_dict.get("url", None)
0103                 audio_title = info_dict.get('title', None)
0104                 audio_thumb = info_dict.get('thumbnail', None)
0105             print(audio_url, audio_title, audio_thumb)
0106             self.gui.clear()
0107             self.gui["audioTitle"] = str(audio_title)
0108             self.gui["audioSource"] = str(audio_url)
0109             self.gui["audioThumbnail"] = str(audio_thumb)
0110             self.gui["scSearchBlob"] = {} 
0111             self.gui["status"] = str("play")
0112             current_song = urlvideo
0113             print(current_song)
0114             self.gui.show_pages(["SoundCloudPlayer.qml", "SoundcloudSearch.qml"], 0, override_idle=True)
0115         
0116     def soundcloudpause(self, message):
0117         self.enclosure.bus.emit(Message("metadata", {"type": "soundcloud-audio-player", "status": str("pause")}))
0118 
0119     def soundcloudresume(self, message):
0120         self.enclosure.bus.emit(Message("metadata", {"type": "soundcloud-audio-player", "status": str("play")}))
0121         
0122     def soundcloudnext(self, message):
0123         global soundlst
0124         global current_song
0125         current_index = soundlst.index(current_song)
0126         if (current_index == len(soundlst)-1): 
0127             current_song = soundlst[0]
0128         else:
0129             current_song = soundlst[current_index+1]
0130             print(current_song)
0131                 
0132         urlvideo = current_song
0133         ydl_opts = {}
0134         with youtube_dl.YoutubeDL(ydl_opts) as ydl:
0135                 info_dict = ydl.extract_info(urlvideo, download=False)
0136                 audio_url = info_dict.get("url", None)
0137                 audio_title = info_dict.get('title', None)
0138                 audio_thumb = info_dict.get('thumbnail', None)
0139         print(audio_url, audio_title, audio_thumb)
0140         self.gui["audioTitle"] = str(audio_title)
0141         self.gui["audioSource"] = str(audio_url)
0142         self.gui["audioThumbnail"] = str(audio_thumb)
0143         self.gui["scSearchBlob"] = {} 
0144         self.gui["status"] = str("play")
0145         current_song = urlvideo
0146         print(current_song)
0147         self.gui.show_page("SoundCloudPlayer.qml")
0148         
0149         
0150     def soundcloudprevious(self, message):
0151         global soundlst
0152         global current_song
0153         current_index = soundlst.index(current_song)
0154         if(current_index == 0):
0155             current_song = soundlst[len(soundlst)-1]
0156         else:
0157             current_song = soundlst[current_index - 1]
0158             print(current_song)
0159                 
0160         urlvideo = current_song
0161         ydl_opts = {}
0162         with youtube_dl.YoutubeDL(ydl_opts) as ydl:
0163             info_dict = ydl.extract_info(urlvideo, download=False)
0164             audio_url = info_dict.get("url", None)
0165             audio_title = info_dict.get('title', None)
0166             audio_thumb = info_dict.get('thumbnail', None)
0167         print(audio_url, audio_title, audio_thumb)
0168         self.gui["audioTitle"] = str(audio_title)
0169         self.gui["audioSource"] = str(audio_url)
0170         self.gui["audioThumbnail"] = str(audio_thumb)
0171         self.gui["scSearchBlob"] = {} 
0172         self.gui["status"] = str("play")
0173         current_song = urlvideo
0174         print(current_song)
0175         self.gui.show_page("SoundCloudPlayer.qml")
0176 
0177     def getSearchListInfo(self, soundlst):
0178         global searchlst
0179         global searchlstobject
0180         ydl_opts = {}
0181         ydl = youtube_dl.YoutubeDL(ydl_opts)
0182         searchlstobject.clear();
0183         for x in range(len(soundlst)):
0184             info_dict = ydl.extract_info(soundlst[x], download=False)
0185             searchlstobject.append({"title": info_dict.get("title", None), "url": info_dict.get("url", None), "thumbnail": info_dict.get("thumbnail", None)})
0186             result = json.dumps(searchlstobject)
0187             self.gui["scSearchBlob"] = result
0188             
0189         if self.genMusicSearchList is not None:
0190             sys.exit()
0191         else:
0192             print("process is None")
0193             
0194     def soundplayselection(self, message):
0195         self.stop()
0196         global soundlst
0197         global current_song
0198         soundlst.clear()
0199         utterance = message.data["playtitle"].lower()
0200         aud = self.search(utterance)
0201         urlvideo = aud
0202         if urlvideo is not False:
0203             ydl_opts = {}
0204             with youtube_dl.YoutubeDL(ydl_opts) as ydl:
0205                 info_dict = ydl.extract_info(urlvideo, download=False)
0206                 audio_url = info_dict.get("url", None)
0207                 audio_title = info_dict.get('title', None)
0208                 audio_thumb = info_dict.get('thumbnail', None)
0209             print(audio_url, audio_title, audio_thumb)
0210             self.gui["audioTitle"] = str(audio_title)
0211             self.gui["audioSource"] = str(audio_url)
0212             self.gui["audioThumbnail"] = str(audio_thumb)
0213             self.gui["scSearchBlob"] = {} 
0214             self.gui["status"] = str("play")
0215             current_song = urlvideo
0216             print(current_song)
0217             self.gui.show_pages(["SoundCloudPlayer.qml", "SoundcloudSearch.qml"], 0, override_idle=True)
0218 
0219     def stop(self):
0220         self.enclosure.bus.emit(Message("metadata", {"type": "stop"}))
0221         if self.process:
0222             self.process.terminate()
0223             self.process.wait()
0224         pass
0225 
0226 def create_skill():
0227     return SoundcloudSkill()