Python libtorrent, get file list names - python

I am using libtorrent for python 3.6 . I just want to get any file names that downloaded with a session, e.g. the folder name, the files name etc.
I searched around the web didn't come across anything. I am using the follow example:
https://www.libtorrent.org/python_binding.html
So when the download progress finish, i want to know what files this session downloaded. How can achieve that? Thanks in advance!

Finally found the answer, the code is:
handle = libtorrent.add_magnet_uri(session, magnetLink,params)
session.start_dht()
while not handle.has_metadata():
time.sleep(1)
torinfo = handle.get_torrent_info()
for x in range(torinfo.files().num_files()):
print(torinfo.files().file_path(x))
The code above prints the file names that came with the magnet file.

Related

How to download multiple files wiht the same name with wget in python

I am trying to download multiple files using wget but because they have the same name, each time a file is downloaded it replaces the previously downloaded file.
my current code is:
import wget
for link in links:
wget.download(link ,r'C:\Users\xxx\xxx\xxx\xxx')
Is there a way you can get it to automatically change the name so the next file can be downloaded? It doesn't matter what name. Thanks!
try this, it basically adds a number at the end of each file and thus makes their names different.
for c, link in enumerate(links):
wget.download(link ,r'C:\Users\xxx\xxx\xxx\xxx' + str(c))

how to play mp3 files when a tkinter button is pushed?

I try importing the mp3play module but it says that it doesn't exist. I also don't know how to put mp3 files into the program.
For example:
f = mp3play.load('Sound.mp3'); play = lambda: f.play()
I dont know how to insert the mp3 file into the 'Sound.mp3'.
First of all, here is the mp3play documentation page: https://pypi.org/project/mp3play/ You said that Python returned that the module didn't exist. Are you sure that you installed it correctly on your system. I would run pip install mp3play and verify that it is installed.
I would suggest putting the audio files in the same folder as your code, so that you can keep everything together. If you look at one of the very first example on the docs page I linked above, you can see that the load function takes one paremeter which is the file address. There, you would need to type the full adress of the file, so for example: C:\Documents and Settings\Michael\Desktop\music.mp3, which is the example in the docs page. I think that should solve your problem.
Your code should look something like this:
f = mp3play.load('FULL ADDRESS/Sound.mp3'); play = lambda: f.play()

Read msi with python msilib

I need to read an msi file and make some queries to it. But it looks like despite it is a standard lib for python, it has poor documentation.
To make queries I have to know database schema and I can't find any examples or methods to get it from the file.
Here is my code I'm trying to make work:
import msilib
path = "C:\\Users\\Paul\\Desktop\\my.msi" #I cannot share msi
dbobject = msilib.OpenDatabase(path, msilib.MSIDBOPEN_READONLY)
view = dbobject.OpenView("SELECT FileName FROM File")
rec = view.Execute(None)
r = v.Fetch()
And the rec variable is None. But I can open the MSI file with InstEd tool and see that File is present in the tables list and there are a lot of records there.
What I'm doing wrong?
Your code is suspect, as the last line will throw a NameError in your sample. So let's ignore that line.
The real problem is that view.Execute returns nothing of use. Under the hoods, the MsiViewExecute function only returns success or failure. After you call that, you then need to call view.Fetch, which may be what your last line intended to do.

Python - Reference Redirected Folder

Downloaded files location from Google Chrome is:
\\WALL-E\RedirectedFolders\myname\My Documents\Downloads
How do I reference this?
I cannot reference this in normal way like you do C: Drive.
path='C:\\...'
I think you are doing this in windows. Assuming that what you can do is:
- Go to the Downloads folder in windows.
-Click the addressbar which will give you something like this:
C:\Users\TomHarris\Documents\ViberDownloads
Append the name of your designated file on the last of above.
C:\Users\TomHarris\Documents\ViberDownloads\MYFILETOOPEN
While I think both back-slash and forward-slash should work fine ( I am not sure though, check it!), File input in python follows convention which something like this:
fname= "/WALL-E/RedirectedFolders/myname/My Documents/Downloads"
path='\\\\WALL-E\\RedirectedFolders\\rmarshall\\My Documents\\Downloads\\'

Naming a file when downloading with Selenium Webdriver

I see that you can set where to download a file to through Webdriver, as follows:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
browser = webdriver.Firefox(firefox_profile=fp)
But, I was wondering if there is a similar way to give the file a name when it is downloaded? Preferably, probably not something that is associated with the profile, as I will be downloading ~6000 files through one browser instance, and do not want to have to reinitiate the driver for each download.
I would suggest a little bit strange way: do not download files with the use of Selenium if possible.
I mean get the file URL and use urllib library to download the file and save it to disk in a 'manual' way. The issue is that selenium doesn't have a tool to handle Windows dialogs, such as 'save as' dialog. I'm not sure, but I doubt that it can handle any OS dialogs at all, please correct me I'm wrong. :)
Here's a tiny example:
import urllib
urllib.urlretrieve( "http://www.yourhost.com/yourfile.ext", "your-file-name.ext")
The only job for us here is to make sure that we handle all the urllib Exceptions. Please see http://docs.python.org/2/library/urllib.html#urllib.urlretrieve for more info.
I do not know if there is a pure Selenium handler for this, but here is what I have done when I needed to do something with the downloaded file.
Set a loop that polls your download directory for the latest file that does not have a .part extension (this indicates a partial download and would occasionally trip things up if not accounted for. Put a timer on this to ensure that you don't go into an infinite loop in the case of timeout/other error that causes the download not to complete. I used the output of the ls -t <dirname> command in Linux (my old code uses commands, which is deprecated so I won't show it here :) ) and got the first file by using
# result = output of ls -t
result = result.split('\n')[1].split(' ')[-1]
If the while loop exits successfully, the topmost file in the directory will be your file, which you can then modify using os.rename (or anything else you like).
Probably not the answer you were looking for, but hopefully it points you in the right direction.
Solution with code as suggested by the selected answer. Rename the file after each one is downloaded.
import os
os.chdir(SAVE_TO_DIRECTORY)
files = filter(os.path.isfile, os.listdir(SAVE_TO_DIRECTORY))
files = [os.path.join(SAVE_TO_DIRECTORY, f) for f in files] # add path to each file
files.sort(key=lambda x: os.path.getmtime(x))
newest_file = files[-1]
os.rename(newest_file, docName + ".pdf")
This answer was posted as an edit to the question naming a file when downloading with Selenium Webdriver by the OP user1253952 under CC BY-SA 3.0.

Categories

Resources