ppsspp/link_readme.py

104 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
2021-01-02 13:03:43 +00:00
from time import sleep
from urllib.request import urlopen
from urllib.error import HTTPError
from lxml.html import parse
2021-01-02 13:03:43 +00:00
footer_delimiter = "\n\n[comment]: # (LINK_LIST_BEGIN_HERE)\n"
footer = ""
2021-10-05 17:13:49 +00:00
linked_id = []
2021-10-05 19:07:28 +00:00
present_id = []
2021-10-05 17:13:49 +00:00
def add_bracket(match):
first_char = match.group(1)
id = match.group(2)
2021-01-02 13:03:43 +00:00
replace = first_char + "[#"+id+"]"
2021-10-05 17:13:49 +00:00
return replace
def add_link(match):
id = match.group(1)
2021-10-05 19:07:28 +00:00
present_id.append(id)
2021-10-05 17:13:49 +00:00
replace = "#" + id
if id in linked_id:
return replace
url = "https://github.com/hrydgard/ppsspp/issues/"+id
title = None
while title is None:
try:
2021-01-02 13:03:43 +00:00
p = parse(urlopen(url))
title = p.find(".//title").text.split('by')[0].split('·')[0].strip()
title = re.sub(r"\"", r'\\"', title)
except HTTPError:
print("Something went wrong, retrying in 10 sec...")
2021-01-02 13:03:43 +00:00
sleep(10)
global footer
addition = "[#"+id+"]: https://github.com/hrydgard/ppsspp/issues/"+id+" \""+title+"\""
footer += addition+"\n"
2021-10-05 17:13:49 +00:00
linked_id.append(id)
2021-10-05 18:07:49 +00:00
print("Linked: " + addition)
return replace
2021-10-05 17:13:49 +00:00
def already_added_id(match):
linked_id.append(match.group(1))
return "[#" + match.group(1) + "]:"
2021-10-05 19:07:28 +00:00
def remove_old_link(line):
# Ignore extra new lines at the end
if line.find("#") == -1:
return ""
id = line[line.find("[#")+2 : line.find("]:")]
if id in present_id:
return line + "\n"
else:
print("Removed: #" + id)
return ""
2021-01-02 13:03:43 +00:00
2021-10-05 19:07:28 +00:00
def update(file_name):
global footer
footer = ""
global linked_id
linked_id = []
global present_id
present_id = []
f = open(file_name, "r+")
cont = f.read()
# We don't want to match issues id in title so stop before the link list
d = cont.find(footer_delimiter)
if (d != -1):
footer = cont[d + len(footer_delimiter):]
cont = cont[0 : d]
re.sub(r"\[#(\d+)\]:", already_added_id, footer)
# Add brackets if missing
added_bracket = re.sub(r"([^[])#(\d+)", add_bracket, cont)
# Add links if missing
updated = re.sub(r"#(\d+)", add_link, added_bracket)
# Remove old unused link
updated_footer = ""
for line in footer.split("\n"):
updated_footer += remove_old_link(line)
2021-10-05 17:13:49 +00:00
2021-10-05 19:07:28 +00:00
# Remove extra new lines at the end
while updated_footer[-1] == "\n":
updated_footer = updated_footer[0:-1]
2021-10-05 17:13:49 +00:00
2021-10-05 19:07:28 +00:00
f.seek(0)
f.write(updated)
f.write(footer_delimiter)
f.write(updated_footer)
f.truncate()
f.close()
2021-01-02 13:03:43 +00:00
2021-10-05 19:07:28 +00:00
update("README.md")
update("history.md")