2024-07-24 19:13:42 +02:00
|
|
|
import json
|
|
|
|
import requests
|
2024-10-25 23:19:29 +02:00
|
|
|
|
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
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
|
|
|
|
def parse_args(argv=None):
|
2024-07-24 19:13:42 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-10-25 23:19:29 +02:00
|
|
|
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-10-25 23:19:29 +02:00
|
|
|
|
2024-07-24 19:13:42 +02:00
|
|
|
def get_image_data_from_sequences():
|
|
|
|
username = args.username
|
2024-10-25 23:19:29 +02:00
|
|
|
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"]
|
2024-10-25 23:19:29 +02:00
|
|
|
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}
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
ii = 0
|
2024-07-24 19:13:42 +02:00
|
|
|
limit_requests = 1000000000
|
2024-10-25 23:19:29 +02:00
|
|
|
# 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:
|
2024-10-25 23:19:29 +02:00
|
|
|
ii += 1
|
2024-07-24 19:13:42 +02:00
|
|
|
if limit_requests >= ii and image_id:
|
|
|
|
params = {"id": image_id, "fields": "id,sequence"}
|
2024-10-25 23:19:29 +02:00
|
|
|
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"])
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
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-10-25 23:19:29 +02:00
|
|
|
|
2024-07-24 19:13:42 +02:00
|
|
|
def persist_files():
|
|
|
|
# Sauvegarde des nouveaux résultats dans le fichier output.json
|
2024-10-25 23:19:29 +02:00
|
|
|
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)
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
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)
|
2024-10-25 23:19:29 +02:00
|
|
|
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
|