-
Notifications
You must be signed in to change notification settings - Fork 3
/
playstv.py
53 lines (42 loc) · 1.69 KB
/
playstv.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
import requests
import re
import config
import HTMLParser
# user agent used to access playstv
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
DOWNLOAD_URL = 'http://m0playscdntv-a.akamaihd.net/video/%s/processed/720.mp4'
# regex used to find the id of a playstv video
re_vidid = re.compile('akamaihd\.net\/video\/([a-zA-Z0-9-_]*)\/processed')
# regex used to find the title of a playstv video
re_title = re.compile(ur'data-share-text=\\"(.*?)\\"')
# regex used to find the author of a playstv video
re_author = re.compile(ur'\\"original_author_urlname\\":\\"(.*?)\\"')
def get_title(url):
h = HTMLParser.HTMLParser()
html = h.unescape(requests.get(url, headers={'User-Agent': USER_AGENT}).content.encode('utf-8'))
match_title = re.search(re_title, html)
if match_title:
return match_title.group(1)
else:
return None
def get_video_id(url):
h = HTMLParser.HTMLParser()
html = h.unescape(requests.get(url, headers={'User-Agent': USER_AGENT}).content.encode('utf-8'))
match_id = re.search(re_vidid, html)
if match_id:
return match_id.group(1)
else:
return None
def get_author(url):
h = HTMLParser.HTMLParser()
html = h.unescape(requests.get(url, headers={'User-Agent': USER_AGENT}).content.encode('utf-8'))
match_id = re.search(re_author, html)
if match_id:
return match_id.group(1)
else:
return None
def download(video_id):
req = requests.get(DOWNLOAD_URL % video_id, stream=True)
with open(config.DOWNLOAD_FOLDER + video_id + '.mp4', 'wb') as f:
for chunk in req.iter_content(chunk_size=1024):
if chunk: f.write(chunk)