Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Hulu(UserLibrary):
- def __init__(self, user, password):
- UserLibrary.__init__(self)
- self.platform = 'Hulu'
- self.username = user
- self.password = password
- self.cookies = None
- self.csrf = None
- self.session = None
- self.headers = {
- "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
- "Accept-Encoding" : "gzip, deflate, sdch",
- "Accept-Language" : "en-US,en;q=0.8,pt;q=0.6",
- "Connection" : "keep-alive",
- "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",
- }
- def _build_url_from_json(self, json):
- return 'http://www.hulu.com/watch/%s' % json['video']['id']
- def login(self):
- # This was the biggest pain I have ever had to do.
- # So to give an idea of what steps are needed to take to be
- # Step 1: Obtain the cookie, think of this as XSRF token.
- cookie_url = 'http://www.hulu.com/api/2.0/begin_login?_user_pgid=1&_content_pgid=29635&_device_id=1®ion=us&locale=en&language=en'
- cookie_session = Session()
- cookie_req = cookie_session.get(cookie_url)
- self.cookies = {
- 'login': self.username,
- 'password': self.password
- }
- if cookie_req.json().get('is_locked_out') == True:
- raise Exception('Account is currently locked out form Hulu services.')
- self.session = cookie_session
- # Step 2: Convert the cookie.
- for k,v in cookie_session.cookies.items():
- self.cookies[k] = v
- # Step 3: Do the actual JSONP login. Why this is over GET / using cookies is beyond me..
- login_url = 'https://secure.hulu.com/api/2.0/authenticate?callback=result&_user_pgid=1&_content_pgid=29635&_device_id=1®ion=us&locale=en&language=en'
- login_req = self.session.get(login_url, cookies=self.cookies)
- self.cookies = login_req.cookies
- page = self.session.get('https://secure.hulu.com/account/history', cookies=self.cookies, headers=self.headers)
- self.csrf = page.content.split('history":"')[1].split('"')[0]
- def get_data(self):
- if self.cookies is None:
- self.login()
- urls = set()
- index = 1
- pages = 2
- while index <= pages:
- 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)
- r = self.session.get(watch_url, cookies=self.cookies, headers=self.headers)
- data = r.json()
- pages = data['page_count']
- for video in data['data']:
- urls.add(self._build_url_from_json(video))
- print '>>>> Processed page: %s/%s' % (index, pages)
- index += 1
- return urls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement