-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsource_format.py
More file actions
71 lines (54 loc) · 1.65 KB
/
Copy pathsource_format.py
File metadata and controls
71 lines (54 loc) · 1.65 KB
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
from collections import OrderedDict
import yaml
FIELD_ORDER = [
"url",
"description",
"priority",
"wrappers",
"whitelist_regex",
"blacklist_regex",
"allowed_languages",
]
yaml.add_representer(
OrderedDict,
lambda dumper, data: dumper.represent_mapping(
"tag:yaml.org,2002:map", data.items()
),
)
def feed_sort_key(feed) -> str:
key = feed["url"]
key = key.replace("https://", "")
key = key.replace("http://", "")
key = key.replace("www.", "")
key = key.lower()
return key
def source_sort_key(source) -> str:
return feed_sort_key(source["feeds"][0])
# Get categories
with open("config/sources.yml", "r") as sources_file:
categories = yaml.safe_load(sources_file)
# Sort sources in each category
for category in categories:
for source in category["sources"]:
source["feeds"].sort(key=feed_sort_key)
category["sources"].sort(key=source_sort_key)
# Arrange fields within each source
for category in categories:
if category["sources"] is None:
continue
for source in category["sources"]:
for i, feed in enumerate(source["feeds"]):
new_feed = OrderedDict()
# Add fields in specified order
for key in FIELD_ORDER:
if key in feed:
new_feed[key] = feed[key]
# Add remaining fields
for key in feed:
if key not in FIELD_ORDER:
new_feed[key] = feed[key]
# Update feed
source["feeds"][i] = new_feed
# Write categories
with open("config/sources.yml", "w") as sources_file:
yaml.dump(categories, sources_file)