import json, ssl, urllib.request

CTX = ssl._create_unverified_context()
BASE = 'https://10.112.205.15'
MAXR = 30000


def get(u):
    req = urllib.request.Request(u, headers={'User-Agent': 'Mozilla/5.0'})
    r = urllib.request.urlopen(req, timeout=20, context=CTX)
    return r.read()


def main():
    out = open('/tmp/.nx_list.tsv', 'w')
    for repo in ('raw-hosted', 'npm-hosted', 'pypi-hosted', 'nuget-hosted', 'helm-hosted', 'gitlfs-hosted'):
        tok = None
        n = 0
        while n < MAXR:
            u = BASE + '/service/rest/v1/search?repository=%s' % repo
            if tok:
                u += '&continuationToken=' + tok
            try:
                d = json.loads(get(u))
            except Exception as e:
                print('ERR', repo, type(e).__name__, str(e)[:80])
                break
            items = d.get('items', [])
            for it in items:
                for a in it.get('assets', []) or [{}]:
                    out.write('%s\t%s\t%s\t%s\t%s\n' % (repo, it.get('name'), a.get('fileSize', ''), a.get('contentType', ''), a.get('downloadUrl', '')))
                    n += 1
            tok = d.get('continuationToken')
            if not tok or not items:
                break
        out.flush()
        print(repo, n, flush=True)
    out.close()

main()
