Download File From Shared Dropbox Using Dropbox ApiError - python

Im looking to download a CSV file that is on a shared dropbox folder. The code that I currently have given me an ApiError. Full code and error below:
My Code:
import dropbox
ACCESS_TOKEN = '***MY ACCESS_TOKEN***'
dbx = dropbox.Dropbox(ACCESS_TOKEN)
url = "https://www.dropbox.com/sh/s8vwbg46zjsg3rw/AAC0T1BhIgfp5BfH_sJ_Vnb1a?dl=0"
file = "https://www.dropbox.com/sh/s8vwbg46zjsg3rw/AAC0T1BhIgfp5BfH_sJ_Vnb1a?dl=0&preview=Stock+List+2021-03-08.csv"
md, res = dbx.sharing_get_shared_link_file(url=file)
print(md)
print(res)
Error:
Traceback (most recent call last):
File "D:\***\PyCharm\Furniture\Test 1\dropbox_test.py", line 10, in <module>
md, res = dbx.sharing_get_shared_link_file(url=file)
File "C:\Python39\lib\site-packages\dropbox\base.py", line 4181, in sharing_get_shared_link_file
r = self.request(
File "C:\Python39\lib\site-packages\dropbox\dropbox_client.py", line 346, in request
raise ApiError(res.request_id,
dropbox.exceptions.ApiError: ApiError('90075839f9f94c53a112a48692314d4f', GetSharedLinkFileError('shared_link_is_directory', None))
Any help would be great. I have also tried files_download and I also get an error.

The "https://www.dropbox.com/sh/s8vwbg46zjsg3rw/AAC0T1BhIgfp5BfH_sJ_Vnb1a?dl=0..." link itself points to a folder, not a particular file (whether or not you have the preview parameter on it).
Here are two ways you can make this work:
Supply the path parameter on sharing_get_shared_link_file to specify the file in the folder you want:
md, res = dbx.sharing_get_shared_link_file(url=url, path="/Stock List 2021-03-08.csv")
Use the actual link to the file in particular (which I retrieved manually via the shared link page):
file = "https://www.dropbox.com/sh/s8vwbg46zjsg3rw/AABhEIN92e98iufhllgVuIvga/Stock%20List%202021-03-08.csv?dl=0"
md, res = dbx.sharing_get_shared_link_file(url=file)
Also, if the file is in the connected account for the access token you're using, you should certainly be able to use files_download to download it. Feel free to open another question with the details of that issue if you wish.

Related

OpenAI retrieve file content

Unable to retrieve the content of file uploaded already.
Kindly suggest what is going wrong? I have tried for each type of file: search, classification, answers, and fine-tune. Files upload successfully but while retrieving content it shows an error.
import openai
openai.api_key = "sk-bbjsjdjsdksbndsndksbdksbknsndksd" # this is wrong key
# Replace file_id with the file's id whose file content is required
content = openai.File.download("file-5Xs86wEDO5gx8fOitMYArV8r")
print(content)
Error:
Traceback (most recent call last):
File "main.py", line 6, in <module>
content = openai.File.download("file-5Xs86wEDO5gx8fOitMYArV8r")
File "/usr/local/lib/python3.8/dist-packages/openai/api_resources/file.py", line 61, in download
raise requestor.handle_error_response(
openai.error.InvalidRequestError: Not allowed to download files of purpose: classifications
Answer from OpenAI community
Currently, we only allow downloads on the results of fine-tuning runs and not the input files to the fine tuning run. We also don’t allow downloads for search related files. ↗️

Simple PyPDF exercise - AttributeError: 'NullObject' object has no attribute 'get'

Working on a simple PyPDF related exercise - I basically need to take a PDF file and apply a watermark to to it.
Here's my code:
# We need to build a program that will watermark all of our PDF files
# Use the wtr.pdf and apply it to all of the pages of our PDF file
import PyPDF2
# Open the file we want to add the watermark to
with open("combined.pdf", mode="rb") as file:
reader = PyPDF2.PdfFileReader(file)
# Open the watermark file and get the watermark
with open("wtr.pdf", mode="rb") as watermark_file:
watermark_reader = PyPDF2.PdfFileReader(watermark_file)
# Create a writer object for the output file
writer = PyPDF2.PdfFileWriter()
for i in range(reader.numPages):
page = reader.getPage(i)
# Merge the watermark page object into our current page
page.mergePage(watermark_reader.getPage(0))
# Append this new page into our writer object
writer.addPage(page)
with open("watermarked.pdf", mode="wb") as output_file:
writer.write(output_file)
I am unclear as to why I get this error:
$ python watermark.py
Traceback (most recent call last):
File "watermark.py", line 20, in <module>
page.mergePage(watermark_reader.getPage(0))
File "C:\Python38\lib\site-packages\PyPDF2\pdf.py", line 2239, in mergePage
self._mergePage(page2)
File "C:\Python38\lib\site-packages\PyPDF2\pdf.py", line 2260, in _mergePage
new, newrename = PageObject._mergeResources(originalResources, page2Resources, res)
File "C:\Python38\lib\site-packages\PyPDF2\pdf.py", line 2170, in _mergeResources
newRes.update(res1.get(resource, DictionaryObject()).getObject())
AttributeError: 'NullObject' object has no attribute 'get'
I would appreciate any insights. I have been staring at this for a while.
For some reason your pdf file doesn't contain "/Resources". PyPDF2 tries to get it in line 2314 in https://github.com/mstamy2/PyPDF2/blob/master/PyPDF2/pdf.py#L2314
You can try another pdf file to check if the error persists. May be it is a bug in the library or the library doesn't support such files.
Another thing I noticed is that line numbers in master branch of the library do not match line numbers in your stack trace, so may be you need to get more recent version of the library and hope that the problem is fixed there.
By briefly looking at pdf file structure it seems that /Resources are optional. If this is a case, then PyPDF2 doesn't handle this case and it should be probably reported as a bug at https://github.com/mstamy2/PyPDF2/issues

python-pptx: Dealing with password-protected PowerPoint files

I'm using a slightly modified version of the "Extract all text from slides in presentation" example at https://python-pptx.readthedocs.io/en/latest/user/quickstart.html to extract text from some PowerPoint slides.
I'm getting a PackageNotFoundError when I try to use the Presentation() method to open some of the PowerPoint files to read the text.
This appears to be due to the fact that, unbeknownst to me before I started this project, a few of the PowerPoint files are password protected.
I obviously don't expect to be able to read text from a password-protected file but is there a recommended way of dealing with password-protected PowerPoint files? Having my Python script die every time it runs into one is annoying.
I'd be fine with something that basically went: "Hi! The file you're trying to read may be password-protected. Skipping."
I tried using a try/except block to catch the PackageNotFoundError but then I got "NameError: name 'PackageNotFoundError' is not defined".
EDIT1: Here's a minimal case the generates the error:
EDIT2: See below for a working try/catch block, thanks to TheGamer007's suggestion.
import pptx
from pptx import Presentation
password_protected_file = r"C:\Users\J69401\Documents\password_protected_file.pptx"
prs = Presentation(password_protected_file)
And here's the error that is generated:
Traceback (most recent call last):
File "T:/W/Wintermute/50 Sandbox/Pownall/Python/copy files/minimal_case_opening_file.py", line 6, in <module>
prs = Presentation(password_protected_file)
File "C:\Anaconda3\lib\site-packages\python_pptx-0.6.18-py3.6.egg\pptx\api.py", line 28, in Presentation
presentation_part = Package.open(pptx).main_document_part
File "C:\Anaconda3\lib\site-packages\python_pptx-0.6.18-py3.6.egg\pptx\opc\package.py", line 125, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "C:\Anaconda3\lib\site-packages\python_pptx-0.6.18-py3.6.egg\pptx\opc\pkgreader.py", line 33, in from_file
phys_reader = PhysPkgReader(pkg_file)
File "C:\Anaconda3\lib\site-packages\python_pptx-0.6.18-py3.6.egg\pptx\opc\phys_pkg.py", line 32, in __new__
raise PackageNotFoundError("Package not found at '%s'" % pkg_file)
pptx.exc.PackageNotFoundError: Package not found at 'C:\Users\J69401\Documents\password_protected_file.pptx'
Here's the minimal case again but with a working try/catch block.
import pptx
from pptx import Presentation
import pptx.exc
from pptx.exc import PackageNotFoundError
password_protected_file = r"C:\Users\J69401\Documents\password_protected_file.pptx"
try:
prs = Presentation(password_protected_file)
except PackageNotFoundError:
print("PackageNotFoundError generated - possible password-protected file.")

Cannot access file on Samba server via Python

I'm trying to access a file on our Samba server using Python. I found out I need to use a Samba client for this, so I started using PySmbClient. Even though there are many examples online of how to do this, mine just does not want to work. See below.
smb = smbclient.SambaClient(server="192.168.0.320", share="DATA", domain="WORKGROUP",username="admin", password="abc123")
f = smb.open('test.json', 'r')
This produces the following error:
OSError: [Errno 2] No such file or directory
with the following trace:
Traceback (most recent call last):
File "create_dataset.py", line 35, in <module>
f = smb.open('serverSaver.txt', 'r')
File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 408, in open
f = _SambaFile(self, path, mode)
File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 448, in __init__
connection.download(remote_name, self._tmp_name)
File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 393, in download
result = self._runcmd('get', remote_path, local_path)
File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 184, in _runcmd
return self._raw_runcmd(fullcmd)
File "/home/grant/Development/create_dataset/env/local/lib/python2.7/site-packages/smbclient.py", line 168, in _raw_runcmd
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
I've read and implemented many "solutions", but so far nothing has worked for me. I can access the Samba server with the given credentials through my file manager just fine, so I know those values should be fine. I even spoke to our sys admin and he doesn't know what could be wrong.
It must be more than the simple code I wrote. Do you think there's an issue on the server side of things? Something with the values I input into SambaClient? At this point I'm pretty much open to anything that leads to a solution.
Here's some code that works for me, transferring a file from a Linux Samba share to my Windows laptop. It's also known to work fine in the other direction (Linux client, Windows server).
I'm using the pysmb library version 1.1.19 (the latest) and Python 2.7.1.
See the pysmb site for the pysmb package; I actually downloaded and installed it directly from its tarball and setup.py, as pip was throwing an error.
The pysmb package is less user-friendly but it does work well for Windows clients.
I set up a share called "my_share" on the Linux machine for user "edwards" using the following entry in smb.conf:
[my_share]
path = /home/edwards
valid_users = edwards
read only = no
guest ok = yes
browseable = yes
And then used the following code to list the files on the share, and download a file called "rti_license.dat" to my laptop:
import tempfile
import smb
import shutil
from smb.SMBConnection import SMBConnection
share_name = "my_share"
user_name = "edwards"
password = "######" # secret :-)
local_machine_name = "laptop" # arbitrary
server_machine_name = "edwards-Yocto" # MUST match correctly
server_IP = "192.162.2.1" # as must this
# create and establish connection
conn = SMBConnection(user_name, password, local_machine_name, server_machine_name, use_ntlm_v2 = True)
assert conn.connect(server_IP, 139)
# print list of files at the root of the share
files = conn.listPath(share_name, "/")
for item in files:
print item.filename
# check if the file we want is there
sf = conn.getAttributes(share_name, "rti_license.dat")
print sf.file_size
print sf.filename
# create a temporary file for the transfer
file_obj = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
file_name = file_obj.name
file_attributes, copysize = conn.retrieveFile(share_name, "rti_license.dat", file_obj)
print copysize
file_obj.close()
# copy temporary file
shutil.copy(file_name, "rti_license.dat")
# close connection
conn.close()
Note that the server name must be correct or the connection won't work (from a Linux machine it's the output of the hostname command)
Hope this may be useful.

Correct way for uploading image bytes to cloudinary

I am using http://cloudinary.com/documentation/image_upload_api_reference as reference.
There are two cases in which I want to upload the files to cloudinary.
Upload image by directly giving url link.
Upload image bytes by taking them from different source.
I could solve case 1, but had trouble in 2nd. I am pasting my code flow below for reference.
import cloudinary
import cloudinary.uploader
from io import BytesIO
from StringIO import StringIO
def upload_image_to_cloudinary(img_tag):
logging.debug("Uploading Image to cloudinary : %s"%img_tag)
if 'src' not in img_tag.attrs:
del img_tag
return
img_src = img_tag['src']
if img_src.startswith('/blob'):
quip_client = pgquip.get_client()
blob_ids = img_src.split('/')
blob_response = quip_client.get_blob(blob_ids[2], blob_ids[3])
img_src_str = blob_response.read() # this returns str object.
# img_src = BytesIO(img_src_str)
img_src = StringIO(img_src_str)
cloudinary_response = cloudinary.uploader.upload_image(
img_src,
use_filename=True,
folder="/pagalguy/articles",
width=546,
crop="limit"
)
img_tag['src'] = cloudinary_response.metadata.get("url")
return img_tag
In case where img_src is a image blob str returned by another api, I passed it as file param mentioned in cloudinary doc in a very similar way as any external image url for eg: https://media.licdn.com/mpr/mpr/shrinknp_400_400/AAEAAQAAAAAAAAIkAAAAJGRhNzJiYjY1LTUxOTctNDI4NC1hOGIwLWQ1OTVlNmZlZmVmYw.jpg
And, for checking how generic upload flows work like boto for s3, I check below repo code.
Refered https://github.com/boto/boto/blob/develop/boto/vendored/six.py#L633 this too.
Error Log:
Invalid URL for upload
Traceback (most recent call last):
File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/articleslib/article_util.py", line 68, in upload_images_n_publish
tag = image_util.upload_image_to_cloudinary(tag)
File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/api/image_util.py", line 133, in upload_image_to_cloudinary
crop="limit"
File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/libs/cloudinary/uploader.py", line 23, in upload_image
result = upload(file, **options)
File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/libs/cloudinary/uploader.py", line 17, in upload
return call_api("upload", params, file = file, **options)
File "/base/data/home/apps/s~pagalguy-staging/namita:v1.397698162588746989/libs/cloudinary/uploader.py", line 226, in call_api
raise Error(result["error"]["message"])
Error: Invalid URL for upload
Finally I don't know which is the correct way to upload image bytes to cloudinary.
Your img_src parameter, which represents file, should be populated with either a byte array buffer (bytearray) or a Base64 URI. You can try something like:
with open(img_src_str, "rb") as imageFile:
f = imageFile.read()
img_src = bytearray(f)
cloudinary_response = cloudinary.uploader.upload(
img_src,
...
)
That API can upload bytes, so if you are uploading _io.BytesIO, you may just use .getvalue() method to your bytesIO object like uploader.upload(image_stream.getvalue(), public_id = filename)

Categories

Resources