In [139]:
import requests, json, sys
from bib.models import Book
from orea.settings.pandorfer_local import Z_USER_ID, Z_COLLECTION, Z_API_KEY

In [140]:
root = "https://api.zotero.org/users/"
params = "{}/collections/{}/items/top?v=3&key={}".format(Z_USER_ID,
                                                         Z_COLLECTION,
                                                         Z_API_KEY)
url = root+params+"&sort=dateModified&limit=100"

In [141]:
try:
    r = requests.get(url)
except:
    sys.exit("aa! errors!")

In [142]:
response = r.json()

In [143]:
response


Out[143]:
[{'data': {'ISBN': '',
   'abstractNote': '',
   'accessDate': '',
   'archive': '',
   'archiveLocation': '',
   'callNumber': '',
   'collections': ['5XIKB2GS'],
   'creators': [{'creatorType': 'author', 'name': 'asdf xyz'}],
   'date': '',
   'dateAdded': '2016-03-07T09:30:20Z',
   'dateModified': '2016-03-07T09:47:57Z',
   'edition': '',
   'extra': '',
   'itemType': 'book',
   'key': 'WV58FJZJ',
   'language': '',
   'libraryCatalog': '',
   'numPages': '',
   'numberOfVolumes': '',
   'place': '',
   'publisher': '',
   'relations': {},
   'rights': '',
   'series': '',
   'seriesNumber': '',
   'shortTitle': '',
   'tags': [],
   'title': 'third test',
   'url': '',
   'version': 7092,
   'volume': ''},
  'key': 'WV58FJZJ',
  'library': {'id': 2756275,
   'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw',
     'type': 'text/html'}},
   'name': 'defc-orea-oeaw',
   'type': 'user'},
  'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw/items/WV58FJZJ',
    'type': 'text/html'},
   'self': {'href': 'https://api.zotero.org/users/2756275/items/WV58FJZJ',
    'type': 'application/json'}},
  'meta': {'creatorSummary': 'asdf xyz', 'numChildren': 0},
  'version': 7092},
 {'data': {'ISBN': '',
   'abstractNote': '',
   'accessDate': '',
   'archive': '',
   'archiveLocation': '',
   'bookTitle': '',
   'callNumber': '',
   'collections': ['5XIKB2GS'],
   'creators': [],
   'date': '',
   'dateAdded': '2016-03-07T09:36:02Z',
   'dateModified': '2016-03-07T09:45:31Z',
   'edition': '',
   'extra': '',
   'itemType': 'bookSection',
   'key': 'W3ZWS5UR',
   'language': '',
   'libraryCatalog': '',
   'numberOfVolumes': '',
   'pages': '',
   'place': '',
   'publisher': '',
   'relations': {},
   'rights': '',
   'series': '',
   'seriesNumber': '',
   'shortTitle': '',
   'tags': [],
   'title': 'A test book section',
   'url': '',
   'version': 7091,
   'volume': ''},
  'key': 'W3ZWS5UR',
  'library': {'id': 2756275,
   'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw',
     'type': 'text/html'}},
   'name': 'defc-orea-oeaw',
   'type': 'user'},
  'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw/items/W3ZWS5UR',
    'type': 'text/html'},
   'self': {'href': 'https://api.zotero.org/users/2756275/items/W3ZWS5UR',
    'type': 'application/json'}},
  'meta': {'numChildren': 0},
  'version': 7091},
 {'data': {'ISBN': '',
   'abstractNote': 'something to modify the modification date',
   'accessDate': '',
   'archive': '',
   'archiveLocation': '',
   'callNumber': '',
   'collections': ['5XIKB2GS'],
   'creators': [{'creatorType': 'author',
     'firstName': 'test',
     'lastName': 'testauthor'}],
   'date': '',
   'dateAdded': '2016-03-03T09:24:09Z',
   'dateModified': '2016-03-07T09:22:22Z',
   'edition': '',
   'extra': '',
   'itemType': 'book',
   'key': 'JT4A3ZZV',
   'language': '',
   'libraryCatalog': '',
   'numPages': '',
   'numberOfVolumes': '',
   'place': '',
   'publisher': '',
   'relations': {},
   'rights': '',
   'series': '',
   'seriesNumber': '',
   'shortTitle': '',
   'tags': [],
   'title': 'another test',
   'url': '',
   'version': 7082,
   'volume': ''},
  'key': 'JT4A3ZZV',
  'library': {'id': 2756275,
   'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw',
     'type': 'text/html'}},
   'name': 'defc-orea-oeaw',
   'type': 'user'},
  'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw/items/JT4A3ZZV',
    'type': 'text/html'},
   'self': {'href': 'https://api.zotero.org/users/2756275/items/JT4A3ZZV',
    'type': 'application/json'}},
  'meta': {'creatorSummary': 'testauthor', 'numChildren': 0},
  'version': 7082},
 {'data': {'ISBN': '978-0-253-21306-8',
   'abstractNote': '',
   'accessDate': '',
   'archive': '',
   'archiveLocation': '',
   'callNumber': '',
   'collections': ['5XIKB2GS'],
   'creators': [{'creatorType': 'author',
     'firstName': 'K. D.',
     'lastName': 'Vitelli'}],
   'date': '1999',
   'dateAdded': '2016-03-03T13:00:17Z',
   'dateModified': '2016-03-03T13:00:17Z',
   'edition': '',
   'extra': '',
   'itemType': 'book',
   'key': '5P6JTJMB',
   'language': '',
   'libraryCatalog': '',
   'numPages': '',
   'numberOfVolumes': '',
   'place': 'Indianapolis',
   'publisher': '',
   'relations': {},
   'rights': '',
   'series': '',
   'seriesNumber': '',
   'shortTitle': '',
   'tags': [],
   'title': 'Franchthi Neolithic Pottery. Volume 2.The later Neolithic Ceramic Phases 3 to 5, Excavations at Franchthi Cave, Greece Fascicle 10',
   'url': '',
   'version': 7079,
   'volume': '2'},
  'key': '5P6JTJMB',
  'library': {'id': 2756275,
   'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw',
     'type': 'text/html'}},
   'name': 'defc-orea-oeaw',
   'type': 'user'},
  'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw/items/5P6JTJMB',
    'type': 'text/html'},
   'self': {'href': 'https://api.zotero.org/users/2756275/items/5P6JTJMB',
    'type': 'application/json'}},
  'meta': {'creatorSummary': 'Vitelli',
   'numChildren': 0,
   'parsedDate': '1999'},
  'version': 7079},
 {'data': {'ISBN': '',
   'abstractNote': '',
   'accessDate': '',
   'archive': '',
   'archiveLocation': '',
   'callNumber': '',
   'collections': ['5XIKB2GS'],
   'creators': [{'creatorType': 'editor',
     'firstName': 'V.',
     'lastName': 'Vassilopoulou'},
    {'creatorType': 'author',
     'firstName': 'S.',
     'lastName': 'Katsarou-Tzeveleki'}],
   'date': '2009',
   'dateAdded': '2016-03-03T12:54:46Z',
   'dateModified': '2016-03-03T12:54:46Z',
   'edition': '',
   'extra': '',
   'itemType': 'book',
   'key': 'Q8JUCJWT',
   'language': '',
   'libraryCatalog': '',
   'numPages': '',
   'numberOfVolumes': '',
   'place': 'Markopoulo of Mesogeia',
   'publisher': '',
   'relations': {},
   'rights': '',
   'series': '',
   'seriesNumber': '',
   'shortTitle': '',
   'tags': [],
   'title': 'From Mesogeia to Argosaronikos. B’ Ephorate of Prehistoric and Classical Antiquities. Research of a Decade, 1994-2003. Proceedings of Conference Athens December 18-20 2003.',
   'url': '',
   'version': 7077,
   'volume': ''},
  'key': 'Q8JUCJWT',
  'library': {'id': 2756275,
   'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw',
     'type': 'text/html'}},
   'name': 'defc-orea-oeaw',
   'type': 'user'},
  'links': {'alternate': {'href': 'https://www.zotero.org/defc-orea-oeaw/items/Q8JUCJWT',
    'type': 'text/html'},
   'self': {'href': 'https://api.zotero.org/users/2756275/items/Q8JUCJWT',
    'type': 'application/json'}},
  'meta': {'creatorSummary': 'Katsarou-Tzeveleki',
   'numChildren': 0,
   'parsedDate': '2009'},
  'version': 7077}]

In [144]:
failed = []
saved = []
for x in response:
    try:
        x["data"]["creators"][0]
        try:
            x["data"]["creators"][0]['name']
            name = (x["data"]["creators"][0]["name"])
        except:
            firstname = x["data"]["creators"][0]["firstName"]
            lastname = x["data"]["creators"][0]["lastName"]
            name = "{}, {}".format(lastname, firstname)

    except:
        name = "no name provided"
    NewBook = Book(zoterokey = x["data"]["key"],
                   item_type =x["data"]["itemType"],
                   author=name,
                   title =x["data"]["title"],
                   short_title = x["data"]["shortTitle"])
    try:
        NewBook.save()
        saved.append(x['data'])
    except:
        failed.append(x['data'])

In [120]:
failed = []
saved = []
for x in response:
    try:
        x["data"]["creators"][0]
        if x["data"]["creators"][0]["name"]:
            NewBook = Book(zoterokey = x["data"]["key"],
                           item_type =x["data"]["itemType"],
                           author=x["data"]["creators"][0]["name"],
                           title =x["data"]["title"],
                           short_title = x["data"]["shortTitle"])
            print(x["data"]["creators"][0]["name"])
        else:
            firstname = x["data"]["creators"][0]["firstName"]
            lastname = x["data"]["creators"][0]["lastName"]
            name = "{}, {}".format(lastname, firstname)
            NewBook = Book(zoterokey = x["data"]["key"],
                           item_type =x["data"]["itemType"],
                           author=name,
                           title =x["data"]["title"],
                           short_title = x["data"]["shortTitle"])
            print(name)
    except:
        noname = 'no author information provided'
        NewBook = Book(zoterokey = x["data"]["key"],
                       item_type =x["data"]["itemType"],
                       author=noname,
                       title =x["data"]["title"],
                       short_title = x["data"]["shortTitle"])
        print(noname)
    try:
        NewBook.save()
        saved.append(x['data'])
    except:
        failed.append(x['data'])


no author information provided
no author information provided
no author information provided
no author information provided
no author information provided

In [145]:
saved


Out[145]:
[{'ISBN': '',
  'abstractNote': '',
  'accessDate': '',
  'archive': '',
  'archiveLocation': '',
  'callNumber': '',
  'collections': ['5XIKB2GS'],
  'creators': [{'creatorType': 'author', 'name': 'asdf xyz'}],
  'date': '',
  'dateAdded': '2016-03-07T09:30:20Z',
  'dateModified': '2016-03-07T09:47:57Z',
  'edition': '',
  'extra': '',
  'itemType': 'book',
  'key': 'WV58FJZJ',
  'language': '',
  'libraryCatalog': '',
  'numPages': '',
  'numberOfVolumes': '',
  'place': '',
  'publisher': '',
  'relations': {},
  'rights': '',
  'series': '',
  'seriesNumber': '',
  'shortTitle': '',
  'tags': [],
  'title': 'third test',
  'url': '',
  'version': 7092,
  'volume': ''},
 {'ISBN': '',
  'abstractNote': '',
  'accessDate': '',
  'archive': '',
  'archiveLocation': '',
  'bookTitle': '',
  'callNumber': '',
  'collections': ['5XIKB2GS'],
  'creators': [],
  'date': '',
  'dateAdded': '2016-03-07T09:36:02Z',
  'dateModified': '2016-03-07T09:45:31Z',
  'edition': '',
  'extra': '',
  'itemType': 'bookSection',
  'key': 'W3ZWS5UR',
  'language': '',
  'libraryCatalog': '',
  'numberOfVolumes': '',
  'pages': '',
  'place': '',
  'publisher': '',
  'relations': {},
  'rights': '',
  'series': '',
  'seriesNumber': '',
  'shortTitle': '',
  'tags': [],
  'title': 'A test book section',
  'url': '',
  'version': 7091,
  'volume': ''},
 {'ISBN': '',
  'abstractNote': 'something to modify the modification date',
  'accessDate': '',
  'archive': '',
  'archiveLocation': '',
  'callNumber': '',
  'collections': ['5XIKB2GS'],
  'creators': [{'creatorType': 'author',
    'firstName': 'test',
    'lastName': 'testauthor'}],
  'date': '',
  'dateAdded': '2016-03-03T09:24:09Z',
  'dateModified': '2016-03-07T09:22:22Z',
  'edition': '',
  'extra': '',
  'itemType': 'book',
  'key': 'JT4A3ZZV',
  'language': '',
  'libraryCatalog': '',
  'numPages': '',
  'numberOfVolumes': '',
  'place': '',
  'publisher': '',
  'relations': {},
  'rights': '',
  'series': '',
  'seriesNumber': '',
  'shortTitle': '',
  'tags': [],
  'title': 'another test',
  'url': '',
  'version': 7082,
  'volume': ''},
 {'ISBN': '978-0-253-21306-8',
  'abstractNote': '',
  'accessDate': '',
  'archive': '',
  'archiveLocation': '',
  'callNumber': '',
  'collections': ['5XIKB2GS'],
  'creators': [{'creatorType': 'author',
    'firstName': 'K. D.',
    'lastName': 'Vitelli'}],
  'date': '1999',
  'dateAdded': '2016-03-03T13:00:17Z',
  'dateModified': '2016-03-03T13:00:17Z',
  'edition': '',
  'extra': '',
  'itemType': 'book',
  'key': '5P6JTJMB',
  'language': '',
  'libraryCatalog': '',
  'numPages': '',
  'numberOfVolumes': '',
  'place': 'Indianapolis',
  'publisher': '',
  'relations': {},
  'rights': '',
  'series': '',
  'seriesNumber': '',
  'shortTitle': '',
  'tags': [],
  'title': 'Franchthi Neolithic Pottery. Volume 2.The later Neolithic Ceramic Phases 3 to 5, Excavations at Franchthi Cave, Greece Fascicle 10',
  'url': '',
  'version': 7079,
  'volume': '2'},
 {'ISBN': '',
  'abstractNote': '',
  'accessDate': '',
  'archive': '',
  'archiveLocation': '',
  'callNumber': '',
  'collections': ['5XIKB2GS'],
  'creators': [{'creatorType': 'editor',
    'firstName': 'V.',
    'lastName': 'Vassilopoulou'},
   {'creatorType': 'author',
    'firstName': 'S.',
    'lastName': 'Katsarou-Tzeveleki'}],
  'date': '2009',
  'dateAdded': '2016-03-03T12:54:46Z',
  'dateModified': '2016-03-03T12:54:46Z',
  'edition': '',
  'extra': '',
  'itemType': 'book',
  'key': 'Q8JUCJWT',
  'language': '',
  'libraryCatalog': '',
  'numPages': '',
  'numberOfVolumes': '',
  'place': 'Markopoulo of Mesogeia',
  'publisher': '',
  'relations': {},
  'rights': '',
  'series': '',
  'seriesNumber': '',
  'shortTitle': '',
  'tags': [],
  'title': 'From Mesogeia to Argosaronikos. B’ Ephorate of Prehistoric and Classical Antiquities. Research of a Decade, 1994-2003. Proceedings of Conference Athens December 18-20 2003.',
  'url': '',
  'version': 7077,
  'volume': ''}]

In [148]:
Book.objects.get(zoterokey='5P6JTJMB')


Out[148]:
<Book: Vitelli, K. D. Franchthi Neolithic Pottery. Volume 2.The later Neolithic Ceramic Phases 3 to 5, Excavations at Franchthi Cave, Greece Fascicle 10 None None>

In [ ]: