Python urllib.urlretrive cannot download image to the folder - python

I wan to make a instagram photo downloader using python , i will file all the image url for me and then download it to the folder that i specified .
I manage to create a folder also getting the url , but the image doesnt seem to be like downloading to the folder.
Below is the code
def download_image(self, src, image_filename, folder):
"""
Creates a folder named after a user to to store the image, then downloads the image to the folder.
"""
folder_path = 'C:/{}'.format(folder)
#folder_path = 'C:\\.{}'.format(folder)
if not os.path.exists(folder_path):
os.mkdir(folder_path)
print("folder not create")
img_filename = 'image_{}.jpg'.format(image_filename)
print(str(src)," File Name",str(image_filename)," Folder ",str(folder))
urllib.request.urlretrieve(src, '{}/{}'.format(folder, img_filename))
The output is this
# image url
https://instagram.fkul8-1.fna.fbcdn.net/v/t51.2885-15/sh0.08/e35/c120.0.720.720a/s640x640/19534375_479423379059793_7995041966070956032_n.jpg?_nc_ht=instagram.fkul8-1.fna.fbcdn.net&_nc_cat=109&oh=7c8e2f8816f17627424bf38d0267f39b&oe=5E7DFCA1
# file name is the image name and folder name is kwting3
File Name kwt1 Folder kwting3

Have you tried setting the download file path in your driver initialization? This is usually the accepted method for downloading files to a specific folder.
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Downloads") # specify some other file path here
driver = webdriver.Chrome(chrome_options=options)
You will add this code at the very beginning, where your WebDriver object gets initialized.

Related

python script to upload video ( *.mp4 ) into web portal

I wrote a python script to upload a video file into web portal using selenium library . Our video file name keep changing every hour. program A batch job creates a video with current date and writes to current working directory . Program b ( my python script) needs to upload that to website.
file_input = browser.find_element(By.XPATH, '//*[#id="content"]/input')
abs_path = os.path.abspath("Random.mp4") #"random" this keep changing every hour
file_input.send_keys(abs_path)
I need something like
abs_path = os.path.abspath("*.mp4") #any random filename *.mp4 needs to be uploaded . because it changes everytime. only one video file .mp4 exist at any point of time.
if i give as *.mp4 then python script fails.
Need suggestion to change the script logic.
I have solved the problem by scanning the working directory for any files with extension .mp4. This way it finds the MP4 file and I can extract its (changed) current file name:
video_extensions = ['.mp4', '.avi', '.mkv']
current_directory = os.getcwd()
for root, dirs, files in os.walk(current_directory):
for file in files:
if any(file.endswith(ext) for ext in video_extensions):
file_path = os.path.join(root, file)
file_input = browser.find_element(By.XPATH, '//*[#id="content"]/input')
file_input.send_keys(os.path.abspath(file_path))

No such file or directory but all files in the same folder

