How to find location of downloaded files on Heroku - python

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).

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")

PyInstaller: problems on update a json file from an EXE program file (-in the next relunch, json file it's not been update)

I'm testing an EXE made by pyinstaller.
In the project folder there is folder named config, which contains a json file where the user stores all the information he want about -for the GUI im using tkinter-
But finally after I restart this application and reopen the json file, it's appearing the original file.
I read about to create a new folder in execution time, where I put the origina json file. But i'm not properly satisfied with this solution.
Please any help would be appreciated
Update:
Here is the project structure:
/config
|----config.json
/modules
|----admin
|----core
|----graphwo
init.py
The code execute well, except that I want user save their info inside the config.json file in other word, in execution time. But because the PyInstaller I've used is --onefile that's not permiting to update the config.json file
Update II:
Also I have this code which gets the current path at execution time of each file -images, data and json files- the application needs:
def getPathFileAtExecution(relative):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative)
I trace any steps of the program when it's calling the json file for read and write. But after it finishes and restart againg, all changes made previously are not reflected.
May is more clear now?
First check that you pointing into a right path. Some operating systems responds differently for some system variables and function calls.
import sys
import os
if getattr(sys,'frozen',False):
current_path = os.path.dirname(sys.executable)
else:
current_path = os.path.dirname(os.path.realpath(__file__))
config_json_file_path = os.path.join(current_path, 'config', 'config.json')
print config_json_file_path
import os
import sys
if getattr(sys, 'frozen', False):
# we are running in a |PyInstaller| bundle
base_path = sys._MEIPASS
extDataDir = os.getcwd()
print base_path
print extDataDir
else:
# we are running in a normal Python environment
base_path = os.getcwd()
extDataDir = os.getcwd()
The sys._MEIPASS variable is where your app's bundled files are run while your program is running. This is different from where your application lives. In order for your program to find and manipulate that non bundled .json file I have used os.getcwd() to get the folder where your application lives.
The os.getcwd() gets the current working directory that your executable is in. Then if your .json file is in a folder called config and that folder is in the current working directory of where your exe is run from, you would use
ext_config = os.path.join(extDataDir, 'config', 'your.json')

Issue while uploading file with ftputil

I am struggling to upload file to my FTP server. Please advise what is wrong in code below:
host: someserver.com
path: ./my_folder/at_this_server
target: 'test.pdf'
with ftputil.FTPHost(ftp_settings['host'],
ftp_settings['user'],
ftp_settings['password'],
ftp_settings['port']) as ftp_host:
safe_chdir(ftp_host, ftp_settings['path']) # change FTP dir
ftp_host.upload_if_newer('local_test.pdf', 'test.pdf')
There is successfully execute the command upload_if_newer() or upload(), but I don't see any uploaded file to the FTP folder.
UPDATE
I found that file is uploading to host+"/my_folder" only instead of host+"/my_folder/at_this_server".
1) Check the result of ftp_host.upload_if_newer('local_test.pdf', 'test.pdf'). If it is True then the file was copied.
2) Are you sure that safe_chdir function is correct? You could check that current directory on FTP changed using ftp_host.getcwd(). Try to upload file using full path instead of changing FTP dir.
3) Check access rights.

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