Weird behaviour when using item.get_data() with PDF files

750
5
08-27-2017 11:45 AM
AitorCalero_García
Esri Contributor

Hello, I'm trying to use the item.get_data() method in a backup tool, to download all files from my content. However, when I call this method and the item.type is PDF, instead giving me the local path where the PDF should be stored, it gives me somthig like this:

b'PK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x00!\x00\xa0\x82\x90\x90\xc5\x01\x00\x00\xe2\x08\x00\x00\x13\x00\x08\x02[Content_Types].xml \xa2\x04\x02(\xa0\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 ....'

Is this the normal behaviour? When calling this method with CSV, KML, etc it works as I expected. The whole code I'm using is here: GISBox/ArcGISBackup.py at BackupTool · aitorcalero/GISBox · GitHub 

Part of the problem could be that I use this:

path = str(item.get_data())

to convert it to a string. However, if it's that the case, how do I get the path to the dowloaded PDF? The item.get_data() method when used in a PDF seems to be returning a {byte} array.

Tags (3)
0 Kudos
5 Replies
IonutAlixandroae4
Occasional Contributor

Hi Aitor,

I tested your problem, beeing conected to my ArcGIS Online Dev account, with a .pdf item and I can confirm that same problem happens. Also, I tried to dump it as json or to decode using utf-8 (as said in the methodology : arcgis.gis module — arcgis 1.2.1 documentation  ) but no result, same problem...  I`m also looking forward for a solution to this

Ionut

AtmaMani
Esri Contributor

To download a pdf file, try using `Item.download()` method. The `get_data()` is intended to read text based files such as JSON, CSV, XML.

PS: These two methods were enhanced in version 1.2. If you are using an older build, please update to the latest 1.2.1

AitorCalero_García
Esri Contributor

Thank you Atma! It worked like a charm. By the way, is there any way to get the file extension of a file stored in ArcGIS Onine in the Python API?

0 Kudos
AtmaMani
Esri Contributor

I am assuming the reason you want to know the extension is to decide whether to use download() or get_data(). While there is no property or method, you can use download() to always download any type of file (be it binary files like service definition, pdf or text based files like csv, webmap json etc).

In addition, you can use type and typeKeywords properties of the Item object to get a sense of the item type and choose accordingly. In certain cases, you get extension as part of the typeKeywords list.

AitorCalero_García
Esri Contributor

Thank you Atma. The reason why I'm trying to get the extension is because I want to create a kind of back-up process replicating locally the folder structure of a given account in ArcGIS Online/Enterprise.

I've tried to explore the type and typeKeywords options but the results are inconsistent. As you can see in the screenshots attached, these properties do not always return the same thing or even in the same position in the list and with ZIP items, you only get "ArcGIS Desktop". The only way I can get the extension seems to be to parse the path returned from the download() method.

That would be a new property to add in future releases of the API maybe.

Screenshots:

.Debug screen capture

Debug screen capture 2

0 Kudos