xdfcgi/allmaps.py

63 lines
2.0 KiB
Python

import sqlite3 as sql
import subprocess
def getmaps(database):
output = []
con = sql.connect(database)
with con:
cursor = con.cursor()
try:
# get all maps in database
cursor.execute("select distinct mapid from Cts_times;")
output = cursor.fetchall()
except sql.Error:
print("Shit is fucked.")
return output
# if there is no query then it outputs the index file.
def getcontent(query=None):
cmd = [("./cts")]
proc = subprocess.Popen(cmd, env=query, stdout=subprocess.PIPE, shell=True)
# communicate returns 'bytes' class with function 'decode'
return proc.communicate()[0].decode('utf-8')
def renderindex(template):
# no env variable
table = getcontent()
title = "XDF Leaderboards - Xonotie"
filename = "./output/index.html"
header = '''<h3>Xonotic DeFrag Server Leaderboards</h3>
<p><b>Server Name: </b>/v/ Xonotie 0.8.2 - Pacific / US West<br>
<b>Stats: </b><a href="http://stats.xonotic.org/server/6459">http://stats.xonotic.org/server/6459</a></p>
'''
with open(filename, 'w+') as fout:
fout.write(template % (title, header + table))
fout.close
pass
def main():
# for each map generate an html file.
template = ""
with open("template.html", 'r') as fin:
template = fin.read()
renderindex(template)
title_text = "%s - XDF Leaderboards - Xonotie"
maps = getmaps("db/cts.db")
for game_map in maps:
# game_map is a tuple obj.
map_name = game_map[0]
query = {"QUERY_STRING" : ("map=%s" % map_name)}
table = getcontent(query)
header = "<h3>%s</h3>" % map_name
filename = ("./output/%s.html" % map_name)
with open(filename, 'w+') as fout:
title = title_text % map_name
fout.write(template % (title, header + table))
fout.close()
pass
if __name__ == "__main__":
print("allmaps.py - Generating .html files for all maps.")
main()
pass