records/generaterecords.py
2025-05-04 17:23:02 +01:00

72 lines
7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import discogs_client,re,time,thetoken
d = discogs_client.Client("Python/1.0",user_token=thetoken.discogstoken)
user = d.user("pilpaotr")
records = []
thenumber = 1
maxno = len(user.collection_folders[0].releases)
def typography(thestring):
return re.sub(" / ","/",re.sub(" \(.*\)","",re.sub(" - "," ",re.sub(" "," ",re.sub("\.\.\.","",re.sub("'","",re.sub(" = .*","",thestring)))))))
print("Fetching records from Discogs")
for item in user.collection_folders[0].releases:
print("Fetching " + str(thenumber) + " of " + str(maxno))
theitem = {}
theitem["artist"] = typography(item.release.artists[0].name)
theitem["title"] = typography(item.release.title)
try:
if "LP" in item.release.formats[0]["descriptions"] or "12\"" in item.release.formats[0]["descriptions"]:
theitem["format"] = "large"
else:
theitem["format"] = "small"
except:
theitem["format"] = "small"
try:
theitem["img"] = item.release.images[0]["uri"]
except:
pass
records.append(theitem)
thenumber += 1
time.sleep(1) # only allowed 1 request per second
records = sorted(records, key=lambda d: d["title"])
records = sorted(records, key=lambda d: d["artist"])
smalls = []
larges = []
for record in records:
if record["format"] == "large":
larges.append(record)
else:
smalls.append(record)
def recordwrite(record):
recordstring = " <div class=\"container " + record["format"] + "\">\n <img src=\""
try:
recordstring += record["img"]
except:
if record["format"] == "large":
recordstring += "/trackers/records/large.png"
elif record["format"] == "small":
recordstring += "/trackers/records/small.png"
recordstring += "\">\n <div>\n <p>" + record["artist"] + "<br>•<br>" + record["title"] + "</p>\n </div>\n </div>\n"
return recordstring
writefile = open("build/index.html","w")
writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>praze • Records</title>\n <meta property=\"og:title\" content=\"praze • Records\">\n <meta property=\"og:type\" content=\"website\">\n <meta property=\"og:image\" content=\"https://tre.praze.net/ab.png\">\n <meta property=\"og:url\" content=\"https://tre.praze.net/trackers/records\">\n <meta name=\"description\" property=\"og:description\" content=\"Record collection\">\n <meta property=\"og:locale\" content=\"en_GB\">\n <meta property=\"og:site_name\" content=\"tre.praze.net\">\n <meta name=\"fediverse:creator\" content=\"@tre@praze.net\">\n <link rel=\"stylesheet\" href=\"/new.css\">\n <link rel=\"me\" href=\"https://kes.praze.net/@tre\">\n <link rel=\"alternate\" type=\"application/rss+xml\" title=\"tre.praze.net\" href=\"/feed.xml\">\n <meta name=\"theme-color\" content=\"#f2f2f2\" />\n <style>\n section#records {\n display: flex;\n gap: 10px;\n flex-wrap: wrap;\n justify-content: space-evenly;\n }\n .container {\n position: relative;\n display: flex;\n justify-content: center;\n }\n .container.small {\n width: 180px;\n min-width: 180px;\n }\n .container.large {\n width: 300px;\n min-width: 300px;\n }\n .container img {\n display: block;\n width: 100%;\n height: auto;\n margin: auto;\n }\n .container div {\n position: absolute;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n height: 100%;\n width: 100%;\n opacity: 0;\n transition: .5s ease;\n background-color: rgba(0,0,0,0.5);\n overflow: auto;\n }\n .container:hover div {\n opacity: 1;\n }\n .container div p {\n color: white;\n width: 100%;\n margin: 0;\n position: absolute;\n top: 50%;\n left: 50%;\n -webkit-transform: translate(-50%, -50%);\n -ms-transform: translate(-50%, -50%);\n transform: translate(-50%, -50%);\n text-align: center;\n text-transform: lowercase;\n }\n </style>\n </head>\n <body>\n<nav>\n <input type=\"checkbox\" id=\"toggle\" name=\"toggle\">\n <label class=\"toggle-btn\" for=\"toggle\">Menu</label>\n <ul>\n <li>Fundamentals\n <ul>\n <li><a href=\"/\">Home</a></li>\n <li><a href=\"/about\">About + listings</a></li>\n <li><a href=\"/follow\">Follow</a></li>\n <li><a href=\"/feed.xml\">Changelog</a> <a href=\"/feed.xml\"><span class=\"rss\"></span></a></li>\n <li><a href=\"/sitemap.xml\">Sitemap</a></li>\n <li><a href=\"/siteroll\">Siteroll</a></li>\n </ul>\n </li>\n <li>Projects\n <ul>\n <li><a href=\"/fic\">Fanfiction</a> <a href=\"/fic/feed.xml\"><span class=\"rss\"></span></a></li>\n <li><a href=\"/music\">Music</a></li>\n <li><a href=\"/notes\">Journal</a></li>\n <li><a href=\"/trackers\">Trackers</a></li>\n <li><a href=\"https://git.praze.net/tre\" target=\"_blank\">Code</a> <a href=\"https://git.praze.net/tre.rss\"><span class=\"rss\"></span></a></li>\n <li><a href=\"https://img.praze.net\" target=\"_blank\">Photos</a> <a href=\"https://img.praze.net/feed.php\"><span class=\"rss\"></span></a></li>\n </ul>\n </li>\n <li>Fan content\n <ul>\n <li><a href=\"/ffx\">FFX</a></li>\n <li><a href=\"/xvi\">FF16</a></li>\n <li><a href=\"https://morgan.praze.net\" target=\"_blank\">Dermot Morgan</a> <a href=\"https://morgan.praze.net/feed.xml\"><span class=\"rss\"></span></a></li>\n </ul>\n </li>\n <li>Misc.\n <ul>\n <li><a href=\"https://tcg.praze.net\" target=\"_blank\">TCG</a></li>\n <li><a href=\"https://links.praze.net\" target=\"_blank\">Bookmarks</a> <a href=\"https://links.praze.net/feed/rss\"><span class=\"rss\"></span></a></li>\n <li><a href=\"https://kes.praze.net/@tre\" target=\"_blank\">Fediverse</a> <a href=\"https://kes.praze.net/@tre/feed.rss\"><span class=\"rss\"></span></a></li>\n </ul>\n </li>\n </ul>\n </nav>\n <div>\n <main>\n <section><h1>Record collection</h1>\n</section>\n<section id=\"records\">")
for record in smalls:
writefile.write(recordwrite(record))
for record in larges:
writefile.write(recordwrite(record))
writefile.write(" </section>\n<section>\n <p>Generic disc icons by <a href=\"https://www.flaticon.com/free-icon/cd_5344201\">popo2021</a> and <a href=\"https://www.flaticon.com/free-icon/disc_5613845\">Dinosoft Labs</a> on Flaticon.<p>\n</section>\n <footer>\n <ul>\n <li><a href=\"/\">home</a></li>\n </ul>\n </footer>\n </div>\n <a href=\"/\"><img src=\"/a.png\" style=\"position:fixed;bottom:2px;right:2px;\" title=\"home\"></a></body>\n</html>\n")
writefile.close()