#!/usr/bin/python3

import os
import pymysql
site = os.getenv('SERVER_NAME')

print("content-type: application/xml")

root_mtime = int(os.path.getmtime('index.full.python.html'))
news_mtime = ''
pop_mtime = ''
con = pymysql.connect(host = 'localhost', user = 'casino', database = 'casino', password = 'casino')
with con.cursor() as cur:
    cur.execute('select value from settings where name="popularity_last_updated"')
    rows = cur.fetchall()
    pop_mtime = rows[0][0]
    cur.execute('select value from settings where name="news_last_updated"')
    rows = cur.fetchall()
    news_mtime = rows[0][0]

print(f'''
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://{site}/</loc>
    <lastmod>{root_mtime}</lastmod>
  </url>
  <url>
    <loc>https://{site}/news.jsn</loc>
    <lastmod>{news_mtime}</lastmod>
  </url>
  <url>
    <loc>https://{site}/popularity.jsn</loc>
    <lastmod>{pop_mtime}</lastmod>
  </url>
</urlset>
''')
