Add script for displaying record collection

master
trémeur 2 weeks ago
parent 2886ca7192
commit 94ce340e8f

1
.gitignore vendored

@ -5,4 +5,5 @@ gamebuild/*
!gamebuild/*.png !gamebuild/*.png
placebuild/* placebuild/*
!placebuild/.gitkeep !placebuild/.gitkeep
recordbuild/index.html
variables.py variables.py

@ -1,17 +1,21 @@
#+TITLE: Trackers #+TITLE: Trackers
* About * About
The scripts in this repository take information from a structured set of =.org= files and use them to build static webpages that track various activities (so far, gaming and travelling). These pages can be used as a replacement for third-party services e.g. Backloggery. The scripts in this repository build static webpages based on information from two sources:
- a structured set of =.org= files;
- APIs of selected third-party websites (so far: Discogs).
These pages can be used as a replacement for third-party services e.g. Backloggery.
* Dependencies * Dependencies
- =geopy= if you are tracking places visited (install via pip)
- =orgparse= (install via pip) - =orgparse= (install via pip)
- =python3= - =python3=
- =rclone= (for syncing to the server) - =rclone= (for syncing to the server)
- =geopy= if you are tracking places visited (install via pip)
- =python3-discogs-client= if you are displaying a record collection (install via pip)
* General setup * General setup
** Structure of the .org files ** Structure of the .org files
See =demo.org= for an example. See =demo.org= for an example.
** Setting the variables ** Setting the variables
Rename =variables-template.py= to =variables.py= and edit: Copy =variables-template.py= to =variables.py= and edit:
- =orgpath=: absolute path to the root directory in which =.org= files are stored, including trailing slash - =orgpath=: absolute path to the root directory in which =.org= files are stored, including trailing slash
- =localpath=: absolute path to this directory, including trailing slash - =localpath=: absolute path to this directory, including trailing slash
- =domain=: =https://yourdomain.tld= without trailing slash - =domain=: =https://yourdomain.tld= without trailing slash
@ -43,6 +47,19 @@ In =variables.py=, edit:
** Testing ** Testing
- Run =python3 generateplaces.py local= to build in =placebuild=. (This may take some time.) - Run =python3 generateplaces.py local= to build in =placebuild=. (This may take some time.)
- Check whether places are displayed accurately in the output file. If not, add regions/countries to disambiguate any incorrectly displayed places to the =disambig= dictionary in =variables.py= (see examples). - Check whether places are displayed accurately in the output file. If not, add regions/countries to disambiguate any incorrectly displayed places to the =disambig= dictionary in =variables.py= (see examples).
* Displaying a record collection (taking data from Discogs)
** Setup
- Upload a CSS file to the server for styling fonts etc; most CSS is set inline.
- Get a Discogs API token [[https://www.discogs.com/settings/developers][here]].
*** Setting the variables
In =variables.py=, edit:
- =trackrecords=: set to =True=
- =recordserverpath=: path to directory on the server where files will be uploaded, including trailing slash
- =recordcss=: location of the CSS file you wish to apply, relative to the site root
- =discogstoken=: the API token you generated
- =discogsuser=: your Discogs username
** Testing
- Run =python3 generaterecords.py local= to build in =recordbuild=.
* First run (or if adding any new trackers) * First run (or if adding any new trackers)
- Run =python3 init.py= and then =chmod +x build.sh=. - Run =python3 init.py= and then =chmod +x build.sh=.
- Create directories on the server corresponding to any =*serverpath= variables you have set. - Create directories on the server corresponding to any =*serverpath= variables you have set.

@ -93,7 +93,7 @@ if variables.trackplaces == True:
writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <base target=\"_top\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n <title>Map</title>\n <link rel=\"stylesheet\" href=\"") writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <base target=\"_top\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n <title>Map</title>\n <link rel=\"stylesheet\" href=\"")
if local: if local:
writefile.write(variables.domain) writefile.write(variables.domain)
writefile.write(variables.placecss + "\"/>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n <style>\n #map {\n width: 90vw;\n height: 90vh;\n margin: auto;\n }\n </style>\n </head>\n <body>\n <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n <script>\n const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n attribution: '&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n }).addTo(map);\n") writefile.write(variables.placecss + "\">\n <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n <style>\n #map {\n width: 90vw;\n height: 90vh;\n margin: auto;\n }\n </style>\n </head>\n <body>\n <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n <script>\n const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n attribution: '&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n }).addTo(map);\n")
for place in theplaces: for place in theplaces:
saniname = re.sub(" ","",(re.sub("","",(re.sub("-","",place["name"]))))) saniname = re.sub(" ","",(re.sub("","",(re.sub("-","",place["name"])))))
writefile.write(" const " + saniname + " = L.marker([" + str(place["lat"]) + ", " + str(place["long"]) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>") writefile.write(" const " + saniname + " = L.marker([" + str(place["lat"]) + ", " + str(place["long"]) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>")

@ -0,0 +1,88 @@
import discogs_client,re,time,variables
d = discogs_client.Client("Python/1.0",user_token=variables.discogstoken)
user = d.user(variables.discogsuser)
try:
if sys.argv[1] == "local":
local = True
else:
local = False
except:
local = False
if variables.trackrecords == True:
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":
if local:
recordstring += variables.domain
recordstring += variables.recordserverpath + "large.png"
elif record["format"] == "small":
if local:
recordstring += variables.domain
recordstring += variables.recordserverpath + "small.png"
recordstring += "\">\n <div>\n <p>" + record["artist"] + "<br>•<br>" + record["title"] + "</p>\n </div>\n </div>\n"
return recordstring
writefile = open("recordbuild/index.html","w")
writefile.write("<!DOCTYPE html>\n<html>\n <head>\n <link rel=\"stylesheet\" href=\"")
if local:
writefile.write(variables.domain)
writefile.write(variables.recordcss + "\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <title>Record collection</title>\n <style>\n main {\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 <h1>Record collection</h1>\n <main>\n")
for record in smalls:
writefile.write(recordwrite(record))
for record in larges:
writefile.write(recordwrite(record))
writefile.write(" </main>\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 </body>\n</html>\n")
writefile.close()

@ -10,4 +10,7 @@ if variables.trackgames == True:
if variables.trackplaces == True: if variables.trackplaces == True:
script.write("python3 generateplaces.py\nrclone copy placebuild " + variables.rclonesiteroot + variables.placeserverpath + " -P\n") script.write("python3 generateplaces.py\nrclone copy placebuild " + variables.rclonesiteroot + variables.placeserverpath + " -P\n")
if variables.trackrecords == True:
script.write("python3 generaterecords.py\nrclone copy recordbuild " + variables.rclonesiteroot + variables.recordserverpath + " -P\n")
script.close() script.close()

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

@ -48,3 +48,17 @@ placestartyear = 0
placecss = "" placecss = ""
# disambig = {"london":"england","belfast":"northern ireland"} # disambig = {"london":"england","belfast":"northern ireland"}
"""
Records
"""
trackrecords = False
recordserverpath = ""
recordcss = ""
discogstoken = ""
discogsuser = ""

Loading…
Cancel
Save