python-ox/ox/web/arsenalberlin.py

73 lines
2.3 KiB
Python
Raw Normal View History

2013-06-06 09:20:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-30 19:27:26 +00:00
from __future__ import print_function
2013-06-06 09:20:43 +00:00
import json
import os
import re
2014-09-30 19:04:46 +00:00
from ox import find_re, strip_tags
2013-06-06 09:20:43 +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 = {}
2023-07-27 16:12:13 +00:00
data['id'] = id
data['url'] = url
2013-06-06 09:20:43 +00:00
m = re.compile('<h1>(.*?)</h1>').findall(html)
if m:
2023-07-27 16:12:13 +00:00
data['title'] = m[0]
2013-06-06 09:20:43 +00:00
m = re.compile("<b>Director: </b><a href='.*?'>(.*?)</a>").findall(html)
if m:
2023-07-27 16:12:13 +00:00
data['director'] = m[0]
2013-06-06 09:20:43 +00:00
2024-08-30 11:30:47 +00:00
m = re.compile(r"caUI.initImageScroller\(\[\{url:'(.*?)'").findall(html)
2013-06-06 09:20:43 +00:00
if m:
2023-07-27 16:12:13 +00:00
data['image'] = m[0]
2013-06-06 09:20:43 +00:00
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:
2023-07-27 16:12:13 +00:00
data['runtime'] = float(data.pop("running time (minutes)").replace(',', '.')) * 60
2013-06-06 09:20:43 +00:00
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 19:04:46 +00:00
start = max(map(int, data)) or 1
2013-06-06 09:20:43 +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 19:27:26 +00:00
print('save', filename, len(data))
2013-06-06 09:20:43 +00:00
with open(filename, 'w') as f:
json.dump(data, f)
else:
2014-09-30 19:27:26 +00:00
print('ignore', i)
2013-06-06 09:20:43 +00:00
with open(filename, 'w') as f:
json.dump(data, f)
return data