예제 #1
0
def get_youtube_urls(video_name: str, max_results: int = 10) -> List[str]:
    # get {max_results} results from a youtube query of the video name
    results = YoutubeSearch(video_name, max_results=max_results)
    # convert the object to a list of dictionaries
    results = results.to_dict()
    # make sure there is at least one item in the list of dictionaries
    assert len(results) > 0
    # return the videos' urls
    return ["youtube.com" + d['url_suffix'] for d in results]
예제 #2
0
def search_youtube(search_term, num_results):
    """ Searches YouTube for a specified search term
        and returns a set number of results.

    Args:
        search_term: The string to search on YouTube

    Returns:
        results: A dictionary of YouTube video details
    """

    results = YoutubeSearch(search_term, max_results=num_results)
    results_dict = results.to_dict()

    return results_dict
예제 #3
0
class Google:
    """query string"""
    def __init__(self,query='exso kamabay'):
        self.query = quote_plus(query)
        # Templates
        self.urlp = f"https://www.picsearch.com/index.cgi?q={self.query}"
        self.urlq = f"https://www.google.com/search?q={self.query}&safe=off"#&hl=%(language)s&cr=%(country)s&num=%(stop)s"%vars()
        self.urli = f"https://www.google.com/search?q={self.query}&tbm=isch"

    def get_content(self,url) -> str:
        """Mengambil konten alamat url"""
        try:
            self.url = requests.get(url,headers=random_user_agent())
            if self.url.status_code == 200:return self.url.content
            else:return f"Response status code {self.url.status_code}"
        except:return "Check koneksi internet!"

    def search_image(self,**kwargs):
        """
        Untuk 'images'
        start : int -> index pencarian 
        related_results : boolean -> mengembalikan hasil terkait
        Filter Pencarian
        color : str -> warna 
        :red,orange,yellow,green,teal,purple,pink,brown,gray,white,black
        size : str -> ukuran
        :small,medium,large,wallpaper
        orientation : str -> orientasi
        :portrait,landscape,square
        anim : str -> yes/no -> animasi
        face : str -> yes/no -> wajah
        Untuk 'related_results'
        hl : str -> bahasa query
        cr : str -> spesifik negara untuk hasil query
        """
        def related_results():
            self.lst_rst = []
            if not kwargs:
                return "no keywords!";
            else:
                for k,v in kwargs.items():
                    if k in ('hl','cr'):
                        self.urli += f'&%s=%s'%(k,v)
                    else:pass
            self.soup = BeautifulSoup(self.get_content(self.urli),'html.parser')
            for i in self.soup.find_all('a',{'class':'VFACy kGQAp sMi44c lNHeqe WGvvNb'}):
                try:
                    self.lst_rst.append({'title':i['title'],'url':i['href']})
                except:pass
            if not self.lst_rst:return 'terjadi kesalahan,silakan coba lagi!'
            else:return self.lst_rst;
        def search_results():
            self.lst_rst = []
            if not kwargs:pass
            else:
                for k,v in kwargs.items():
                    if k in ('color','size','orientation','anim','face','start'):
                        self.urlp += '&%s=%s'%(k,v)
                    else:pass
            self.soup = BeautifulSoup(self.get_content(self.urlp),'html.parser')
            for i,d in zip(
                self.soup.find_all('span',{'class':'result'}),
                self.soup.find_all('span',{'class':'dimensions'})):
                self.lst_rst.append({'url':'http:'+i.a.img['src'],'size':d.text})
            return self.lst_rst;
        if 'related_results' in kwargs.keys():
            if kwargs['related_results'] == True:
                return json.dumps({
                    'images':search_results(),
                    'related_results':related_results()
                },indent=2)
            else:
                return json.dumps({
                        'images':search_results(),
                        'related_results':'%s tidak valid, silakan diubah ke True'%(kwargs['related_results']),
                    },indent=1)
        else:return json.dumps({'images':search_results()},indent=1)

    def search_query(self,**kwargs):
        """
        start : int -> start search
        stop : int -> stop search
        lang : str -> language query
        country : str -> country code
        """
        if not kwargs:
            return "no keywords!";
        else:
            self.results = []
            def filter_(url):
                remove = url.strip('/url?q=')
                find = remove.find('&sa=U&ved=')
                return remove[:find]
            self.dict = dict({'lang':'hl','country':'cr','start':'start','stop':'num'})
            for k,v in kwargs.items():
                if k in self.dict.keys():
                    self.urlq += "&%s=%s"%(self.dict[k],v)
                else:pass
            self.soup = BeautifulSoup(self.get_content(self.urlq),'html.parser')
            for u,d,t in zip(
                self.soup.find_all('div',{'class':'kCrYT'}),
                self.soup.find_all('div',{'class':'BNeawe s3v9rd AP7Wnd'}), 
                self.soup.find_all('div',{'class':'BNeawe vvjwJb AP7Wnd'})
                ):
                if not 'pause' in kwargs.keys():pass
                else:timeout(kwargs['pause'])
                try:
                    # result
                    self.results.append({'url':filter_(u.a['href']),'title':t.text,'decription':d.text})
                except:pass
            if not self.results:
                return "sorry an error occurred please try again!"
            else:return json.dumps(self.results,indent=1)

    def search_video(self,max_search=5):
        "maximal search"
        self.query_ = self.query.replace('+',' ')
        self.results = YoutubeSearch(self.query_,max_search)
        return json.dumps(self.results.to_dict(),indent=2)