pandora_cdosea/playout/update.py

69 lines
2.1 KiB
Python
Executable File

#!/usr/bin/python3
import os
import requests
import string
import time
import datetime
import shutil
import sys
import socket
import ox
socket.setdefaulttimeout(10)
base = 'https://cdosea.0x2620.org/static/render/'
lock = '/tmp/update.lock'
folder = os.path.abspath(os.path.dirname(__file__))
os.chdir(folder)
lang = '1080p'
lang = 'no-en'
def get_subtitle(url, name):
try:
r = requests.get(url, stream=True)
with open('%s.tmp' % name, 'wb') as fd:
shutil.copyfileobj(r.raw, fd)
shutil.move('%s.tmp' % name, name)
except:
pass
if os.path.exists(lock):
sys.exit(0)
with open(lock, 'w') as f:
f.write('')
for i in range(10):
for letter in string.ascii_lowercase:
name = '%s%02d.1080p.mp4' % (letter, i)
url = '%s%s' % (base, name)
name = 'video/' + name
folder = os.path.dirname(name)
if not os.path.exists(folder):
os.makedirs(folder)
if os.path.exists(name):
head = requests.head(url)
mtime = time.mktime(datetime.datetime.strptime(head.headers.get("Last-Modified"), "%a, %d %b %Y %X GMT").timetuple())
get = mtime > os.path.getmtime(name) or int(head.headers.get("Content-Length")) != os.path.getsize(name)
else:
get = True
if get:
print(url)
try:
r = requests.get(url, stream=True)
with open('%s.tmp' % name, 'wb') as fd:
shutil.copyfileobj(r.raw, fd)
ox.avinfo('%s.tmp' % name)['duration']
shutil.move('%s.tmp' % name, name)
mtime = time.mktime(datetime.datetime.strptime(r.headers.get("Last-Modified"), "%a, %d %b %Y %X GMT").timetuple())
os.utime(name, (mtime, mtime))
ox.avinfo(name)['duration']
get_subtitle(url.replace('1080p.mp4', lang + '.srt'), name.replace('.mp4', '.srt'))
except KeyboardInterrupt:
os.unlink(lock)
sys.exit(-1)
except:
print(url, 'failed')
os.unlink(lock)