Skip to content

Instantly share code, notes, and snippets.

@codeliger
Created April 24, 2018 02:52
Show Gist options
  • Select an option

  • Save codeliger/9a427f7c5898ba6fc8113400c9720913 to your computer and use it in GitHub Desktop.

Select an option

Save codeliger/9a427f7c5898ba6fc8113400c9720913 to your computer and use it in GitHub Desktop.
import json
import requests
import sys
import os
import multiprocessing
from PIL import Image
from math import ceil
import logging
logging.basicConfig()
''' Command line usage:
Open a command prompt in the same directory as a script
Type this, use auto complete on thumbnail.py by using spacebar
python thumbnail.py [PERISCOPE BROADCAST URL]
'''
PROCESSES = 20
def tn(thumbnail):
_, _, file_path, remote_url = thumbnail
with open(file_path,'wb') as f:
f.write(requests.get(remote_url).content)
# Converts https://prod-thumbnail.pscp.tv/cNvhuX6P1anVjKqd1q9Op1SiRZxwiJ2g0pb_19tSi-UJrFpyhtVvChSccB4OCkJteA-nt_fF29SHNA2QDzI0TQ/orig.jpg?Expires=1523921203&Signature=aR6fvNUVYkcdo1RGqKQbm4~8S-jfCZmOdXDcGpzJtq6jFnhjO5x~5cGFEp4o2MWr~QqU6hnB55a4Xl1E4qUQ75T7qdxtVG2YqE3WBMmV6p7xtH1Vth04yb0oArsYgyFosZOMzndX5UznaD83ULtiIM3-23nYIO7ZkloVSGsKrSauRGLvqnP95CNCh0xT4iS~QK-vEBv18rzhuhaFWR0vUVeb~EY8AM3-vdrHvqGnIntOFbFT7hfOsQ63ELtiTXFycTgIZ4-fNzeWqyQ8BDo7RNUby84DonvOxRoXa07VoVMBvWGL7IXtr6Xa5vyJiQb5uc--x1MUybJB2T~8j64FUw__&Key-Pair-Id=APKAIHCXHHQVRTVSFRWQ to {thumbnail_name}.jpg
def get_name(url):
return url.split('/')[-1].split('?')[0]
if __name__ == '__main__':
broadcast = sys.argv[1]
api = 'https://api.periscope.tv/api/v2/publicReplayThumbnailPlaylist?broadcast_id={}'
id = broadcast.split('/')[-1]
path = os.path.join('.','thumbnails',id)
listing = []
if not os.path.exists(path):
os.makedirs(path)
else:
listing = os.listdir(path)
response = requests.get(api.format(id))
if response.ok:
thumbnail_objects = json.loads(response.content)['chunks']
else:
raise Exception('Response was {} body:\n{}'.format(response.status_code, response.content))
total = len(thumbnail_objects)
thumbnails = []
filenames = []
for tn_object in thumbnail_objects:
tn_url = tn_object['tn']
tn_filename = get_name(tn_url)
filenames.append(tn_filename)
# Ignore already existing chunks
if tn_filename in listing:
continue
tn_folder_path = path
tn_file_path= os.path.join(tn_folder_path,tn_filename)
thumbnails.append((tn_filename,tn_folder_path,tn_file_path,tn_url))
if len(thumbnails) > 0:
print('Sending {}/{} thumbnails to {} processes'.format(len(thumbnails),total,PROCESSES))
pool = multiprocessing.Pool(processes=min(PROCESSES,len(thumbnails)))
pool.map(func=tn,iterable=thumbnails)
pool.close()
else:
print('All thumbnails already downloaded.')
tn_objects = []
for filename in filenames:
i = Image.open(os.path.join(path,filename))
width, height = i.size
if width > height:
i = i.rotate(90,expand=True)
i.save(os.path.join(path,filename))
tn_objects.append(i)
tn_count = len(tn_objects)
tn_width, tn_height = tn_objects[0].size
rows = ceil(tn_count / 10) # 10 columns
joined_width = 10 * tn_width
joined_height = rows * tn_height
preview = Image.new('RGB',(joined_width,joined_height))
index = 0
for y in range(0,rows):
for x in range (0,10):
tn = tn_objects[index]
tn_xtopleft = x * tn_width
tn_ytopleft = y * tn_height
preview.paste(tn,(tn_xtopleft,tn_ytopleft))
if index < tn_count-1:
index+=1
else:
break
print('Download complete in folder {}'.format(path))
print('Creating thumbnail preview image')
preview_path = os.path.join('.','{}.jpg'.format(id))
preview.save(preview_path)
print('Opening preview!')
os.startfile(preview_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment