diff --git a/.gitignore b/.gitignore index 1da22d7..ce2ed00 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +ficheader.html prompts.org \ No newline at end of file diff --git a/README.org b/README.org index ceaf623..de4d854 100644 --- a/README.org +++ b/README.org @@ -1,6 +1,7 @@ * Python utilities for personal and fannish purposes - =ao3scrape.py= – downloads all an author’s works from AO3 (notes [[https://tobli.dreamwidth.org/45337.html][here]]) - =cellopractice.py= – selects some exercises from Walter Mengler’s /Fit in 15 Minutes/, according to the author’s recommendations +- =fic-header.py= – automates, as much as possible, the creation of a fic header for my website - =promptscrape.py= – scrapes Dreamwidth writing challenge communities for prompts - =randomline.py= – returns a random line from a file - =sponge.py= – fOrMaTs tExT LiKe tHiS diff --git a/fic-header.py b/fic-header.py new file mode 100644 index 0000000..7e32874 --- /dev/null +++ b/fic-header.py @@ -0,0 +1,165 @@ +import requests, datetime, os, re +from bs4 import BeautifulSoup + +# presumably needed for nsfw content +login_url = "https://www.archiveofourown.org/users/login" +data = { + "user": "translinkni", + "password": "notreallyanapi" +} + +if os.path.exists("ficheader.html"): + os.remove("ficheader.html") + +thefile = open("ficheader.html", "a") + +with requests.Session() as s: + response = s.post(login_url , data) + print("Work ID: ") + id = input() + url = "https://archiveofourown.org/works/" + str(id) + ficpage = s.get(url) + ficsoup = BeautifulSoup(ficpage.content, "html.parser") + rating = ficsoup.find("dd", class_="rating").text.strip() + if str(rating) == "General Audiences": + prazerating = "g" + elif str(rating) == "Teen And Up Audiences": + prazerating = "t" + elif str(rating) == "Mature": + prazerating = "m" + elif str(rating) == "Explicit": + prazerating = "e" + warningheader = ficsoup.find("dd", {"class":["warning", "warnings"]}) + rawwarnings = warningheader.find_all("li") + warnings = [] + for c in rawwarnings: + warning = c.text + warnings.append(str(warning)) + prazewarnings = [] + if "No Archive Warnings Apply" in warnings: + prazewarn = 0 + elif "Creator Chose Not To Use Archive Warnings" in warnings: + prazewarn = 1 + prazewarnings.append("miscellaneous") + else: + prazewarn = 1 + if "Major Character Death" in warnings: + prazewarnings.append("character death") + if "Underage" in warnings: + prazewarnings.append("underage") + categoryheader = ficsoup.find("dd", {"class":["category", "categories"]}) + rawcategories = categoryheader.find_all("li") + categories = [] + for c in rawcategories: + category = c.text + categories.append(str(category)) + prazegenre = [] + if "Gen" in categories: + prazegenre.append("gen") + if "M/M" in categories: + prazegenre.append("slash") + if "F/M" in categories: + prazegenre.append("het") + if "F/F" in categories: + prazegenre.append("femslash") + if "Multi" in categories: + prazegenre.append("poly") + fandomheader = ficsoup.find("dd", {"class":["fandom", "fandoms"]}) + rawfandoms = fandomheader.find_all("li") + fandoms = [] + for c in rawfandoms: + rawfandom = str(c.text) + if rawfandom == "Final Fantasy VI": + fandom = "FF6" + elif rawfandom == "Compilation of Final Fantasy VII": + fandom = "FF7" + elif rawfandom == "Crisis Core: Final Fantasy VII": + fandom = "FF7 Crisis Core" + elif rawfandom == "Final Fantasy VII (Video Game 1997)": + fandom = "FF7" + elif rawfandom == "Final Fantasy VII Remake (Video Game 2020)": + fandom = "FF7R" + elif rawfandom == "Final Fantasy X": + fandom = "FFX" + elif rawfandom == "Final Fantasy X Series": + fandom = "FFX" + elif rawfandom == "Final Fantasy X-2": + fandom = "FFX-2" + elif rawfandom == "Final Fantasy IX": + fandom = "FF9" + else: + fandom = rawfandom + fandoms.append(fandom) + characterheader = ficsoup.find("dd", {"class":["character", "characters"]}) + rawcharacters = characterheader.find_all("li") + characters = [] + for c in rawcharacters: + rawcharacter = str(c.text) + cleancharacter = re.sub(r" \(.*\)","",rawcharacter) + if cleancharacter == "Locke Cole": + character = "Locke" + elif cleancharacter == "Macías \"Mash\" Rene Figaro | Sabin Rene Figaro": + character = "Sabin" + elif cleancharacter == "Celes Chere": + character = "Celes" + elif cleancharacter == "Edgar Roni Figaro": + character = "Edgar" + elif cleancharacter == "Tina Branford | Terra Branford": + character = "Terra" + elif cleancharacter == "Original Characters": + character = "OCs" + elif cleancharacter == "Original Male Character": + character = "OMC" + elif cleancharacter == "Original Female Character": + character = "OFC" + else: + character = cleancharacter + characters.append(str(character)) + date = ficsoup.find("dd", class_="published").text + prazedate = datetime.datetime.strptime(str(date), "%Y-%m-%d").strftime("%-d %B %Y") + words = ficsoup.find("dd", class_="words").text + title = ficsoup.find("h2", class_="title").text.strip() + summaryheader = ficsoup.find("div", class_="summary") + summary = summaryheader.find("blockquote") + notesheader = ficsoup.find("div", class_="notes") + try: + rawnotes = str(notesheader.find("blockquote")) + except: + rawnotes = 0 + if rawnotes: + notesa = re.sub(r" rel=\"nofollow\"","",rawnotes) + notesb = re.sub(r"alt=\"\[community profile\]\"","alt=\"[community profile]\" style=\"vertical-align: text-bottom; border: 0; padding-right: 1px;\"",notesa) + notesc = re.sub(r"alt=\"\[personal profile\]\"","alt=\"[personal profile]\" style=\"vertical-align: text-bottom; border: 0; padding-right: 1px;\"",notesb) + notesd = re.sub(r"

\nStandard note for 3sf fills: I’m aware that some AO3 users may not consider prompt fills “gifts”, and will take no offence if the gift is refused!","",notesc) + notes = re.sub(r"\n

","

",notesd) + else: + notes = 0 + masterlist = s.get("https://tre.praze.net/fic/master.html") + mastersoup = BeautifulSoup(masterlist.content, "html.parser") + ficno = int(mastersoup.find("span", class_="ficno").text.strip()) + 1 + thefile.write("
\n") + thefile.write("

" + str(ficno) + " " + title + "

\n") + thefile.write(" \n") + thefile.write("

" + str(summary)[34:-18] + "

\n") + if notes: + thefile.write("

" + str(notes)[34:-18] + "

\n") + thefile.write(" \n") + thefile.write("
\n") + print("File saved to " + str(os.path.abspath("ficheader.html")))