Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. class Hulu(UserLibrary):
  2. def __init__(self, user, password):
  3. UserLibrary.__init__(self)
  4. self.platform = 'Hulu'
  5. self.username = user
  6. self.password = password
  7. self.cookies = None
  8. self.csrf = None
  9. self.session = None
  10. self.headers = {
  11. "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  12. "Accept-Encoding" : "gzip, deflate, sdch",
  13. "Accept-Language" : "en-US,en;q=0.8,pt;q=0.6",
  14. "Connection" : "keep-alive",
  15. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36",
  16. }
  17.  
  18.  
  19. def _build_url_from_json(self, json):
  20. return 'http://www.hulu.com/watch/%s' % json['video']['id']
  21.  
  22. def login(self):
  23. # This was the biggest pain I have ever had to do.
  24.  
  25. # So to give an idea of what steps are needed to take to be
  26.  
  27. # Step 1: Obtain the cookie, think of this as XSRF token.
  28. cookie_url = 'http://www.hulu.com/api/2.0/begin_login?_user_pgid=1&_content_pgid=29635&_device_id=1&region=us&locale=en&language=en'
  29. cookie_session = Session()
  30. cookie_req = cookie_session.get(cookie_url)
  31. self.cookies = {
  32. 'login': self.username,
  33. 'password': self.password
  34. }
  35.  
  36. if cookie_req.json().get('is_locked_out') == True:
  37. raise Exception('Account is currently locked out form Hulu services.')
  38.  
  39. self.session = cookie_session
  40. # Step 2: Convert the cookie.
  41. for k,v in cookie_session.cookies.items():
  42. self.cookies[k] = v
  43.  
  44. # Step 3: Do the actual JSONP login. Why this is over GET / using cookies is beyond me..
  45. login_url = 'https://secure.hulu.com/api/2.0/authenticate?callback=result&_user_pgid=1&_content_pgid=29635&_device_id=1&region=us&locale=en&language=en'
  46. login_req = self.session.get(login_url, cookies=self.cookies)
  47. self.cookies = login_req.cookies
  48.  
  49. page = self.session.get('https://secure.hulu.com/account/history', cookies=self.cookies, headers=self.headers)
  50. self.csrf = page.content.split('history":"')[1].split('"')[0]
  51.  
  52.  
  53. def get_data(self):
  54. if self.cookies is None:
  55. self.login()
  56.  
  57. urls = set()
  58. index = 1
  59. pages = 2
  60.  
  61. while index <= pages:
  62. watch_url = 'http://www.hulu.com/api/2.0/retrieve_history?items_per_page=500&order=desc&page=%s&_user_pgid=1&_content_pgid=29635&_device_id=1&locale=en&language=en&csrf=%s' % (index, self.csrf)
  63. r = self.session.get(watch_url, cookies=self.cookies, headers=self.headers)
  64. data = r.json()
  65.  
  66. pages = data['page_count']
  67. for video in data['data']:
  68. urls.add(self._build_url_from_json(video))
  69.  
  70. print '>>>> Processed page: %s/%s' % (index, pages)
  71. index += 1
  72.  
  73. return urls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement