pandora_cdosea/playout/update.py
2017-03-20 02:30:51 +01:00

48 lines
1.5 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)
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_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:
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']
except:
print(url, 'failed')
os.unlink(lock)