-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
37 lines (33 loc) · 1.17 KB
/
Copy pathlambda_function.py
File metadata and controls
37 lines (33 loc) · 1.17 KB
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
from datetime import datetime
import requests
import json
import time
import os
import boto3
def lambda_handler(event, context):
number_of_videos = os.environ.get("NUMBER_OF_VIDEOS")
bucketname= os.environ.get("BUCKET_NAME")
client_id = os.environ.get("CLIENT_ID")
url="https://api.twitch.tv/kraken/clips/top?limit="+str(number_of_videos)+"&game=League%20of%20Legends&trending=true&period=week"
referrer = "google.ie"
headers = {
'Client-ID' : client_id,
'Accept': 'application/vnd.twitchtv.v5+json'
}
response = requests.get(url, headers=headers, timeout=10)
response_json = json.loads(response.content)
today = datetime.now().strftime("%d_%m_%Y")
clips=[]
for item in response_json['clips']:
temp = item['thumbnails']['medium']
slug='-'.join(temp.split('-')[:-2])+".mp4"
clips.append(slug)
for clip in clips:
r=requests.get(clip)
key=clip.split('/')[-1]
s3=boto3.client('s3')
final_key = 'videos/' +today+'/'+key
place='/tmp/'+key
with open(place, 'wb') as f:
f.write(r.content)
s3.upload_file(place, bucketname,final_key)