pandora_cdosea/playout/update.py

47 lines
1.3 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-03-06 08:49:33 +00:00
base = 'https://cdosea.0x2620.org/static/render'
lock = '/tmp/update.lock'
2017-03-01 09:20:55 +00:00
folder = os.path.abspath(os.path.dirname(__file__))
os.chdir(folder)
2017-03-06 08:49:33 +00:00
if os.path.exists(lock):
sys.exit(0)
with open(lock, 'w') as f: f.write('')
2017-03-01 09:20:55 +00:00
for i in range(10):
for letter in string.ascii_uppercase:
name = '%02d/%s.mp4' % (i, letter)
url = '%s/%s' % (base, 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)
else:
get = True
if get:
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-06 12:34:11 +00:00
ox.avinfo(name)['duration']
2017-03-01 09:20:55 +00:00
except:
2017-03-06 08:49:33 +00:00
print(url, 'failed')
os.unlink(lock)