fetch_allowed_addons.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. import urllib.request
  3. import json
  4. import base64
  5. import sys
  6. def fetch(x):
  7. with urllib.request.urlopen(x) as response:
  8. return response.read()
  9. def find_addon(addons, addon_id):
  10. results = addons['results']
  11. for x in results:
  12. addon = x['addon']
  13. if addon['guid'] == addon_id:
  14. return addon
  15. sys.exit("Error: cannot find addon " + addon_id)
  16. def fetch_and_embed_icons(addons):
  17. results = addons['results']
  18. for x in results:
  19. addon = x['addon']
  20. icon_data = fetch(addon['icon_url'])
  21. addon['icon_url'] = 'data:image/png;base64,' + str(base64.b64encode(icon_data), 'utf8')
  22. def patch_https_everywhere(addons):
  23. addon = find_addon(addons, 'https-everywhere@eff.org')
  24. addon['guid'] = 'https-everywhere-eff@eff.org'
  25. addon['url'] = 'https://www.eff.org/https-everywhere'
  26. def main(argv):
  27. amo_collection = argv[0] if argv else '83a9cccfe6e24a34bd7b155ff9ee32'
  28. url = 'https://services.addons.mozilla.org/api/v4/accounts/account/mozilla/collections/' + amo_collection + '/addons/'
  29. data = json.loads(fetch(url))
  30. fetch_and_embed_icons(data)
  31. patch_https_everywhere(data)
  32. data['results'].sort(key=lambda x: x['addon']['guid'])
  33. find_addon(data, '{73a6fe31-595d-460b-a920-fcc0f8843232}') # Check that NoScript is present
  34. print(json.dumps(data, indent=2, ensure_ascii=False))
  35. if __name__ == "__main__":
  36. main(sys.argv[1:])