I'm getting a
No such file or directory: 'SnP1000_TICKR.csv' error but all my files are in the following folder:
and I'm calling the file here
which is running on this piece of code:
def finVizEngine(input,output):
import chromedriver_autoinstaller
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
driver = webdriver.Chrome()
ipo_df = pd.DataFrame({})
openFinViz()
with open(input, 'r') as IPO_List:
csv_reader = reader(IPO_List)
This was running before, but then I uploaded files to Github and started running the files from vscode instead of pycharm and started to get a load of errors, but honestly don't understand what is wrong. Any help would be amazing,
Best Joao
First check in which folder it runs code
import os
print( os.getcwd() )
cwd means Current Working Directory.
If it runs in different folder then you have script then it also search csv in different folder.
The simplest method is to use "/full/path/to/SnP1000_TICKR.csv".
But more useful method is to get path to folder with script - like this
BASE = os.path.abspath(os.path.dirname(__file__))
and use it to create full path to file csv
input_full_path = os.path.join(BASE, "SnP1000_TICKR.csv")
output_full_path = os.path.join(BASE, "SnP1000_DATA.csv")
finVizEngine(input_full_path, output_full_path)
BTW:
If you will keep csv in subfolder data then you will need
input_full_path = os.path.join(BASE, "data", SnP1000_TICKR.csv")

How to find location of downloaded files on Heroku

I am using YouTube-dl with Python and Flask to download youtube videos and return them using the send_file function.
When running locally I have been using to get the file path:
username = getpass.getuser()
directories = os.listdir(rf'C:\\Users\\{username}')
I then download the video with YouTube-dl:
youtube_dl.YoutubeDL().download([link])
I then search the directory for the file based on the video code:
files = [file for file in directories]
code = link.split('v=')[1]
for file in files:
if file.endswith('.mp4') is True:
try:
code_section = file.split('-')[1].split('.mp4')[0]
if code in code_section:
return send_file(rf'C:\\Users\\{username}\\{file}')
except:
continue
Finally, I return the file:
return send_file(rf'C:\\Users\\{username}\\{file}')
to find the location of the downloaded file, but, on Heroku, this doesn't work - simply the directory doesn't exist. How would I find where the file is downloaded? Is there a function I can call? Or is there a set path it would go to?
Or alternatively, is there a way to set the download location with YouTube-dl?
Since heroku is running Linux and not windows, you could attempt to download your files to your current working directory and then just send it from there.
The main tweak would be setting up some options in your YoutubeDL app:
import os
opts = {
"outtmpl": f"{os.getcwd()}/(title)s.(ext)s"
}
youtube_dl.YoutubeDL(opts).download([link])
That will download the file to your current working directory.
Then you can just upload it from your working directory using return send_file(file).

Python - upload csv file to Dropbox

How to upload csv file to Dropbox with Python
I tried all the examples in this post bellow, neither works
upload file to my dropbox from python script
I am getting error:
FileNotFoundError: [Errno 2] No such file or directory: 'User\pb\Automation\test.csv'
My username: pb
Folder name: Automation
file name: test.csv
import pathlib
import dropbox
import re
# the source file
folder = pathlib.Path("User/pb/Automation") # located in folder
filename = "test.csv" # file name
filepath = folder / filename # path object, defining the file
# target location in Dropbox
target = "Automation" # the target folder
targetfile = target + filename # the target path and file name
# Create a dropbox object using an API v2 key
token = ""
d = dropbox.Dropbox(token)
# open the file and upload it
with filepath.open("rb") as f:
# upload gives you metadata about the file
# we want to overwite any previous version of the file
meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))
# create a shared link
link = d.sharing_create_shared_link(targetfile)
# url which can be shared
url = link.url
# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"\?dl\=0", "?dl=1", url)
print (dl_url)
FileNotFoundError: [Errno 2] No such file or directory: 'User\\pb\\Automation\\test.csv'
The error message is indicating that you're supplying a local path of 'User\pb\Automation\test.csv' but nothing was found at that path on your local filesystem.
Based on the path format, it looks like you're on macOS, but you have the wrong path for accessing your home folder. The path should start with "/", and the home folders are located under "Users" (not "User"), so your folder definition should probably be:
folder = pathlib.Path("/Users/pb/Automation")
Or, use pathlib.Path.home() to automatically expand the home folder for you:
pathlib.Path.home() / "Automation"

Can I create directories with dynamic names on run time using os.mkdir()?

I have to download files from the web over several requests. The downloaded files for each request have to be put in a folder with the same name as the request number.
For example:
My script is now running to download files for request number 87665. So all the downloaded files are to be put in the destination folder Current Download\Attachment87665. So how do I do that?
What I have tried so far:
my_dir = "D:\Current Download"
my_dir = os.path.expanduser(my_dir)
if not os.path.exists(my_dir):
os.makedirs(my_dir)
But it doesn't meet my original requirement. Any idea how to achieve this?
Just create a path beforehand, via os.path.join:
request_number = 82673
# base dir
_dir = "D:\Current Download"
# create dynamic name, like "D:\Current Download\Attachment82673"
_dir = os.path.join(_dir, 'Attachment%s' % request_number)
# create 'dynamic' dir, if it does not exist
if not os.path.exists(_dir):
os.makedirs(_dir)

Categories