mapillary_download/get_sequences_of_username.py

112 lines
3.2 KiB
Python
Raw Permalink Normal View History

2024-07-24 19:13:42 +02:00
import json
import requests
2024-07-24 19:13:42 +02:00
# lit un json listant les id de photo de chaque séquence et va
# chercher la séquence par API.
import argparse
def parse_args(argv=None):
2024-07-24 19:13:42 +02:00
parser = argparse.ArgumentParser()
parser.add_argument(
"--username",
type=str,
help="Username to get the sequences id of",
required=True,
)
parser.add_argument("--dev_token", type=str, help="Your mapillary developer token")
parser.add_argument(
"--max_sequence", type=str, help="Limit the amount of retrieved sequence ids"
)
2024-07-24 19:13:42 +02:00
global args
args = parser.parse_args(argv)
print(args)
# Initialisation de la liste pour stocker les réponses
responses = []
sequences = []
2024-07-24 19:13:42 +02:00
def get_image_data_from_sequences():
username = args.username
input_file = "out_" + username + ".json"
2024-07-24 19:13:42 +02:00
# Chargement du fichier JSON d'entrée
with open(input_file, "r") as file:
input_data = json.load(file)
# Itération sur les noeuds pour collectionner les image_ids
nodelist = input_data["data"]["fetch__User"]["feed"]["nodes"]
print("séquences : ", len(nodelist))
2024-07-24 19:13:42 +02:00
image_ids = [node["image_id"] for node in nodelist]
print(image_ids)
dev_token = args.dev_token
# Préparation de la tête d'autorisation pour toutes les futures requêtes
header = {"Access-Token": dev_token}
ii = 0
2024-07-24 19:13:42 +02:00
limit_requests = 1000000000
# limit_requests = 5 # pour tester
2024-07-24 19:13:42 +02:00
# Boucle sur chaque image_id pour interroger l'API Mapillary
for image_id in image_ids:
ii += 1
2024-07-24 19:13:42 +02:00
if limit_requests >= ii and image_id:
params = {"id": image_id, "fields": "id,sequence"}
request_url = (
"https://graph.mapillary.com/"
+ str(image_id)
+ "?access_token="
+ dev_token
+ "&fields=id,sequence"
)
2024-07-24 19:13:42 +02:00
# print("requete: "+request_url)
response = requests.get(request_url)
# Analyse de la réponse
parsed_response = {}
if response.ok and response.status_code == 200:
raw_response = response.json()
parsed_response["id"] = raw_response["id"]
parsed_response["sequence"] = raw_response["sequence"]
sequences.append(parsed_response["sequence"])
print(
"séquence trouvée: "
+ str(ii)
+ "/"
+ args.max_sequence
+ " : "
+ raw_response["sequence"]
)
2024-07-24 19:13:42 +02:00
else:
print(response)
responses.append(parsed_response)
2024-07-24 19:13:42 +02:00
def persist_files():
# Sauvegarde des nouveaux résultats dans le fichier output.json
output_file = "sequences_" + args.username + ".json"
2024-07-24 19:13:42 +02:00
with open(output_file, "w") as file:
json.dump(responses, file)
sequence_filename = "sequences_" + args.username + ".txt"
2024-07-24 19:13:42 +02:00
with open(sequence_filename, "w") as file:
json.dump(sequences, file)
print("fichier sauvegardé: " + sequence_filename)
2024-07-24 19:13:42 +02:00
parse_args()
get_image_data_from_sequences()
persist_files()
# si la requete donne moins du max de noeuds on prévoit d'en relancer une nouvelle pour avoir la suite