-
Notifications
You must be signed in to change notification settings - Fork 18
/
html2meta.py
executable file
·146 lines (112 loc) · 4.09 KB
/
html2meta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: UTF-8 -*-
from dataclasses import dataclass, field
from typing import List, Any
from bs4 import BeautifulSoup, Tag
import os
import sys
import json
import locale
import argparse
import requests
import pytz
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
tz = pytz.timezone("Europe/Amsterdam")
parser = argparse.ArgumentParser()
# parser.add_argument('acronym', action="store", help="Series acronym, e.g. jev22")
parser.add_argument('--url', action="store", help="Source url, e.g. https://events.ccc.de/2022/11/28/dezentral-2022/", default='https://events.ccc.de/2022/11/28/dezentral-2022/')
parser.add_argument('-o', action="store", dest="output", help="output filename, e.g. current", default='current')
args = parser.parse_args()
acronym = 'jev22' # args.acronym.lower()
@dataclass
class Location:
id: str
name: str
region: str
country: str
description: dict[str, List[Tag]] = field(default_factory=lambda: {'Deutsch': [], 'English': []})
infos: List[List[Any]] = field(default_factory=list)
doc: List[Tag] = field(default_factory=list)
def dict(self):
return {
'country': self.country,
'region': self.region,
'name': self.name,
'id': self.id,
'description': {
'de': "\n".join([x for x in self.description['Deutsch']]),
'en': "\n".join([x for x in self.description['English']]),
},
'infos': serialize_links(self.infos)
}
def __str__(self):
# return str(self.dict())
return json.dumps(self.dict(), indent=2, cls=SoupEncoder)
def serialize_links(infos):
for x in infos:
try:
t, a = x
return {"title": t.string.strip().strip(':'), "url": a['href']}
except Exception as e:
print(type(e).__name__)
return ''.join([str(e) for e in x])
def fetch_post(source_url):
print("Requesting source")
conferences = []
soup = BeautifulSoup(requests.get(source_url).text, 'html5lib')
for chapter in soup.select('div.content > h2'):
cid = chapter['id'].split('-')[3]
print("\n==", cid, chapter.string)
elements = chapter.find_next_siblings('h4')
element = next(elements)
while True:
location = Location(
id=element['id'],
name=element.string,
region=element.find_previous_sibling('h3'),
country=cid
)
print("-", location.name)
conferences.append(location)
subsection = None
for p in element.next_siblings:
if p.name != 'p':
break
if p.get('class') == ['subsection']:
subsection = p.string.strip()
continue
if subsection == 'Info':
location.infos = [list(li.children) for li in p.select('li')]
continue
if subsection and location:
location.description[subsection] += p.text.prettify()
continue
element = next(elements)
if not element or element.previous_sibling.name == 'h2':
break
return conferences
class SoupEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Location):
return obj.dict()
if isinstance(obj, Tag):
return str(obj)
return json.JSONEncoder.default(self, obj)
def main():
data = fetch_post(args.url)
with open("meta.json", "w") as fp:
json.dump(data, fp, indent=2, cls=SoupEncoder)
# schedule.export(args.output)
print('')
print('end')
if __name__ == '__main__':
output_dir = "/srv/www/" + acronym
secondary_output_dir = "./" + acronym
if len(sys.argv) == 2:
output_dir = sys.argv[1]
if not os.path.exists(output_dir):
if not os.path.exists(secondary_output_dir):
os.mkdir(output_dir)
else:
output_dir = secondary_output_dir
os.chdir(output_dir)
main()