Skip to content

Instantly share code, notes, and snippets.

@dido18
Last active June 22, 2017 18:29
Show Gist options
  • Save dido18/8e34b49af1fcbd4ebc6f8c91c0e3bdf7 to your computer and use it in GitHub Desktop.
Save dido18/8e34b49af1fcbd4ebc6f8c91c0e3bdf7 to your computer and use it in GitHub Desktop.
import os
import json
import requests
url = "http://131.114.88.8:3000/api/images"
path_file_json = os.getcwd()+"/images-graph.json"
page = 1 # first page to be downloaed
limit = 1000 # number of images per page
payload = {'page': page, 'limit': limit}
images_graph = {}
images = []
res = requests.get(url, params=payload).json()
# "count":87570,"page":1,"limit":200,"pages":438,"images":
count = res['count']
pages = res['pages']
images_graph["num_nodes"] = count
print("{} number of images to download".format(count))
print("{} number of total pages".format(pages))
print("downloading...")
while page <= pages:
payload = {'page': page, 'limit': limit}
res = requests.get(url, params=payload).json()
#print("downloading page {}".format(page))
for image in res["images"]:
images.append((image["name"], image["from_repo"]))
page += 1
images_graph["edges"] = images
print("downloaed completed")
with open(path_file_json, 'w') as f:
json.dump(images_graph, f, ensure_ascii=False)
print("Saved into {} ".format(path_file_json))
with open(path_file_json) as json_data:
images_graph = json.load(json_data)
print(images_graph["num_nodes"])
print(images_graph["edges"])
@dido18
Copy link
Author

dido18 commented Jun 22, 2017

JSON structure

{
    "num_nodes": <num>,
   "edges": 
        [ 
          [node1, node-parent1], 
          [node2, node-parent2],
         ...
         ]
}

requirements.txt
requests==2.11.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment