-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.py
executable file
·52 lines (42 loc) · 1.63 KB
/
redirect.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
import json
import re
import os
import sys
home_dir = os.path.expanduser("~")
def load_json(filename):
with open(filename, 'r') as file:
return json.load(file)
def save_json(filename, data):
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
def find_result_files():
pattern = re.compile(r'text_[^.]*\.json')
return [file for file in os.listdir(f'{home_dir}/surchin/') if pattern.match(file)]
def process_url(url, rules):
for rule in rules:
if not rule["disabled"]:
pattern = re.sub(r'\*', '(.*)', rule["includePattern"])
match = re.match(pattern, url)
if match:
new_url = re.sub(r'\$1', match.group(1), rule["redirectUrl"])
return new_url
return url
def generate_html(input_data):
html_content = '<html><head><title>Entries</title></head><body><ol>'
for idx, entry in enumerate(input_data, start=1):
html_content += f'<li><a href="{entry["href"]}">{entry["title"]}</a><p>{entry["body"]}</p>'
html_content += f'<div class ="linky"><p>{entry["href"]}</p></div></li>'
html_content += '</ol></body></html>'
with open('output.html', 'w') as file:
file.write(html_content)
def main():
redirector_rules = load_json(os.path.join(home_dir, 'surchin', 'Redirector.json'))
json_files = find_result_files()
for filename in json_files:
input_data = load_json(filename)
for item in input_data:
item["href"] = process_url(item["href"], redirector_rules)
save_json(filename, input_data)
generate_html(input_data)
if __name__ == "__main__":
main()