Skip to content

Commit ff12687

Browse files
committed
Create build_url_map.py
1 parent 8215459 commit ff12687

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

scripts/build_url_map.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
"""Build a map of url => id from all completed post JSON files."""
3+
4+
import json
5+
import glob
6+
import os
7+
8+
def build_url_map():
9+
completed_dir = os.path.join(os.path.dirname(__file__), 'completed')
10+
url_map = {}
11+
12+
for filepath in sorted(glob.glob(os.path.join(completed_dir, '*.json'))):
13+
with open(filepath) as f:
14+
data = json.load(f)
15+
url = data.get('url')
16+
post_id = data.get('id')
17+
if url and post_id:
18+
url_map[url] = post_id
19+
20+
return url_map
21+
22+
if __name__ == '__main__':
23+
url_map = build_url_map()
24+
# print(json.dumps(url_map, indent=2))
25+
print(f"\nTotal: {len(url_map)} entries", file=__import__('sys').stderr)
26+
27+
for url, post_id in url_map.items():
28+
# if post_id is not a number
29+
if not isinstance(post_id, int):
30+
print(f"UPDATE post set ref_id = '{post_id}', ref_urn = 'urn:reddit:post:{post_id}' where url = '{url}';")

0 commit comments

Comments
 (0)