# Consider this file MIT licensed.

import pyfusefilter
import re
import urllib.parse
import base64
from http.cookiejar import Cookie
import os
import requests
from urllib.parse import urlparse
import tempfile

with open('cookie', 'r', encoding='utf-8') as file:
    COOKIE = file.read().strip()

# Stolen from https://commons.wikimedia.org/wiki/Commons:SPARQL_query_service/API_endpoint#Python
def init_session(endpoint, token):
    domain = urlparse(endpoint).netloc
    session = requests.Session()
    session.headers.update({
        'User-Agent': 'nsfw-gadget/0.0 (https://commons.wikimedia.org/wiki/User:Bawolff/nsfw.js)',
    })
    session.cookies.set_cookie(Cookie(0, 'wcqsOauth', token, None, False, domain, False, False, '/', True,
        False, None, True, None, None, {}))
    return session

ENDPOINT = 'https://commons-query.wikimedia.org/sparql'
session = init_session(ENDPOINT, COOKIE)
response = session.post(
    url=ENDPOINT,
    data={'query': """
        SELECT distinct ?url WHERE {
          ?file wdt:P14416 [] .
          ?file schema:contentUrl ?url.
        }
    """},
    headers={'Accept': 'application/json'}
)
response.raise_for_status()

queryResults = response.json()['results']['bindings']

# End part stolen from wiki.


items = []
for entry in queryResults:
	suffix = re.search( r"[^/]+$", entry['url']['value'] )
	items.append(urllib.parse.unquote(suffix.group(0)))
# xor is more space efficient than binary fuse at our scale.
filter = pyfusefilter.Xor8(items)
with tempfile.NamedTemporaryFile('wb', dir=".", delete=False ) as tf:
	tf.write(bytes(filter.serialize()))
	# Not sure if I should check that hash is different than only replace on hash mismatch to make browser cache more efficient.
	os.replace( tf.name, "public_html/filters/p14416.xor8.bin" )
filter16 = pyfusefilter.Xor16(items)
with tempfile.NamedTemporaryFile('wb', dir=".", delete=False ) as tf:
	tf.write(bytes(filter16.serialize()))
	# Not sure if I should check that hash is different than only replace on hash mismatch to make browser cache more efficient.
	os.replace( tf.name, "public_html/filters/p14416.xor16.bin" )
