import datetime, os, shutil
from importlib import import_module
"""
Warning: code in this file is /particularly/ bizarre and non-optimised.
"""
"""
Write the wee blurb for each fic
"""
def ficsum(ficcount,year,month=0,showfandom=True,local=False):
if ficcount < 10:
ficcountstring = "00" + str(ficcount)
elif ficcount < 100:
ficcountstring = "0" + str(ficcount)
else:
ficcountstring = str(ficcount)
if os.path.exists("originalsmeta/" + ficcountstring + ".py"):
ficfile = "originalsmeta." + ficcountstring
fileread = import_module(ficfile)
elif os.path.exists("translationsmeta/" + ficcountstring + ".py"):
ficfile = "translationsmeta." + ficcountstring
fileread = import_module(ficfile)
else:
fileread = False
if fileread:
try:
theorig = "originalsmeta." + str(fileread.original)
origfile = import_module(theorig)
except:
pass
try:
fandom = "/".join(origfile.fandom)
except:
fandom = "/".join(fileread.fandom)
thechars = ""
try:
if "OW" not in origfile.fandom:
if "gen" not in origfile.genre:
try:
thechars = origfile.ship[0]
except:
chars = []
try:
chars.extend(origfile.charpov)
except:
pass
try:
chars.extend(origfile.charmain)
except:
pass
if chars != []:
thechars = ", ".join(chars)
else:
thechars = ""
else:
chars = []
try:
chars.extend(origfile.charpov)
except:
pass
try:
chars.extend(origfile.charmain)
except:
pass
if chars != []:
thechars = ", ".join(chars)
else:
thechars = ""
else:
thechars = ""
except:
if "OW" not in fileread.fandom:
if "gen" not in fileread.genre:
try:
thechars = fileread.ship[0]
except:
chars = []
try:
chars.extend(fileread.charpov)
except:
pass
try:
chars.extend(fileread.charmain)
except:
pass
if chars != []:
thechars = ", ".join(chars)
else:
thechars = ""
else:
chars = []
try:
chars.extend(fileread.charpov)
except:
pass
try:
chars.extend(fileread.charmain)
except:
pass
if chars != []:
thechars = ", ".join(chars)
else:
thechars = ""
else:
thechars = ""
if thechars == "":
thechars = "no characters specified"
if fileread.language == "fr":
language = "French"
else:
language = ""
try:
rating = origfile.rating
except:
rating = fileread.rating
genre = []
try:
for thegenre in origfile.genre:
if thegenre == "gen":
genre.append(thegenre)
elif thegenre == "het" or thegenre == "pre-het":
genre.append("het")
elif thegenre == "slash" or thegenre == "pre-slash" or thegenre == "poly slash":
genre.append("slash")
elif thegenre == "femslash":
genre.append("femslash")
elif thegenre == "poly":
genre.append("multi")
elif thegenre == "masturbation":
genre.append("other")
except:
for thegenre in fileread.genre:
if thegenre == "gen":
genre.append(thegenre)
elif thegenre == "het" or thegenre == "pre-het":
genre.append("het")
elif thegenre == "slash" or thegenre == "pre-slash" or thegenre == "poly slash":
genre.append("slash")
elif thegenre == "femslash":
genre.append("femslash")
elif thegenre == "poly":
genre.append("multi")
elif thegenre == "masturbation":
genre.append("other")
try:
if origfile.warnings:
warnings = "?!"
except:
try:
if fileread.warnings:
warnings = "?!"
except:
warnings = ""
words = 0
for datewords in fileread.datewords:
if month:
if int((datewords["date"]).year) == year:
if int((datewords["date"]).month) == month:
words += datewords["words"]
else:
if int((datewords["date"]).year) == year:
words += datewords["words"]
ficstring = ""
ficstring += ""
if showfandom:
ficstring += fandom
if thechars or language:
ficstring += ", "
if thechars:
ficstring += thechars
if language:
ficstring += ", "
if language:
ficstring += language
ficstring += ""
if rating == "g":
ficstring += " U "
elif rating == "t":
ficstring += " G "
elif rating == "m":
ficstring += " M "
elif rating == "e":
ficstring += " X "
for thegenre in genre:
if thegenre == "gen":
ficstring += " ☉ "
elif thegenre == "slash":
ficstring += " ♂ "
elif thegenre == "het":
ficstring += " ⚤ "
elif thegenre == "femslash":
ficstring += " ♀ "
elif thegenre == "poly":
ficstring += " "
elif thegenre == "other":
ficstring += " ☿ "
if warnings:
ficstring += " !? "
ficstring += " " + str(words) + "
"
return ficstring
"""
Generate stats page for each year
"""
def yeargen(local=False):
yearlist = []
ficcount = 500
while ficcount > 0:
ficcount -= 1
if ficcount < 10:
ficcountstring = "00" + str(ficcount)
elif ficcount < 100:
ficcountstring = "0" + str(ficcount)
else:
ficcountstring = str(ficcount)
if os.path.exists("originalsmeta/" + ficcountstring + ".py"):
ficfile = "originalsmeta." + ficcountstring
fileread = import_module(ficfile)
elif os.path.exists("translationsmeta/" + ficcountstring + ".py"):
ficfile = "translationsmeta." + ficcountstring
fileread = import_module(ficfile)
else:
fileread = False
if fileread:
try:
if fileread.revealdate > datetime.datetime.now():
revealed = False
else:
revealed = True
except:
revealed = True
if revealed == True:
for date in fileread.datewords:
yearlist.append(date["date"].year)
yearlist = sorted(list(dict.fromkeys(yearlist)))
for year in yearlist:
yearpath = "build/stats/" + str(year)
if not os.path.isdir(yearpath):
os.mkdir(yearpath)
if os.path.exists(yearpath + "/index.html"):
os.remove(yearpath + "/index.html")
filewrite = open(yearpath + "/index.html", "a")
filewrite.write("\n\n
Total: " + str(len (allfics)) + " fic, " + str(totalwords) + " words
\n") else: filewrite.write("Total: " + str(len (allfics)) + " fics, " + str(totalwords) + " words
") prompts = [] for fic in ficdeets: if fic["event"] == "prompt": prompts.append(fic) if prompts: promptwords = 0 for fic in prompts: promptwords += fic["words"] promptdict = {"type":"Prompt fills","fics":len(prompts),"words":promptwords,"list":prompts} else: promptdict = False unprompted = [] for fic in ficdeets: if fic["event"] == False: unprompted.append(fic) if unprompted: unpromptedwords = 0 for fic in unprompted: unpromptedwords += fic["words"] unprompteddict = {"type":"Unprompted","fics":len(unprompted),"words":unpromptedwords,"list":unprompted} else: unprompteddict = False challenges = [] for fic in ficdeets: if fic["event"] == "challenge": challenges.append(fic) if challenges: challengewords = 0 for fic in challenges: challengewords += fic["words"] challengedict = {"type":"Challenges","fics":len(challenges),"words":challengewords,"list":challenges} else: challengedict = False exchanges = [] for fic in ficdeets: if fic["event"] == "exchange" or fic["event"] == "ao3exchange": exchanges.append(fic) if exchanges: exchangewords = 0 for fic in exchanges: exchangewords += fic["words"] exchangedict = {"type":"Exchanges","fics":len(exchanges),"words":exchangewords,"list":exchanges} else: exchangedict = False byevent = [] if promptdict: byevent.append(promptdict) if unprompteddict: byevent.append(unprompteddict) if challengedict: byevent.append(challengedict) if exchangedict: byevent.append(exchangedict) byevent = sorted(byevent,key=lambda d: d["words"],reverse=True) filewrite.write("Type | \nWords | \n
---|---|
" + event["type"] + " | \n" + str(event["words"]) + "" + str(event["fics"]) + " fic") if event["fics"] > 1: filewrite.write("s") filewrite.write(", " + str(event["words"]) + " words | \n
Event | \nWords | \n
---|---|
") if event["eventlocation"] == "dwcomm": filewrite.write("" + (event["eventname"]).replace("-","_") + "") elif event["eventlocation"] == "dwjournal": filewrite.write("" + (event["eventname"]).replace("-","_")+ "") elif event["eventlocation"] == "ljjournal": filewrite.write("" + (event["eventname"]).replace("-","_")+ "") else: filewrite.write(event["eventname"]) filewrite.write(" | \n" + str(event["words"]) + "" + str(len(event["ficno"])) + " fic") if len(event["ficno"]) > 1: filewrite.write("s") filewrite.write(", " + str(event["words"]) + " words | \n
Fandom | \nWords | \n
---|---|
" + fandom["name"] + " | \n" + str(fandom["words"]) + "" + str(len(fandom["ficno"])) + " fic") if len(fandom["ficno"]) > 1: filewrite.write("s") filewrite.write(", " + str(fandom["words"]) + " words | \n
Month | \nWords | \n
---|---|
Jan | \n" + str(jan["words"]) + "" + str(len(jan["fics"])) + " fic") if len(jan["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(jan["words"]) + " words | \n
Feb | \n" + str(feb["words"]) + "" + str(len(feb["fics"])) + " fic") if len(feb["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(feb["words"]) + " words | \n
Mar | \n" + str(mar["words"]) + "" + str(len(mar["fics"])) + " fic") if len(mar["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(mar["words"]) + " words | \n
Apr | \n" + str(apr["words"]) + "" + str(len(apr["fics"])) + " fic") if len(apr["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(apr["words"]) + " words | \n
May | \n" + str(may["words"]) + "" + str(len(may["fics"])) + " fic") if len(may["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(may["words"]) + " words | \n
Jun | \n" + str(jun["words"]) + "" + str(len(jun["fics"])) + " fic") if len(jun["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(jun["words"]) + " words | \n
Jul | \n" + str(jul["words"]) + "" + str(len(jul["fics"])) + " fic") if len(jul["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(jul["words"]) + " words | \n
Aug | \n" + str(aug["words"]) + "" + str(len(aug["fics"])) + " fic") if len(aug["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(aug["words"]) + " words | \n
Sep | \n" + str(sep["words"]) + "" + str(len(sep["fics"])) + " fic") if len(sep["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(sep["words"]) + " words | \n
Oct | \n" + str(octo["words"]) + "" + str(len(octo["fics"])) + " fic") if len(octo["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(octo["words"]) + " words | \n
Nov | \n" + str(nov["words"]) + "" + str(len(nov["fics"])) + " fic") if len(nov["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(nov["words"]) + " words | \n
Dec | \n" + str(dec["words"]) + "" + str(len(dec["fics"])) + " fic") if len(dec["fics"]) != 1: filewrite.write("s") filewrite.write(", " + str(dec["words"]) + " words | \n
") listposition = yearlist.index(year) if year != 1998: prevyear = yearlist[listposition - 1] filewrite.write("« " + str(prevyear) + "") thisyear = int(datetime.datetime.now().strftime("%Y")) if year < thisyear: try: nextyear = yearlist[listposition + 1] filewrite.write("" + str(nextyear) + " »") except: pass filewrite.write("
\n