This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.py
192 lines (177 loc) · 10.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import sys
import requests
import cookielib
import re
import json
import os
import sys
import random
debug = False
# 22/03/18 - updated file to work with slightly altered api of ustvnow. - MikeC
##################################################################################################################
# CONFIG
##################################################################################################################
try:
username = sys.argv[1]
except:
username = "" # Set username manually here if not using the command line
try:
password = sys.argv[2]
except:
password = "" # Set username manually here if not using the command line
try:
channel = sys.argv[3]
except:
channel = "ALL" # Set channel manually here if not using the command line (use ALL to show all)
try:
CreateSTRM = sys.argv[4]
except:
CreateSTRM = False # Set if you would like to create the STRM file
try:
hideTitle = sys.argv[5]
except:
hideTitle = True # For those of you who would like to hide the showing now show title
##################################################################################################################
## FUNCTIONS
##################################################################################################################
###########
# Decoder #
###########
def robust_decode(bs):
'''Takes a byte string as param and convert it into a unicode one. First tries UTF8, and fallback to Latin1 if it fails'''
bs.decode(errors='replace')
cr = None
try:
cr = bs.decode('utf8')
except UnicodeDecodeError:
cr = bs.decode('latin1')
return cr
###########
# Scraper #
###########
def Ustvnow(username, password, exclude=[]):
result = ""
with requests.Session() as s:
### Get CSRF Token ###
url="https://watch.ustvnow.com/account/signin"
r = s.get(url)
html = r.text
html = ' '.join(html.split())
ultimate_regexp = "(?i)<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>"
for match in re.finditer(ultimate_regexp, html):
i = repr(match.group())
if '<input type="hidden" name="csrf_ustvnow" value="' in i:
csrf = i.replace('<input type="hidden" name="csrf_ustvnow" value="','').replace('">','')
csrf = str(csrf).replace("u'","").replace("'","")
if debug:
print csrf
### Get Token ###
url = "https://watch.ustvnow.com/account/login"
payload = {'csrf_ustvnow': csrf, 'signin_email': username, 'signin_password':password, 'signin_remember':'1'}
r = s.post(url, data=payload)
html = r.text
html = ' '.join(html.split())
html = html[html.find('var token = "')+len('var token = "'):]
html = html[:html.find(';')-1]
token = str(html)
if debug:
print token
### Get Stream ###
device = "gtv"
url = "http://m-api.ustvnow.com/"+device+"/1/live/login"
payload = {'username': username, 'password': password, 'device':device}
r = s.post(url, data=payload)
html = r.text
j = json.loads(html)
if debug:
print j
if j['result'] != "success":
if debug:
print "error"
return False
url = "http://m-api.ustvnow.com/gtv/1/live/playingnow?token="+token
if debug:
print url
r = s.get(url)
html = r.text
j = json.loads(html)
if debug:
print j
n = 0
if channel=="ALL":
result += "#EXTM3U\n"
while True:
try:
scode = j['results'][n]['scode']
except:
break
stream_code = j['results'][n]['stream_code']
title = j['results'][n]['title']
title = u' '.join((title,"")).encode('utf-8').strip()
title = str(title)
url = "http://m.ustvnow.com/stream/1/live/view?scode="+scode+"&token="+token+"&br_n=Firefox&pr=ec&tr=expired&pl=vjs&pd=1&br_n=Firefox&br_v=54&br_d=desktop"
if debug:
print url
r = s.get(url)
html = r.text
try:
i = json.loads(html)
#URL = i["stream"].replace("\n","")[:-1]
URL = i["stream"]
#if "&jwt=" in URL:
# URL = URL.split("&jwt=")[0]
except:
break
# Create STRM file
if(CreateSTRM):
fname = stream_code+".strm"
if os.path.isfile(fname):
f= open(fname,"wb")
f.write(URL)
f.close
else:
f= open(fname,"wb+")
f.write(URL)
f.close
# Print Link
if channel=="ALL":
if robust_decode(stream_code) not in exclude:
if not hideTitle:
result += "#EXTINF:-1,"+robust_decode(stream_code)+" - "+robust_decode(title)+"\n"
else:
result += "#EXTINF:-1,"+robust_decode(stream_code)+"\n"
result += URL+"\n"
else:
if debug:
print stream_code
if channel==stream_code:
result += URL+"\n"
break
n += 1
return(result.strip("\n"))
##################################################################################################################
## EXCLUDE THESE CHANNELS
##
## exclude = ["ABC", "My9" "FOX"] these channels would be excluded from the result
##
##################################################################################################################
exclude = []
##################################################################################################################
## MAIN
##################################################################################################################
if(username==""):
#no username given, try predefined username and password
usernames = ["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"]
random.shuffle(usernames)
for username in usernames:
try:
password = username
result = Ustvnow(username, password, exclude)
if result != False:
break #success
except:
next #try again
else:
result = Ustvnow(username, password, exclude)
print result
##################################################################################################################