pandora_cdosea/playout/update.py

65 lines
2 KiB
Python
Raw Normal View History

2017-03-01 09:20:55 +00:00
#!/usr/bin/python3
import os
import requests
import string
import time
import datetime
import shutil
2017-03-06 08:49:33 +00:00
import sys
import socket
import ox
2017-03-01 09:20:55 +00:00
2017-03-06 08:49:33 +00:00
socket.setdefaulttimeout(10)
2017-03-01 09:20:55 +00:00
2017-05-20 16:30:31 +00:00
base = 'https://cdosea.0x2620.org/static/render/'
2017-03-06 08:49:33 +00:00
lock = '/tmp/update.lock'
2017-03-01 09:20:55 +00:00
folder = os.path.abspath(os.path.dirname(__file__))
os.chdir(folder)
2017-05-20 16:30:31 +00:00
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
2017-03-06 08:49:33 +00:00
if os.path.exists(lock):
sys.exit(0)
2017-05-15 17:03:28 +00:00
with open(lock, 'w') as f:
f.write('')
2017-03-06 08:49:33 +00:00
2017-03-01 09:20:55 +00:00
for i in range(10):
2017-05-15 17:03:28 +00:00
for letter in string.ascii_lowercase:
name = '%s%02d.1080p.mp4' % (letter, i)
2017-05-20 16:30:31 +00:00
url = '%s%s' % (base, name)
2017-05-15 17:03:28 +00:00
name = 'video/' + name
2017-03-01 09:20:55 +00:00
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())
2017-05-15 17:03:28 +00:00
get = mtime > os.path.getmtime(name) or int(head.headers.get("Content-Length")) != os.path.getsize(name)
2017-03-01 09:20:55 +00:00
else:
get = True
if get:
2017-05-20 16:30:31 +00:00
print(url)
2017-03-01 09:20:55 +00:00
try:
2017-03-06 08:49:33 +00:00
r = requests.get(url, stream=True)
2017-03-01 09:20:55 +00:00
with open('%s.tmp' % name, 'wb') as fd:
shutil.copyfileobj(r.raw, fd)
2017-03-06 08:49:33 +00:00
ox.avinfo('%s.tmp' % name)['duration']
2017-03-01 09:20:55 +00:00
shutil.move('%s.tmp' % name, name)
2017-03-20 01:30:51 +00:00
mtime = time.mktime(datetime.datetime.strptime(r.headers.get("Last-Modified"), "%a, %d %b %Y %X GMT").timetuple())
os.utime(name, (mtime, mtime))
2017-03-06 12:34:11 +00:00
ox.avinfo(name)['duration']
2017-05-20 16:30:31 +00:00
get_subtitle(url.replace('.mp4', '.srt'), name.replace('.mp4', '.srt'))
except KeyboardInterrupt:
os.unlink(lock)
sys.exit(-1)
2017-03-01 09:20:55 +00:00
except:
2017-03-06 08:49:33 +00:00
print(url, 'failed')
os.unlink(lock)