urschrei / pyzotero

Pyzotero: a Python client for the Zotero API
https://pyzotero.readthedocs.org
Other
908 stars 99 forks source link

How can I access the link of an attachment file in Zotero and update it? #156

Closed hbshrestha closed 2 years ago

hbshrestha commented 2 years ago

First of all, thank you for this great package. I have recently switched from Mendeley to Zotero, so I am relatively new to using it.

I have attached link to the copy of a file, which is in dropbox, as can be seen in the image below: image

I am going to buy a professional version of Zotero soon. So I am going to migrate these attachment files from dropbox to the Zotero cloud. This means that the links to these files won't work any more, and they need to be updated.

I am able to access all_items from my Zotero library of group library_type using the following code:

zot = zotero.Zotero(library_id, library_type, api_key)

#To acccess only 1000 items
#items = zot.top(limit = 10000)

#To access all items at once
all_items = zot.everything(zot.top())
all_items

I am able to access the specific attributes of the specific item as follows:

for item in all_items:
    if item["meta"]["createdByUser"]["username"] == username:

        if ("title" in item["data"].keys()) and item["data"]["title"] == "100% variable renewable energy grid: Survey of possibilities":

            print (item["links"],"\n")
            print (item["meta"], "\n")
            print (item["data"])

This returns me the item attributes as shown below: image

However, I don't find the link to the attached file in dropbox in these attributes returned. How can I access the link or the attributes of the attached file? I have thousands of files, and not all have attachments. For the files which have attachments, I want to change the link from dropbox to one in Zotero cloud when I purchase the Zotero professional.

Is it possible to automate this process? Thank you!

urschrei commented 2 years ago

Top-level items e.g. articles with attachments have a numChildren key which will tell you how many child items they have. You can retrieve children using zot.children(itemKey). The returned items contain extensive metadata in their links key, including MIME type and href. That would be the first place I would look for information pertaining to Dropbox. However, I've never used Zotero with a third-party storage service so I don't know if this is the correct key.

Regarding changing the link, I don't know whether that's possible or whether you'll have to add a new child item, pointing to the new attachment you've stored in Zotero, to the parent, then delete the "stale" child. In general however, the process can certainly be automated. The forums are the best place to ask about how best to do this: https://forums.zotero.org

hbshrestha commented 2 years ago

Thank you for your prompt response! I see that there is mention of numChildren in item["meta"]. image

However, I get the following error message when I tried to retrieve children using zot.children(item) orzot.children(all_items) or even zot.children(item["meta"]. I am not sure where I can find the itemKey. Could you possible elaborate?

AttributeError: 'dict' object has no attribute 'upper'

urschrei commented 2 years ago

As per the docs, children is called with a string argument, which is the key attribute of a retrieved item: https://pyzotero.readthedocs.io/en/latest/index.html?highlight=children#zotero.Zotero.children

hbshrestha commented 2 years ago

Thank you! I could access the child item using

zot.children(item["key")

and I could access the url link to dropbox using

zot.children(item["key"])[0]["data"]["url"]