def add_song(name, list): startTime = time.time() ytb = Youtube() print("ytb " + str(time.time() - startTime)) url = ytb.get_one(name) print("url " + str(time.time() - startTime)) pafy = ytb.get_audio(url) print("pafy " + str(time.time() - startTime)) list.add(pafy) print("list " + str(time.time() - startTime))
def daily_thread(): """ Print daily data every minute """ youtube = Youtube() portfolio = Portfolio() while True: try: youtube.subscribers() portfolio.value() except ConnectionError: time.sleep(300) time.sleep(60)
def main(): sp = Spotify() yt = Youtube() yt_playlist_id = input("Enter youtube playlist id: ") spotify_playlist_name = input("Enter a name for your spotify playlist: ") songs = yt.get_songs_from_playlist(yt_playlist_id) spotify_playlist_id = sp.create_playlist(spotify_playlist_name) for song in songs: song_uri = sp.get_song_uri(song.artist, song.title) was_added = sp.add_song_to_playlist(song_uri, spotify_playlist_id) if was_added: print(f'{song.artist} - {song.title} was added to playlist.')
def gen_process() -> list: event_list = [] if config['youtube']['enable']: for user_config in config['youtube']['users']: y = Youtube(user_config) event_list.append(y) if config['twitcasting']['enable']: for user_config in config['twitcasting']['users']: t = Twitcasting(user_config) event_list.append(t) if config['openrec']['enable']: for user_config in config['openrec']['users']: o = Openrec(user_config) event_list.append(o) if config['mirrativ']['enable']: for user_config in config['mirrativ']['users']: m = Mirrativ(user_config) event_list.append(m) if config['bilibili']['enable']: for user_config in config['bilibili']['users']: b = Bilibili(user_config) event_list.append(b) if user_config['article']: b1 = BilibiliArticle(user_config) event_list.append(b1) if user_config['live']: b2 = BilibiliLive(user_config) event_list.append(b2) return event_list
def initialize(): """Method for initializing a youtube object, connection to the DB and load comments get the comments for the youtube videos ids listed in the DB, store the comments in the DB, train the classifier and print the classifier metrics Args: config: dicitionary containing the DB parameters """ with app.app_context(): g.config = get_config() g.yt = Youtube(api_key= g.config ['youtube']['API_KEY'], api_version = g.config ['youtube']['API_VERSION'], service_name=g.config['youtube']['API_SERVICE_NAME']) video_ids = g.config['youtube_videos_id'] g.comments = [] for video_id in video_ids: g.comments.extend(g.yt.get_video_comments(video_id)) #db_params = {"user":g.config['db']['user'], "host":g.config['db']["host"], "port":g.config['db']["port"],"database": g.config['db']["database"]} g.db = DBController(**g.config['db']) # create table if it does not exist: g.db.create_comments_table() # train classifier g.classifier = Classifier() g.classifier.train() # predication of the comments for comment_item in g.comments: text = comment_item.text predication = g.classifier.predict(comment_text) comment_item.sentiment = predication # insert the comment in DB g.db.insert_comments(comment_item)
def __init__(self): self.index = 0 self.youtube_services = [] for api_key in YOUTUBE_API_KEYS: self.youtube_services.append( Youtube(api_key, YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION))
def youtube(*args): """ Use the Youtube API to return a youtube video """ # Convert from tuple to list so we can modify try: idx, args = _get_idx_from_args(list(args)) idx = 0 if idx == -1 else idx except ValueError as error: return "text", str(error) try: youtube = Youtube(*args) except APIError as error: return "text", str(error) return "text", youtube.get_video(idx)
def __init__(self, spotify={}, youtube={}): """ make a resource to talk to spotify api and youtube api see spotify.py for params see youtube.py for params """ self.sf = Spotfy(**spotify) self.yt = Youtube(**youtube_parser)
def listening_now(max_tunes, by_id=True): """ Get a list of tunes other people using last.fm are listening to, and turn them in to a youtube list of Tracks objects. TODO: this might need a little more work :param max_tunes: The amount of Tracks. :type max_tunes: int :param by_id: Only add tunes that have a youtube ID(recommended) :type by_id: bool :return: A list of Track objects :rtype: list | None """ url = LISTENING_NOW_URL.format(max_tunes) lastfm = web.get(url=url, as_json=True) log.debug('lastfm response %s' % lastfm.json) if len(lastfm.json['Users']) != 0: yt_tracks = [] for user in lastfm.json['Users']: if 'playlink' in user: if 'data-youtube-id' in user['playlink']: youtube_id = user['playlink']['data-youtube-id'] yt = Youtube.id_details(youtube_id) log.debug(yt) if isinstance(yt, Track): yt_tracks.append(yt) else: if 'Track' in user: search_str = '%s-%s' % (user['Track']['Artist'], user['Track']['Name']) if not by_id: yt = Youtube.search(search_str) log.debug('search by search string: %s result: %s' % (search_str, yt)) if isinstance(yt, Track): yt_tracks.append(yt) return yt_tracks return None
def get_from_yt(user_input): youtube = Youtube(yt_resource) youtube.search_channel(user_input) youtube.print_channels() return youtube.select_channel( int( input_in_range('Select channel: ', 1, youtube.total_channels + 1)))
def upload(self): self.youtube = Youtube() title = util.template(self.config.youtube_title, self.dictionary) description = util.template(self.config.youtube_description, self.dictionary) category_id = self.config.youtube_category_id tags = [] upload_result = self.youtube.initialize_upload(title, description, self.video_path, category_id, tags) self.youtube_videoid = upload_result["id"] return upload_result["id"]
def main(): playlist_url = input("Youtube playlist url: ") playlist_name = input("Playlist name: ") yt = Youtube(playlist_url) song_titles = yt.get_songs_title() print(len(song_titles)) songs_info = yt.get_songs_info(song_titles) print(len(songs_info)) spotify = Spotify() playlst_id = spotify.create_playlist(playlist_name) for song_name, artist in songs_info.items(): uri = spotify.get_spotify_uri(artist, song_name) status = spotify.add_songs_to_playlist(playlst_id, {"uris": [uri]}) if status: print(f"{artist}-{song_name} was added to playlist.") else: print(f"\nERROR!! {artist}-{song_name} could not be added.\n")
async def populate_random(self): youtube = Youtube() async def get_one(): async for item in youtube.random(None, 50): self.db[item['id']['videoId']] = 'random' if len(self.db) % 100 == 0: print(len(self.db)) tasks = [get_one() for i in range(2000)] await asyncio.gather(*tasks) await youtube.close()
def setpassword(message, client): global youtube_instance global password global user_id print("In set password") with open(os.path.join(sys.path[0], "assets/ussername_pass.json"), 'r+') as passfile: data = json.load(passfile) email = data["email"] passs = data["password"] credentials = message[9:] username, password_ = credentials.split(",") print(username) print(password_) if passs == "": print("Here") hashpass = hashlib.sha256(str.encode(password_)).hexdigest() print(hashpass) data = {"email": username, "password": hashpass} passfile.seek(0) json.dump(data, passfile) passfile.truncate() youtube_instance = Youtube(username, password_) password = password_ client.publish("GladOs/messages/raspberry2phone" + user_id, "Everything OK") TTS("I am ready to take your commands master!") else: if (hashlib.sha256( str.encode(password_)).hexdigest() == passs): print("Here") youtube_instance = Youtube(username, password_) client.publish("GladOs/messages/raspberry2phone" + user_id, "Everything OK") TTS("I am ready to take your commands master!") else: client.publish("GladOs/messages/raspberry2phone" + user_id, "Wrong password enter again!")
def paginate(self) -> bool: if self.current_playlist_urls and len( self.current_playlist_urls) == self.total_items: return False self.current_page += 1 response: Dict = self.spotify.get_liked_songs(limit=PAGINATION_LIMIT, page=self.current_page) songs: List[Dict] = response['items'] self.total_items = response['total'] queries = list(map(Spotify.get_searchable_name, songs)) print(queries) new_urls = list( map(lambda s: Youtube.get_audio_url(Youtube.search(s)[0]), queries)) print("URLS: ", new_urls) self.current_playlist_urls.extend(new_urls) self.current_media_list.lock() for url in new_urls: self.current_media_list.add_media(url) self.current_media_list.unlock() return True
def main(): sp = Spotify() yt = Youtube() yt_playlist_id = input("Enter youtube playlist id: ") spotify_playlist_name = input("Enter a name for your spotify playlist: ") spotify_playlist_id = sp.create_playlist(spotify_playlist_name) songs = yt.get_songs_from_playlist(yt_playlist_id) for song in songs: song_uri = sp.get_song_uri(song.artist, song.title) if not song_uri: print(f"{song.artist} - {song.title} was not found!") continue was_added = sp.add_song_to_playlist(song_uri, spotify_playlist_id) if was_added: print(f'{song.artist} - {song.title} was added to playlist.') total_songs_added = sp._num_playlist_songs(spotify_playlist_id) print(f'Added {total_songs_added} songs out of {len(songs)}')
def get_songs(self): spotify = spotipy.Spotify(auth=self.token) results = spotify.search(q=self.genre, type='playlist',limit=10) for playlist in results['playlists']['items']: if playlist['tracks']['total'] < self.num_of_vid: print('tracks less than '+ str(self.num_of_vid) + 'skipping to next playlist') continue else: playlist_tracks = spotify.user_playlist_tracks(playlist['owner']['id'], playlist['id']) print('playlist has ' +str(playlist_tracks['total'])+ ' songs.') f_cnt = -1 s_cnt = 0 for playlist_track in playlist_tracks['items']: f_cnt += 1 if f_cnt == 0: continue try: #song data song_title = playlist_track['track']['artists'][0]['name'] + ' - ' + playlist_track['track']['name'] song_id = playlist_track['track']['id'] # song features audio_features = spotify.audio_features(song_id) # video data video_url = Youtube(song_title).search_video() format = pytube.YouTube(video_url).streams.get_by_itag('133') if s_cnt == self.num_of_vid: break # donwload video / save data in db s_cnt += 1 print('song: '+str(s_cnt)+ ': '+song_title+ ' | ' +video_url+ ' | ' +str(format)) format.download(self.dir) except: print('error. continuing to next song.') continue break
async def scheduled(wait_for): while True: await asyncio.sleep(wait_for) with open('data.json', 'r') as data_file: json_data = data_file.read() video_links = json.loads(json_data) video_links1 = Youtube.get_all_video_in_channel(channel_id) subscriptions = db.get_subscriptions() if video_links1[0] != video_links[0]: for s in subscriptions: vd = video.vid_info(video_links1[0]) await bot.send_message(s[1], vd['title'] + "\n" + video_links1[0], disable_notification=True) video_links = video_links1
def test_get_video(mock_request, mock_response): """ Test getting a YT video """ mock_request.return_value = mock_response youtube = Youtube("northernlion") assert youtube.url == (f"https://www.googleapis.com/youtube/v3/search" f"?key={YOUTUBE_AUTH}&q=northernlion&maxResults" f"={MAX_REQUESTED_VIDS}&type=video") assert youtube.get_video(0) == f"{BASE_URL}isaac" # Test getting a youtube video but no results mock_response.json.return_value = {"items": []} youtube = Youtube("northernlion") with pytest.raises(APIError) as error: youtube.validate_num_vids() assert "Sorry, there were no videos" in str(error)
def gen_process(self): if config['youtube']['enable']: for user_config in config['youtube']['users']: y = Youtube(user_config) self.events_multi.append(y) if config['twitcasting']['enable']: for user_config in config['twitcasting']['users']: t = Twitcasting(user_config) self.events_multi.append(t) if config['openrec']['enable']: for user_config in config['openrec']['users']: o = Openrec(user_config) self.events_multi.append(o) if config['mirrativ']['enable']: for user_config in config['mirrativ']['users']: m = Mirrativ(user_config) self.events_multi.append(m) if config['bilibili']['enable']: for user_config in config['bilibili']['users']: b = Bilibili(user_config) self.events_multi.append(b)
async def main(): if "youtube.com/channel" in sys.argv[1] or "youtube.com/watch" in sys.argv[ 1]: s = Youtube(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "") data = await s.get_play_info() title = data["title"] url = data["play_url"] elif "twitch.tv/" in sys.argv[1]: s = Twitch(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "") data = await s.get_play_info() title = data["title"] url = data["play_url"] elif "douyu.com/" in sys.argv[1]: s = Douyu(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "") data = await s.get_play_info() title = data["title"] url = data["play_url"] elif "huya.com/" in sys.argv[1]: s = Huya(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "") data = await s.get_play_info() title = data["title"] url = data["play_url"] elif "live.bilibili.com/" in sys.argv[1]: s = Bilibili(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "") data = await s.get_play_info() title = data["title"] url = data["play_url"] elif "bilibili.com/" in sys.argv[1]: s = Bilibili(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "") await s.get_play_info_video() return else: await run_ykdl(sys.argv[1]) return print("title: " + title, flush=True) print(url, flush=True)
def chart(chart_items=5): """ Finds the currently most played tunes on last.fm and returns them as a list of Track objects. :param chart_items: The amount of tracks wanted. :type chart_items: int :return: A list of Track objects or None on error. :rtype: list | None """ url = CHART_URL.format(chart_items) lastfm = web.get(url=url, as_json=True) log.debug('lastfm response %s' % lastfm.json) if len(lastfm.errors) > 0: log.error(lastfm.errors) return None else: if 'results' in lastfm.json: if 'track' in lastfm.json['results']: if len(lastfm.json['results']['track']) != 0: yt_tracks = [] for track in lastfm.json['results']['track']: search_str = '%s-%s' % (track['artist'], track['name']) yt = Youtube.search(search_str) log.debug(yt) if isinstance(yt, Track): yt_tracks.append(yt) return yt_tracks return None
#function to list the region that I am interesting in. def generate_supported_region_ids(): regions_ids = {'Argentina': 'AR', 'Australia': 'AU', 'Brazil': 'BR', 'Canada': 'CA', 'Egypt': 'EG', 'France': 'FR', 'Germany': 'DE', 'Great Britain': 'GB', 'Hong Kong': 'HK', 'India': 'IN', 'Ireland': 'IE', 'Italy': 'IT', 'Japan': 'JP', 'Malaysia': 'MY', 'Mexico': 'MX', 'Netherlands': 'NL', 'New Zealand': 'NZ', 'Russia': 'RU', 'Saudi Arabia': 'SA', 'Singapore': 'SG', 'South Africa': 'ZA', 'South Korea': 'KR', 'Spain': 'ES', 'Sweden': 'SE', 'Switzerland': 'CH', 'Taiwan': 'TW', 'United States': 'US'} return regions_ids #main if __name__ == '__main__': #instantiate the youtubapi class. This would default the region to US and api string. youtube = Youtube() #get a dictionary of regions ids. regions_ids = generate_supported_region_ids() #a list to store the result. result = [] #go through all regions ids. for name, region_id in regions_ids.iteritems(): temp_result = dict() #first set the region to region id. youtube.set_region_id(region_id) #then do an api to youtube to get the top 10. status, body = youtube.get_most_popular_by_region() #if response from youtube is not okay then store the error. if status != 200: temp_result[name] = 'Unable to get data for %s' % name
class MusicDownloader(object): def __init__(self): self.__youtube = Youtube() self.__spotify = Spotify() self.__editor = TagEditor() self.__last = LastFM() self.__apple = AppleMusic() self.__deezer = Deezer() def __downloadMusicFromYoutube(self, name, uri, dur): #finding song on youtube self.__youtube.get(name, dur) #downloading video from youtube if self.__youtube.download(url=self.__youtube.getResult(), path=uri, filename=uri): #converting video to mp3 file self.__youtube.convertVideoToMusic(uri=uri) return True else: return False def __getSongInfoFromSpotify(self, uri): try: return self.__spotify.getSongInfo(uri) except: return None def getNameFromYoutube(self, url): return self.__youtube.getNameFromYoutube(url) def getData(self, uri): try: return self.__spotify.getSongInfo(uri) except: return None def getLastFMTags(self, name): return self.__last.get(name) def getYoutubeMusicInfo(self, url): return self.__youtube.getNameFromYoutube(url) def downloadBySpotifyUri(self, uri, path): #get info info = self.__getSongInfoFromSpotify(uri) if info: fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and download from YouTube and tagging if self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']): self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") print(path) if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') return True return False def downloadBySearchQuery(self, query, path=None): #get info info = self.__spotify.search(query=query) if not info: info = self.__last.get(query) if info: fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and download from YouTube and tagging self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']) self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') return True, info else: return False, None def downloadBySpotifyUriFromFile(self, filename): try: with open(filename, 'r') as f: data = f.readlines() except FileNotFoundError: print(f'No such file or directory: "{filename}"') exit(2) #normalize try: data.remove('\n') except: pass links = [str(item).replace('\n', '') for item in data] for i, song in zip(range(len(links)), links): print(f'[{i+1}] - {song}') try: state = self.downloadBySpotifyUri(str(song).split(':')[-1]) if not state: notify.send(f'Failed to download', True) except: print('Something went wrong!') def downloadBySpotifyUriPlaylistMode(self, playlist_uri, path): user = Spotify.User() playlist = user.getPlaylistTracks(playlist_uri) for info, i in zip(playlist, range(len(playlist))): print(f'Downloading {i+1} of {len(playlist)}') fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and download from YouTube and tagging self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']) self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') def downloadBySpotifyUriAlbumMode(self, album_uri, path): user = Spotify() playlist = user.getAlbum(album_uri) for info, i in zip(playlist['tracks'], range(len(playlist['tracks']))): print(f'Downloading {i+1} of {len(playlist["tracks"])}') fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and downloading from YouTube and tagging self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']) self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') def downloadByDeezerUrl(self, url, path): link = str(str(url).split('/')[-1]).split('?')[0] #get info info = self.__deezer.getSongInfo(link) if info: fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and download from YouTube and tagging if self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']): self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") print(path) if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') return True return False def downloadByDeezerUrlAlbumMode(self, album_url, path): link = str(str(album_url).split('/')[-1]).split('?')[0] #get info playlist = self.__deezer.getAlbum(link) for info, i in zip(playlist['tracks'], range(len(playlist['tracks']))): print(f'Downloading {i+1} of {len(playlist["tracks"])}') fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and downloading from YouTube and tagging self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']) self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') def downloadByDeezerUrlPlaylistMode(self, playlist_url, path): link = str(str(playlist_url).split('/')[-1]).split('?')[0] #get info playlist = self.__deezer.getPlaylist(link) for info, i in zip(playlist['tracks'], range(len(playlist['tracks']))): print(f'Downloading {i+1} of {len(playlist["tracks"])}') fixed_name = f'{info["artist"][0]} - {info["name"]}' fixed_name = fixed_name.replace('.', '') fixed_name = fixed_name.replace(',', '') fixed_name = fixed_name.replace("'", '') fixed_name = fixed_name.replace("/", "") #finding and download from YouTube and tagging self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']) self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') def downloadFromYoutubeMusic(self, url, info, path): print(info) uri = info['uri'] #downloading video from youtube if self.__youtube.download(url=url, path=uri, filename=uri): #converting video to mp3 file self.__youtube.convertVideoToMusic(uri=uri) self.__editor.setTags(data=info) cachepath = os.getcwd() + '/cache' fullpath = os.getcwd() + '/Downloads' if not os.path.exists(fullpath): os.makedirs(fullpath) name = f'{info["artist"][0]} - {info["name"]}' os.rename(f"{cachepath}/{info['uri']}/{info['uri']}.mp3", f"{fullpath}/{getCorrect(name)}.mp3") if path: os.rename(f"{fullpath}/{getCorrect(name)}.mp3", f"{path}/{getCorrect(name)}.mp3") #deleting cache try: shutil.rmtree(f"cache/{info['uri']}") except: pass notify.send(f'{info["artist"][0]} - {info["name"]}') return True, info else: return False, None def search(self, query): return self.__spotify.search(query=query)
def start_youtube(): for target_id in channel_id: y = Youtube(target_id) y.start()
def answerQuery(query): global selectedRecipe answer = {'text': "", 'voice': ''} nlc = NLC() my_class = "" if query: if query.endswith('?'): query = query[:-1] my_class = nlc.classify(query) #possible options: if my_class == "current": #Read the current step answer['text'] = selectedRecipe.getCurrentDirection() elif my_class == "next": #Read the next step answer['text'] = selectedRecipe.goForward() elif my_class == "previous": #Read the previous step answer['text'] = selectedRecipe.goBack() elif my_class == "specific": #Read a specific step (query~"What was the first step?") words = query.split() index = -1 key_words = { 'first': 0, 'start': 0, 'beginning': 0, 'second': 1, 'third': 2, 'fourth': 3, 'fifth': 4, 'sixth': 5, 'seventh': 6, 'eighth': 7, 'ninth': 8, 'tenth': 9, 'eleventh': 10, 'twelfth': 11, 'last': len(selectedRecipe.directions) - 1, 'end': len(selectedRecipe.directions) - 1, 'finish': len(selectedRecipe.directions) - 1 } for word in words: if word in key_words: index = key_words[word] if index > len(selectedRecipe.directions) - 1 or index < 0: answer['text'] = "The recipe does not have that many steps" else: answer['text'] = selectedRecipe.getSpecificDirection(index) elif my_class == "ingredients": #Find out current ingredient (query~"How much of that?") relaventIngredients = selectedRecipe.getIngredientFromQuery(query) answer['text'] = relaventIngredients.pop()['Text'] for ing in relaventIngredients: answer['text'] = answer['text'] + " and " + ing['Text'] elif my_class == "conversion": unitNames = [ u.symbol for _, u in units.__dict__.items() if isinstance(u, type(units.deg)) ] + [ u for u, f in u.__dict__.items() if isinstance(f, type(units.deg)) ] unitNames.extend([ 'tablespoon', 'teaspoon', 'tsp', 'tbsp', 'ounces', 'Celsius', 'Fahrenheit', 'milliliter', 'liter' ]) unitsFound = [] for token in word_tokenize(query): if (token in unitNames) or (token[:-1] in unitNames): if (token != 'in' and token != 'do' and token != 'use' and token != 'to'): unitsFound.append(token) app.logger.info(unitsFound) if (len(unitsFound) == 1): for ing in selectedRecipe.getIngredientFromQuery(query): conversionQuery = "What is " + ing['Val'] + " " + ing[ 'Unit'] + " in " + unitsFound[0] app.logger.info(conversionQuery) if answer['text']: answer['text'] = answer['text'] + " and " answer['text'] = answer['text'] + UnitConverter.getConversion( conversionQuery) elif (len(unitsFound) > 1): answer['text'] = UnitConverter.getConversion(query) elif (len(unitsFound) == 0): answer['text'] = "Sorry, I don't know how to convert that." elif my_class == "howto": answer['text'] = Youtube.getVideo(query) elif my_class == "temperature": answer['text'] = selectedRecipe.getTemperatureDirectionForQuery(query) elif my_class == "time": answer['text'] = selectedRecipe.getTimeDirectionForQuery(query) else: answer = {'text': "Sorry, I didn't get that.", 'voice': ''} return answer
from youtube_s import Youtube_S from dang_error import DangError from helpers import get_text, get_error_text, get_config, config_files_to_env import logging logging.basicConfig() logging.getLogger().setLevel(logging.INFO) load_dotenv() config_files_to_env() DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') bot = commands.Bot(command_prefix=['dang! ', '!dang ', 'dang!'], help_command=None) bot.add_cog(Youtube(bot)) bot.add_cog(Youtube_S(bot)) def get_random_quote(guild): return choice(get_text(guild.id, "quotes")) def magic_eight_ball(guild): return choice(get_text(guild.id, "magic_eight_ball")) def should_send_random_message(guild=None): freq = get_config(guild.id, "spam_freq") if (freq <= 0): return False
class RecordVideo: logger = logging.getLogger(__name__) def __init__(self, record, cfg): self.record = record self.config = cfg self.video_dir = os.path.join(self.config.video_dir, self.id(), "") if not os.path.exists(self.video_dir): os.makedirs(self.video_dir) self.video_path = os.path.join(self.video_dir, "video.mp4") self.audio_dir = os.path.join(self.config.audio_dir, self.id(), "") if not os.path.exists(self.audio_dir): os.makedirs(self.audio_dir) self.audio_path = os.path.join(self.audio_dir, "audio.mp3") self.dictionary = dict() index = 0 for column in record: self.dictionary["{{Column" + chr(index + 65) + "}}"] = self.get_column(chr(index + 65)) index += 1 if self.config.bitly: self.dictionary["{{ColumnD_bitly}}"] = util.bitly_shorten(self.get_column("D"))["url"] else: self.dictionary["{{ColumnD_bitly}}"] = self.get_column("D")["url"] def id(self): return self.get_column("B") def get_column(self, letter): return self.record[ord(letter) - 65] def get_images(self): self.img_dir = os.path.join(self.config.img_dir, self.get_column("B")) if not os.path.exists(self.img_dir): os.makedirs(self.img_dir) self.img_paths = [] index = 0 for url in self.get_column("I").split(","): img_name = url.rsplit("/", 1)[-1] # path = os.path.join(self.record_path, str(index).zfill(3) + ".jpg") path = os.path.join(self.img_dir, img_name) if not self.config.cache_img or not os.path.exists(path): util.download_url(url, path) time.sleep(1) if os.stat(path).st_size > 0: self.img_paths.append(path) else: self.logger.warn("Removed file: " + path + " from url: " + url) os.remove(path) index += 1 def create(self): self.write_text_files() if self.config.cache_video and os.path.exists(self.video_path): return self.get_images() self.fix_images() if self.config.use_audio and not os.path.exists(self.audio_path) or not self.config.cache_audio: util.from_text_to_speech(self.get_column("J"), self.audio_path) if self.config.use_audio: audio_length = util.get_audio_length(self.audio_path) else: audio_length = 45.0 args3 = ["ffmpeg", "-y", "-t", str(audio_length)] index = 0 # max_ = max(len(self.img_paths), audio_length / 5.0) max_ = len(self.img_paths) min_ = 0 for path in self.img_paths: if index >= min_ and index < max_: args3.extend(["-loop", "1", "-t", "2.5", "-i", path]) index += 1 if self.config.use_audio: args3.extend(["-i", self.audio_path]) args3.extend(["-filter_complex"]) filter = "" index = 0 outputs = "" for path in self.img_paths: if index >= min_ and index < max_ - 1: real_index = index - min_ i = str(real_index) i2 = str(real_index + 1) filter += ( "[" + i2 + ":v][" + i + ":v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b" + i2 + "v];\n" ) outputs += "[" + i + ":v][b" + i2 + "v]" index += 1 c = str((real_index + 1) * 2 + 1) i = str(real_index + 1) filter += outputs + "[" + i + ":" + "v]concat=n=" + c + ":v=1:a=0,format=yuvj422p," text = util.apply_text( self.get_column("A"), self.config.font_file_avenir_black, 36, color="black", movement_x="151", movement_y="51", ) text += "," + util.apply_text( self.get_column("A"), self.config.font_file_avenir_black, 36, color="white", movement_x="150", movement_y="50", ) filter += "" + text filter += "[v]" args3.append(filter) args3.extend(["-map", "[v]"]) if self.config.use_audio: args3.extend(["-map", str(max_) + ":0"]) args3.extend(["-t", str(audio_length), self.video_path]) if util.run_args(args3) != 0: self.logger.warn("Video creation failed!") if os.path.exists(self.video_path): os.remove(self.video_path) def fix_images(self): self.logger.debug("Fixing images.") util.run_args( ["mogrify", "-sampling-factor", "2x2,1x1,1x1", "-resample", "300x300", "-resize", "1024x768!", "*.jpg"], cwd=self.img_dir, ) def write_text_files(self): title = util.template(self.config.youtube_title, self.dictionary) description = util.template(self.config.youtube_description, self.dictionary) util.text_to_file(title, os.path.join(self.video_dir, "title.txt")) util.text_to_file(description, os.path.join(self.video_dir, "description.txt")) def get_csv_row(self): row = [ util.template(self.config.youtube_title, self.dictionary), util.template("{{ColumnA}}", self.dictionary), util.template(self.config.youtube_description, self.dictionary), u"Travel", self.video_path, ] row2 = [] for r in row: row2.append(r.replace('"', '\\"').replace("\n", " ").replace("\r", " ")) return row2 def upload(self): self.youtube = Youtube() title = util.template(self.config.youtube_title, self.dictionary) description = util.template(self.config.youtube_description, self.dictionary) category_id = self.config.youtube_category_id tags = [] upload_result = self.youtube.initialize_upload(title, description, self.video_path, category_id, tags) self.youtube_videoid = upload_result["id"] return upload_result["id"]
def __init__(self): self.items = [] self.wiki = Wiki() self.youtube = Youtube() self.path = os.getcwd() self.s = schedule.every().saturday.do(self.job)
class Scheduler: def callThread(self, onExit, popenArgs): def runInThread(onExit, popenArgs): proc = subprocess.call(popenArgs) #proc.wait() onExit() return thread = threading.Thread(target=runInThread, args=(onExit, popenArgs)) thread.start() return thread def job(self): print "executing job" def onExit(): compress_file = "medicamentos_vademecum_out.tar.gz" listOut = os.listdir("./vademecum/resultsMedicamentos/") compress(listOut, "./vademecum/resultsMedicamentos/", "../out/" + compress_file) shutil.rmtree("./vademecum/resultsMedicamentos/") self.callThread(onExit, ["scrapy", "runspider", "vademecum/medicamentos.py"]) def __init__(self): self.items = [] self.wiki = Wiki() self.youtube = Youtube() self.path = os.getcwd() self.s = schedule.every().saturday.do(self.job) def splitPath(self, petition): names = petition.split("/") names = names[len(names) - 1].split(".") return names def remove_files(self, petition): with open(petition) as f: elem = f.readlines() for e in elem: if os.path.isfile("../out/" + e.strip()): os.remove("../out/" + e.strip()) os.remove(petition) def enqueue(self, x): print schedule.jobs print "enqueue petition" #schedule.run_pending() name = self.splitPath(x)[0] if (name == "remove"): print "remove" print x self.remove_files(x) else: splitPetition = name.split("_") typePetition = splitPetition[len(splitPetition) - 1] self.insertOrderListPetition(priority[typePetition], x) def dequeue(self): try: return self.items.pop(0) except: raise ValueError("there are no petitions!") def isEmpty(self): return self.items == [] def process(self, p=None): petition = self.dequeue() if p is None else p names = self.splitPath(petition) try: list = decompress(names[0], petition) except OSError as osE: print "error with a file" os.chdir(self.path) shutil.rmtree("../out/" + names[0]) self.process(petition) listOut = [] os.mkdir(list['dir'] + "/result/") try: for l in list['listDir']: if names[0].split("_")[1] == "wiki": params = self.wiki.search(l, names[0], list['dir'] + "/result/") if params and len(params) > 0: for d in params: listOut.append(d) compress_file = names[0] + "_out.tar.gz" compress(listOut, list['dir'] + "/result/", list['dir'] + "/../" + compress_file) elif names[0].split("_")[1] == "youtube": self.youtube.search(l, names[0], list['dir'] + "/result/", names[0]) except OSError as osE: print osE print "error with a file" os.chdir(self.path) shutil.rmtree(list['dir']) except Exception as eEx: print eEx os.chdir(self.path) shutil.rmtree(list['dir']) time.sleep(15) self.process(petition) except e: print e print "cannot get resources, check internet connection!" os.chdir(self.path) shutil.rmtree(list['dir']) time.sleep(15) self.process(petition) print "remove" print petition print os.getcwd() os.remove(petition) os.chdir(self.path) def insertOrderListPetition(self, num, petition): i = 0 #NO FUNCIONA: if (len(self.items) > 0): name = self.splitPath(self.items[i])[0] splitPetition = name.split("_") typePetition = splitPetition[len(splitPetition) - 1] numType = priority[typePetition] i += 1 while i < len(self.items) and numType <= num: name = self.splitPath(self.items[i])[0] splitPetition = name.split("_") typePetition = splitPetition[len(splitPetition) - 1] numType = priority[typePetition] i += 1 lenA = len(self.items) for j, val in self.items: self.items.insert(lenA - j, self.items[lenA - j - 1]) self.items.insert(i, petition)
from exalead import Exalead from quora import Quora from youtube import Youtube from parsijoo import Parsijoo scrapers = { 'g': Google(), 'b': Bing(), 'y': Yahoo(), 'd': Duckduckgo(), 'a': Ask(), 'yd': Yandex(), 'u': Baidu(), 'e': Exalead(), 'q': Quora(), 't': Youtube(), 'p': Parsijoo() } def read_in(): lines = sys.stdin.readlines() return json.loads(lines[0]) def small_test(): assert isinstance(scrapers.google.results_search('fossasia'), list) def feedgen(query, engine, count=10): if engine in ['q', 't']:
data = json.load(user_id_file) user_id = data["username"] print(user_id) if (user_id == ""): mqtt_thred_to_get_userid = threading.Thread( target=mqttclient_to_get_userid) mqtt_thred_to_get_userid.daemon = True mqtt_thred_to_get_userid.start() else: TTS("Welcome back " + user_id) '''this loop checks whether the username had been passed or not.''' '''if new username is passed it is written onto disk''' while (True): if (len(user_id) > 0): with open(os.path.join(sys.path[0], "assets/user_id.json"), 'r+') as user_id_file: data = {"username": user_id} user_id_file.seek(0) json.dump(data, user_id_file) user_id_file.truncate() break youtube_instance = Youtube() '''calling the main message thread from here''' mqtt_thred = threading.Thread(target=mqttclient) mqtt_thred.daemon = True mqtt_thred.start() while (True): if active == False: print("Exiting main thread") sys.exit()
from youtube import Youtube s1omething = Youtube() s1omething.youtube_subscriptions()