fetch_resource.py 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import urllib2
  2. import argparse
  3. import xmlrpclib
  4. def parse_args():
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument('-r', '--resource-id', type=int, required=True)
  7. parser.add_argument('-o', '--output', required=True)
  8. return parser.parse_args()
  9. def fetch(url, retries=4, timeout=5):
  10. for i in xrange(retries):
  11. try:
  12. return urllib2.urlopen(url, timeout=timeout).read()
  13. except Exception:
  14. if i + 1 < retries:
  15. continue
  16. else:
  17. raise
  18. def fetch_resource(id_):
  19. urls = xmlrpclib.ServerProxy("https://sandbox.yandex-team.ru/sandbox/xmlrpc").get_resource_http_links(id_)
  20. for u in urls:
  21. try:
  22. return fetch(u)
  23. except Exception:
  24. continue
  25. raise Exception('Cannot fetch resource {}'.format(id_))
  26. if __name__ == '__main__':
  27. args = parse_args()
  28. with open(args.output, 'wb') as f:
  29. f.write(fetch_resource(int(args.resource_id)))