aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/fetch_resource.py
blob: d5af311e5d8e9623c3d45a3bee9ca085bfbbdb61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import urllib2
import argparse
import xmlrpclib


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-r', '--resource-id', type=int, required=True)
    parser.add_argument('-o', '--output', required=True)
    return parser.parse_args()


def fetch(url, retries=4, timeout=5):
    for i in xrange(retries):
        try:
            return urllib2.urlopen(url, timeout=timeout).read()

        except Exception:
            if i + 1 < retries:
                continue

            else:
                raise


def fetch_resource(id_):
    urls = xmlrpclib.ServerProxy("https://sandbox.yandex-team.ru/sandbox/xmlrpc").get_resource_http_links(id_)

    for u in urls:
        try:
            return fetch(u)

        except Exception:
            continue

    raise Exception('Cannot fetch resource {}'.format(id_))


if __name__ == '__main__':
    args = parse_args()

    with open(args.output, 'wb') as f:
        f.write(fetch_resource(int(args.resource_id)))