Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. if "yt: " in msg.text.lower():
  2. query = msg.text.split(":")
  3. try:
  4. if len(query) == 3:
  5. isi = yt(query[2])
  6. hasil = isi[int(query[1])-1]
  7. cl.sendText(msg.to, 'Hasil Pencarian = ' + str(len(isi)) + " ini adalah hasil pencarian ke [" + str(query[1]) + "]")
  8. cl.sendText(msg.to, hasil)
  9. else:
  10. isi = yt(query[1])
  11. cl.sendText(msg.to, "Hasil Pencarian = " + str(len(isi)))
  12. cl.sendText(msg.to, isi[0])
  13. except Exception as e:
  14. print(e)
  15.  
  16.  
  17.  
  18.  
  19.  
  20. def yt(query):
  21. with requests.session() as s:
  22. isi = []
  23. if query == "":
  24. query = "S1B nrik"
  25. s.headers['user-agent'] = 'Mozilla/5.0'
  26.  
  27. url = 'http://www.youtube.com/results'
  28. params = {'search_query': query}
  29.  
  30. r = s.get(url, params=params)
  31. soup = BeautifulSoup(r.content, 'html5lib')
  32.  
  33. for a in soup.select('.yt-lockup-title > a[title]'):
  34. if '&List' not in a['href']:
  35. if 'watch?v' in a['href']:
  36. b = a['href'].replace('watch?v=','')
  37. isi += ['youtu.be' + b]
  38. return isi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement