/
opml_to_gohugo.py
46 lines (36 loc) · 1.07 KB
/
opml_to_gohugo.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
from pprint import pprint
from bs4 import BeautifulSoup
import json
# Reading the data inside the xml
# file to a variable under the name
# data
with open('my-feeds.opml', 'r') as f:
data = f.read()
# Passing the stored data inside
# the beautifulsoup parser, storing
# the returned object
Bs_data = BeautifulSoup(data, "xml")
# Finding all instances of tag
# `unique`
b_unique = Bs_data.find_all('outline')
out = {}
title = ""
for item in b_unique:
if item.attrs.get("xmlUrl", False) is False:
title = item.attrs.get("title")
out[title] = []
else:
out[title].append({"title": item.attrs.get("title"), "url": item.attrs.get("xmlUrl")})
data = []
for key in out.keys():
if len(out[key]) > 0:
data.append({"elements": out[key], "type": key})
with open("data/raw_hoarding.json", "r") as outfile:
data_other = json.load(outfile)
print(len(data_other))
data += data_other
print(len(data))
json_object = json.dumps(data, indent=4)
# Writing to sample.json
with open("data/feed.json", "w") as outfile:
outfile.write(json_object)