forked from wbond/package_control_channel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect301.py
59 lines (47 loc) · 1.82 KB
/
redirect301.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
#!/usr/bin/env python3
# Python 3.4+
import datetime
import json
import logging
from pathlib import Path
from time import sleep
import sys
import requests
base_dir = Path(__file__).parent.parent
log_path = base_dir / "redirect301_{:%Y-%M-%d_%H-%m-%S}.log".format(datetime.datetime.now())
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(message)s")
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(level=logging.DEBUG)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
file_handler = logging.FileHandler(log_path)
file_handler.setLevel(level=logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
for path in sorted(Path(base_dir, "repository").glob("*.json")):
if path.name == "dependencies.json":
continue
logger.debug("Processing '{!s}'".format(path))
with path.open('r', encoding="utf-8") as f:
packages_text = f.read()
packages = json.loads(packages_text)["packages"]
for package in packages:
link = package["details"]
try:
r = requests.head(link, allow_redirects=False)
if r.status_code == 301:
new_link = requests.head(link, allow_redirects=True).url
if link == new_link:
logger.warning("Redirected to same URL: {}".format(link))
else:
logger.info("Found 301: \"{}\" -> \"{}\"".format(link, new_link))
packages_text = packages_text.replace(link, new_link)
else:
logger.debug("No change for \"{}\"".format(link))
except Exception:
logger.exception("Exception on {!r}".format(link))
sleep(0.1)
with path.open('w', encoding="utf-8") as f:
f.write(packages_text)