Skip to content

Instantly share code, notes, and snippets.

@simark
Forked from eepp/toutv-v2.py
Last active March 9, 2016 06:27
Show Gist options
  • Save simark/ca21d9e3bfec69cfb18c to your computer and use it in GitHub Desktop.
Save simark/ca21d9e3bfec69cfb18c to your computer and use it in GitHub Desktop.
import requests
import mechanicalsoup as ms
import sys
import logging
import urllib
import json
import subprocess
# Uncomment this to make the requests print some debug info.
#
#logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
#logging.getLogger().setLevel(logging.DEBUG)
#requests_log = logging.getLogger('requests.packages.urllib3')
#requests_log.setLevel(logging.DEBUG)
#requests_log.propagate = True
_TOUTVAPP_HEADERS = {
'User-Agent': 'TouTvApp/2.3.1 (iPhone4.1; iOS/7.1.2; en-ca)'
}
# Uncomment this if using a proxy such as mitmproxy to inspect the requests.
_PROXIES = {
# 'http': '127.0.0.1:8080',
# 'https': '127.0.0.1:8080',
}
def _toutvapp_get(url, headers=None, params=None):
actual_headers = {}
actual_headers.update(_TOUTVAPP_HEADERS)
if headers is not None:
actual_headers.update(headers)
r = requests.get(url=url, headers=actual_headers, verify=False,
params=params, proxies=_PROXIES)
return r
# get the big JSON blob containing endpoint URLs n stuff
r = _toutvapp_get('http://ici.tou.tv/presentation/settings?v=2&d=phone-ios')
pres_settings = r.json()
mobile_scopes = pres_settings['MobileScopes']
client_id = pres_settings['LoginClientIdIos']
endpoint_auth = pres_settings['EndpointAuthorizationIos']
# concoct URL of login page including the reputed iOS client ID
url = '{}?response_type=token&client_id={}&scope={}&state=authCode&redirect_uri=http://ici.tou.tv/profiling/callback'
url = url.format(endpoint_auth, client_id, mobile_scopes)
# configure requests session
session = requests.Session()
session.verify = False
session.proxies = _PROXIES
session.headers = _TOUTVAPP_HEADERS
# create browser
browser = ms.Browser(session=session)
# get the login page. this page contains the session ID and other
# hidden form inputs.
login_page = browser.get(url)
# fill the form with user creds
login_form = login_page.soup.select('#Form-login')[0]
login_form.select('#login-email')[0]['value'] = sys.argv[1]
login_form.select('#login-password')[0]['value'] = sys.argv[2]
# submit the form. this returns a requests reponse which followed
# redirects. we can find the first response in its history. this
# first response includes super secret stuff (auth tokens and shit)
# in its Location header.
page = browser.submit(login_form)
# extract said super secret stuff from the fragment
url_components = urllib.parse.urlparse(page.history[0].headers['Location'])
qs_parts = urllib.parse.parse_qs(url_components.fragment)
token_type = qs_parts['token_type'][0]
access_token = qs_parts['access_token'][0]
# concoct Authorization header
le_dict = {
'Authorization': '{} {}'.format(token_type, access_token),
'ClientID': client_id,
'RcId': '',
'Accept': 'application/json',
}
# get user infos now that we have the access token and token type
r = _toutvapp_get('http://ici.tou.tv/profiling/userprofile?v=2&d=phone-ios',
headers=le_dict)
subprocess.call(['figlet', 'INFOS'])
print(json.dumps(r.json(), indent=4))
sys.stdout.flush()
# get
r = _toutvapp_get('http://ici.tou.tv/presentation/switch-et-bitch?excludeLineups=False&smallWidth=220&mediumWidth=600&largeWidth=800&v=2&d=phone-ios',
headers=le_dict)
subprocess.call(['figlet', 'SHOW'])
print(json.dumps(r.json(), indent=4))
sys.stdout.flush()
# __ _______ _ ____ _____ _ _ _____ ____ _____
# \ \ / / ____( ) _ \| ____| | | | | ____| _ \| ____|_
# \ \ /\ / /| _| |/| |_) | _| | |_| | _| | |_) | _| (_)
# \ V V / | |___ | _ <| |___ | _ | |___| _ <| |___ _
# \_/\_/ |_____| |_| \_\_____| |_| |_|_____|_| \_\_____(_)
#
# User infos:
#
# GET http://ici.tou.tv/profiling/userprofile?v=2&d=phone-ios
# ← 200 application/json 565B 425ms
#
# List of sections:
#
# GET http://ici.tou.tv/presentation/section?v=2&d=phone-ios
# ← 200 application/json 1.41kB 136ms
#
# List of summaries of shows (display text, ID, etc. for quick search):
#
# GET http://ici.tou.tv/presentation/search?v=2&d=phone-ios
# ← 200 application/json 511.5kB 349ms
#
# Specific section (contains shows and latest episodes):
#
# GET http://ici.tou.tv/presentation/section/a-la-une?smallWidth=220&mediumWidth=600&largeWidth=800&includePartnerTeaser=true&v=2&d=phone-ios
# ← 200 application/json 92.37kB 221ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment