openmedialibrary_platform/Shared/lib/python3.7/site-packages/ox/web/arsenalberlin.py

73 lines
2.3 KiB
Python
Raw Permalink Normal View History

2013-10-11 17:28:32 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-30 20:25:10 +00:00
from __future__ import print_function
2013-10-11 17:28:32 +00:00
import json
import os
import re
2014-09-30 16:15:32 +00:00
from ox import find_re, strip_tags
2013-10-11 17:28:32 +00:00
from ox.cache import read_url
def get_data(id, language='en'):
if language == 'de':
url = 'http://films.arsenal-berlin.de/index.php/Detail/Object/Show/object_id/%d/lang/de_DE' % id
else:
url = 'http://films.arsenal-berlin.de/index.php/Detail/Object/Show/object_id/%d' % id
html = read_url(url, unicode=True)
if 'ID does not exist' in html:
return None
if 'Willkommen in der Datenbank des Arsenal' in html:
return None
data = {}
data[u'id'] = id
data[u'url'] = url
m = re.compile('<h1>(.*?)</h1>').findall(html)
if m:
data[u'title'] = m[0]
m = re.compile("<b>Director: </b><a href='.*?'>(.*?)</a>").findall(html)
if m:
data[u'director'] = m[0]
m = re.compile("caUI.initImageScroller\(\[\{url:'(.*?)'").findall(html)
if m:
data[u'image'] = m[0]
units = re.compile("<div class='unit'>(.*?)</div>", re.DOTALL).findall(html)
for x in map(re.compile('<b>(.*?)</b>: (.*)', re.DOTALL).findall, units):
if x:
#data[x[0][0].lower()] = strip_tags(x[0][1])
key = x[0][0].lower()
data[key] = x[0][1]
if key == "forum catalogue pdf":
data[key] = find_re(data[key], '"(http:.*?)"')
else:
data[key] = strip_tags(data[key])
if "running time (minutes)" in data:
data[u'runtime'] = float(data.pop("running time (minutes)").replace(',', '.')) * 60
for key in ('year', 'length in metres', 'forum participation year', 'number of reels'):
if key in data and data[key].isdigit():
data[key] = int(data[key])
return data
def backup(filename):
if os.path.exists(filename):
with open(filename) as f:
data = json.load(f)
else:
data = {}
2014-09-30 16:15:32 +00:00
start = max(map(int, data)) or 1
2013-10-11 17:28:32 +00:00
for i in range(start, 11872):
info = get_data(i)
if info:
data[i] = info
if len(data) % 10 == 0:
2014-09-30 20:25:10 +00:00
print('save', filename, len(data))
2013-10-11 17:28:32 +00:00
with open(filename, 'w') as f:
json.dump(data, f)
else:
2014-09-30 20:25:10 +00:00
print('ignore', i)
2013-10-11 17:28:32 +00:00
with open(filename, 'w') as f:
json.dump(data, f)
return data