From 826119e79736083890f7ad6bb74bb3748b4500ad Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 3 Oct 2018 08:39:11 +0530 Subject: [PATCH] Use new fosshub upload API --- setup/upload.py | 60 ++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/setup/upload.py b/setup/upload.py index e62f8b7607..fd3df245e9 100644 --- a/setup/upload.py +++ b/setup/upload.py @@ -11,7 +11,7 @@ from subprocess import check_call from tempfile import NamedTemporaryFile, mkdtemp, gettempdir from zipfile import ZipFile from polyglot.builtins import iteritems -from polyglot.urllib import urlopen, urlencode +from polyglot.urllib import urlopen, Request if __name__ == '__main__': d = os.path.dirname @@ -157,8 +157,37 @@ def run_remote_upload(args): def upload_to_fosshub(): + # https://devzone.fosshub.com/dashboard/restApi # fosshub has no API to do partial uploads, so we always upload all files. + api_key = get_fosshub_data() + + def request(path, data=None): + r = Request('https://api.fosshub.com/rest/' + path.lstrip('/'), + headers={ + 'Content-Type': 'application/json', + 'X-auth-key': api_key, + 'User-Agent': 'calibre' + }) + res = urlopen(r, data=data) + ans = json.loads(res.read()) + if ans.get('error'): + raise SystemExit(ans['error']) + if res.getcode() != 200: + raise SystemExit('Request to {} failed with response code: {}'.format(path, res.getcode())) + # from pprint import pprint + # pprint(ans) + return ans['data'] if 'data' in ans else ans['status'] + print('Sending upload request to fosshub...') + project_id = None + + for project in request('projects'): + if project['name'].lower() == 'calibre': + project_id = project['id'] + break + else: + raise SystemExit('No calibre project found') + files = set(installers()) entries = [] for fname in files: @@ -167,33 +196,18 @@ def upload_to_fosshub(): __version__, os.path.basename(fname) ) entries.append({ - 'url': url, + 'fileUrl': url, 'type': desc, 'version': __version__, }) jq = { - 'software': 'Calibre', - 'apiKey': get_fosshub_data(), - 'upload': entries, - 'delete': [{ - 'type': '*', - 'version': '*', - 'name': '*' - }] + 'version': __version__, + 'files': entries, + 'publish': True, + 'isOldRelease': False, } # print(json.dumps(jq, indent=2)) - rq = urlopen( - 'https://www.fosshub.com/JSTools/uploadJson', - urlencode({ - 'content': json.dumps(jq) - }) - ) - resp = rq.read() - if rq.getcode() != 200: - raise SystemExit( - 'Failed to upload to fosshub, with HTTP error code: %d and response: %s' - % (rq.getcode(), resp) - ) + request('projects/{}/releases/'.format(project_id), data=json.dumps(jq)) class UploadInstallers(Command): # {{{ @@ -207,7 +221,7 @@ class UploadInstallers(Command): # {{{ ) def run(self, opts): - # return upload_to_fosshub() + return upload_to_fosshub() all_possible = set(installers()) available = set(glob.glob('dist/*')) files = {