async def youtube_search(query, parse_text=True, return_info=False, limit_duration=False, duration_limit=600): if query in ('maagnolia', 'magnolia') and return_info: query = 'magnolia (Audio)' # icon = 'https://cdn4.iconfinder.com/data/icons/social-media-icons-the-circle-set/48/youtube_circle-512.png' results, kind = 1, 'video' if parse_text: p = re.compile('--[1-4][0-9]|--[1-2]') with suppress(AttributeError): results = int(p.search(query).group()[2:]) p = re.compile('--playlist') # defaults to video so I removed --video with suppress(AttributeError): kind = p.search(query).group()[2:] with suppress(ValueError): query = query[query.index(' '):query.index('--')] max_results = min(results + 5, 50) if kind == 'playlist': search = PlaylistsSearch(query, limit=max_results) else: search = VideosSearch(query, limit=max_results) search_response = await search.next() search_response = search_response['result'] valid_result = None for search_result in search_response: duration = 0 if kind == 'video': for num in search_result['duration'].split(':'): duration = duration * 60 + int(num) if not limit_duration or duration < duration_limit: valid_result = search_result break else: # playlist valid_result = search_result break # TODO: raise ValueError(f'No {kind} found') if valid_result is None: return f'No {kind} found' # TODO: return only title and id if return_info: return valid_result['link'], fix_youtube_title(valid_result['title']), valid_result['id'] return valid_result['link']
async def search(self, ctx, *, search_terms: str): ''' Searches youtube for its first three results for search terms ''' try: # Search with search terms video_search = VideosSearch(search_terms, limit=3) video_results = await video_search.next() except: print('Could not complete search') await ctx.send('Invalid search') return # Store search results self.search_results = video_results['result'] # Initialize embed for search results search_embed = discord.Embed(title='Search Results', description=f'for "{search_terms}"', color=discord.Color.blurple()) for index, video in enumerate(video_results['result']): search_embed.add_field( name=f'{index+1}. {video["title"]}', value= f'{video["channel"]["name"]} - {video["duration"]} - {video["viewCount"]["text"]}' ) await ctx.send(embed=search_embed)
async def get_yt_link(query): vid_ser = VideosSearch(query, limit=1) res = await vid_ser.next() results = res["result"] for i in results: link = i["link"] return link
async def scrape_youtube(search_strings): results = {} for search_string in search_strings: videosSearch = VideosSearch(search_string, limit=5) videosResult = await videosSearch.next() scraped_data = [] for result in videosResult["result"]: scraped_data.append({ "title": result["title"], "url": result["link"] }) results[search_string] = scraped_data return results
else: if (url_from_msg := await find_url_from_msg(m, show_err=False)) and ( yt_id := await yt_x_bleck_megik(url_from_msg[0]) ): audio_msg = url_from_msg[1] else: LOG.info("No Valid URL found now searching for given text") if m.input_str: search_q = m.input_str audio_msg = m elif reply and (reply.text or reply.caption): search_q = reply.text or reply.caption audio_msg = reply else: return await m.err("No Input Found", del_in=5) videosSearch = VideosSearch(search_q.strip(), limit=1) videosResult = await videosSearch.next() if len(res := videosResult["result"]) == 0: return await m.err(f'No Result found for Query: "{search_q}"') yt_id = res[0]["id"] if not (vid_info := await get_ytvid_info(yt_id)): LOG.info("Something Went Wrong :P") return duration = vid_info["duration"] audio_key = yt_id if err_msg := check_audio(duration, audio_key, playlist): return await m.err(err_msg, del_in=7) title = vid_info["title"] thumb = vid_info["thumb"] await m.delete() await append_playlist(
async def __getVideo(self, videoName): search = VideosSearch(videoName, limit=1) result = await search.next() return result['result'][0]
def search_for_music(name_of_song): return VideosSearch(name_of_song, limit = 10)