-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_twitch_clips.py
60 lines (43 loc) · 1.74 KB
/
get_twitch_clips.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
# by chuski1212
# 13/04/2018
import requests
import settings
import urllib.request
import os
# to override default user-agent of urllib
class AppURLopener(urllib.request.FancyURLopener):
version = "Mozilla/5.0"
opener = AppURLopener()
headers = {
'Accept': 'application/vnd.twitchtv.v5+json',
'Client-ID': settings.CLIENTID,
'User-Agent': 'Mozilla/5.0'
}
def download_clip(url, broadcaster, title, lang, position):
# tell the user where we're downloading to
outputpath = (settings.BASEDIR + 'twitch/' + lang + '/' + str(position) + '_' + title + '_' + broadcaster + '.mp4').replace('\n', '')
print(outputpath)
# get html content from url
html = str(opener.open(url).read())
# extract the mp4 url for source quality
mp4url = html.split('source\":\"')[1].split('\"}')[0].split('"')[0]
print(mp4url)
# download file to output path
urllib.request.urlretrieve(mp4url, outputpath)
def get_clips_by_lang(lang):
if lang != 'all':
lang_request = '&language=' + lang
else:
lang_request = ''
print('Downloading TOP 30 last 24h ' + lang + ' Fortnite clips')
if not os.path.exists(settings.BASEDIR + 'twitch/' + lang + '/'):
os.makedirs(settings.BASEDIR + 'twitch/' + lang + '/')
response = requests.get('https://api.twitch.tv/kraken/clips/top?game=' + settings.GAME +
'&period=' + settings.PERIOD +
'&limit=' + settings.LIMIT +
lang_request, headers=headers)
for i, clip in enumerate(response.json()['clips']):
download_clip(clip['url'], clip['broadcaster']['name'], clip['title'], lang, i)
print('Download finished')
for lang in settings.LANGS:
get_clips_by_lang(lang)