X-Git-Url: http://unremediatedgender.space/source?p=Ultimately_Untrue_Thought.git;a=blobdiff_plain;f=notes%2Fslate_starchive.py;fp=notes%2Fslate_starchive.py;h=d4f30713909cebfdb4211960c646de2bdd0c7ad7;hp=0000000000000000000000000000000000000000;hb=21731ba6f1191f1e8f920c44299c4b345f4fa528;hpb=f1129284da0a1943d1f2809292ea2f832b0d03f3 diff --git a/notes/slate_starchive.py b/notes/slate_starchive.py new file mode 100644 index 0000000..d4f3071 --- /dev/null +++ b/notes/slate_starchive.py @@ -0,0 +1,49 @@ +import os +import re +import requests +import sys + + +slate_sturl_regex = re.compile(r"https?://slatestarcodex.com/\d{4}/\d{2}/\d{2}/[-a-z0-9]+/") + + +def slate_starchive_post_content(content): + slate_star_links = slate_sturl_regex.finditer(content) + revised = content + for match in slate_star_links: + link_url = match.group() + archive_response = requests.get( + "http://archive.org/wayback/available?url={}".format(link_url) + ) + try: + archive_url = archive_response.json()['archived_snapshots']['closest']['url'] + except KeyError: + print( + "didn't successfully get an archive link for {}: {}".format( + link_url, archive_response.text + ) + ) + else: + print("replacing \033[93m{}\033[0m with \033[92m{}\033[0m".format(link_url, archive_url)) + revised = revised.replace(link_url, archive_url) + return revised + + +def tree(root): + for path, _dirs, filenames in os.walk(root): + for filename in filenames: + if not filename.endswith(".md"): + continue + filepath = os.path.join(path, filename) + with open(filepath) as f: + print("examining {}".format(filepath)) + content = f.read() + revised = slate_starchive_post_content(content) + if revised != content: + with open(filepath, 'w') as g: + print("revising {}".format(filepath)) + g.write(revised) + + +if __name__ == "__main__": + tree(sys.argv[1])