Guest User

Untitled

a guest
Jan 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import csv, json
  2.  
  3. from asnake.client import ASnakeClient
  4. client = ASnakeClient()
  5. client.authorize()
  6.  
  7. def startCSV(CSV):
  8. fieldnames = ['lock_version', 'indicator', 'collection_identifier', 'series_identifier']
  9. with open(CSV, 'w', newline='') as outputCSV:
  10. writer = csv.DictWriter(outputCSV, fieldnames=fieldnames)
  11. writer.writeheader()
  12.  
  13. def addCSV(CSV, lock, ind, coll_id, ser_id):
  14. fieldnames = ['lock_version', 'indicator', 'collection_identifier', 'series_identifier']
  15. with open(CSV, 'a', newline='') as outputCSV:
  16. writer = csv.DictWriter(outputCSV, fieldnames=fieldnames)
  17. writer.writerow({'lock_version' : lock, 'indicator' : ind, 'collection_identifier' : coll_id, 'series_identifier' : ser_id})
  18.  
  19. def generate_csv(CSV):
  20. startCSV(CSV)
  21. for record in client.get_paged('repositories/3/top_containers'):
  22. if record is not None:
  23. lock = record["lock_version"]
  24. indicator = record["indicator"]
  25. if record["collection"] != []:
  26. collection_id = record["collection"][0]["identifier"] # because collection is a list
  27. else:
  28. collection_id = ''
  29. if record["series"] != []:
  30. for instance in record["series"]:
  31. series_id = instance["identifier"] # getting the identifier if it exists
  32. addCSV(CSV, lock, indicator, collection_id, series_id)
  33. else:
  34. series_id = ''
  35. addCSV(CSV, lock, indicator, collection_id, series_id)
  36.  
  37. newCSV = 'outputCSV.csv'
  38.  
  39. generate_csv(newCSV)
Add Comment
Please, Sign In to add comment