python-ox/ox/web/archive.py

32 lines
1 KiB
Python
Raw Normal View History

2012-01-13 13:52:55 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2012-08-14 13:30:52 +00:00
from .. import cache
from ..utils import json
2012-01-13 13:52:55 +00:00
2012-08-15 15:15:40 +00:00
def get_id(url):
2012-01-13 13:52:55 +00:00
return url.split("/")[-1]
2012-08-15 15:15:40 +00:00
def get_url(id):
2012-01-13 13:52:55 +00:00
return "http://www.archive.org/details/%s" % id
2012-08-15 15:15:40 +00:00
def get_data(id):
2012-08-14 13:30:52 +00:00
data = {}
2012-08-15 15:15:40 +00:00
url = get_url(id)
details = cache.read_url('%s?output=json' % url)
2012-08-14 13:30:52 +00:00
details = json.loads(details)
for key in ('title', 'description', 'runtime'):
2015-03-14 19:37:34 +00:00
if key in details['metadata']:
data[key] = details['metadata'][key]
if isinstance(data[key], list):
data[key] = data[key][0]
2023-07-27 11:07:13 +00:00
if isinstance(data[key], str):
2015-03-14 19:37:34 +00:00
data[key] = data[key].strip()
if data[key][0] == '[' and data[key][-1] == ']':
data[key] = data[key][1:-1]
2012-08-14 13:30:52 +00:00
data['url'] = url
data['image'] = 'http://archive.org/download/%s/format=thumbnail' % id
data['ogg'] = 'http://archive.org/download/%s/format=Ogg+video' % id
data['mp4'] = 'http://archive.org/download/%s/format=512Kb+MPEG4' % id
return data