@@ -0,0 +1,2091 @@
+#!/usr/bin/python%0A%0Aimport os%0Afrom sys import argv%0Afrom sys import exit%0Afrom urllib import urlopen%0Afrom time import sleep%0A%0Ausage = '''Usage: %25s APIKEY STARTID ENDID %5BDESTFOLDER%5D %5BSLEEP_INTERVAL%5D%0A%0ADownloads movie data with ids as filenames to the output folder. Fetches movies with ids %5BSTARTID...ENDID%5D inclusively%0A'''%0A%0Aapi_key = None%0Abase_url = 'http://api.themoviedb.org/2.1/Movie.getInfo/en/json/%25(api_key)s/%25(movie_id)d'%0Ainterval_sec = 1.1%0Adest_dir = 'out'%0A%0Adef main():%0A    global api_key, interval_sec, dest_dir%0A%0A    if len(argv) %3C 4 :%0A        print usage %25 argv%5B0%5D%0A        exit(0)%0A%0A    api_key = argv%5B1%5D%0A%0A    try:%0A        start = int(argv%5B2%5D)%0A        end = int(argv%5B3%5D)%0A        start = min(start,end)%0A        end = max(start,end);%0A%0A        if len(argv) %3E 5:%0A            interval_sec = float(argv%5B5%5D)%0A    except:%0A        print 'invalid numeric parameters'%0A        exit(1)%0A%0A    if len(argv) %3E 4:%0A        dest_dir = argv%5B4%5D%0A%0A    if os.path.exists(dest_dir) == False:%0A        try:%0A            os.mkdir(dest_dir)%0A            print 'Directory %25s is created.' %25 dest_dir%0A        except Exception as err:%0A            print 'Error occurred: %25s' %25 err%0A            exit(2)%0A    print 'Destination directory is %5C'%25s%5C''%25 dest_dir%0A%0A    print 'Starting download %25d...%25d' %25 (start, end)%0A    f = open(%22output.txt%22, %22w%22)%0A    for movie_id in range(start,end+1):%0A        result = download_save(movie_id)%0A        if result:%0A            res = '#%25d: OK' %25 movie_id%0A            print res%0A            f.write(res + %22%5Cn%22)%0A        sleep(interval_sec)%0A    f.close()%0A%0Adef download_save(movie_id):%0A    try:%0A        url = prepare_url(movie_id)%0A%0A        file_name = url.split('/')%5B-1%5D%0A        local = open(os.path.join(dest_dir, file_name),'w+')%0A%0A        web = urlopen(url)%0A        local.write(web.read())%0A        web.close()%0A        local.close()%0A%0A        return True%0A    except Exception as err:%0A        print '#%25d: ERROR: %25s' %25 (movie_id, err)%0A        return False%0A%0Adef prepare_url(movie_id):%0A    global base_url%0A    return base_url %25 %7B%22api_key%22:api_key, %22movie_id%22:movie_id%7D%0A%0Aif __name__ == '__main__':%0A    main()%0A%0A

add download