Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Need to take a One Drive file and upload it to a FTP
-
@cvp, I think it is the same for me. But for this, it is enough to open the .py file, not a folder. And the file can be moved to OneDrive by sharing.
-
-
@mikael so, would it be possible to make a script that select two files and upload them to the ftp?
I have would take the data to the folder and then exec the script and upload the files in the same execution?
Following the logic I only had to say the file name because the script would be in the same folder.
I think this is it...
fil = appex.get_file_path()
if fil == None:
print('no file passed')
returnI have to find the code that says the file names, now it shows the document picker but I want two fixed files.
Thanks.
-
@Tito I think that even a script located in your OneDrive folder would not access to other files while OneDrive is not open first as external folder IN Pythonista
I've tried with
import os print(os.listdir())
But that gives a permission error.
-
And giving the link obtained trough files?
shareddocuments:///private/var/mobile/Containers/Shared/AppGroup/...
-
@Tito I guess you could use MS API with token authorization etc....but I don't know how..
-
@Tito said:
And giving the link obtained trough files?
Keep from /private.. and try
os.listdir('/private...')
-
Now I’m open OneDrive app, go to the folder, I select a file, run Pythonista Script and repeat with the other file.
I can use iOS to access the route give it to Pythonista and upload a file. It has to be a way for doing this with a fixed route.
-
@Tito when you share an OneDrive file to send it to Pythonista, iOS creates a temporary copy and its path is not always the same
-
@cvp what a pity... in that case I’m have to do it manually forever.
Thanks for your help. :)
-
-
@cvp I’ve found a post about the compatibility between OneDrive and Shortcuts but seems to be only for scan documents and related stuff nothing related to file routes.
-
@Tito I had tried also a shortcut opening the url, without success.
Another solution could be perhaps web scraping on the OneDrive site...but surely not easy.
-
@Tito I think I have found a solution.
You have to share (in OneDrive app or web page) your folder once and copy the generated link.Then, try this little script, where you have to replace my url with yours
import ui url = 'https://1drv.ms/u/s!AnAqP7kYeMWrgXwwda6qlAmt-bsl?e=wNu7Tb' w = ui.WebView() w.load_url(url) w.present()
You will get a web page where we will still have to find a way to press the download button to get a zip with all files in the folder.
-
@Tito I'll (try to 🙄)do it this evening
-
@Tito Finally, easier that I thought. For each OneDrive file that you need to upload, in the OneDrive app, once, you share it, and 'copy the link'. In the little script here under, you store in the files dict, the file name name and it's pasted link.
Even if the OneDrive file is modified, its link stays the same, at least during my tests.
The script downloads the OneDrive file and copies it locally.
The FTP unload part is another process, if you want help for it, ask me.Based on Generate OneDrive Direct-Download Link with C# or Python
import requests import base64 def create_onedrive_directdownload (onedrive_link): data_bytes64 = base64.b64encode(bytes(onedrive_link, 'utf-8')) data_bytes64_String = data_bytes64.decode('utf-8').replace('/','_').replace('+','-').rstrip("=") resultUrl = f"https://api.onedrive.com/v1.0/shares/u!{data_bytes64_String}/root/content" return resultUrl def main(): files = {'a.py':'https://1drv.ms/u/s!AnAqP7kYeMWrgX9L-9sMT08Fkgkf', 'aa.txt':'https://1drv.ms/t/s!AnAqP7kYeMWrggCb2Umu3Wn_Hr4G'} for nam_file in files: url_file = create_onedrive_directdownload(files[nam_file]) r = requests.get(url_file) with open(nam_file, mode='wb') as fil: fil.write(r.content) if __name__ == '__main__': main()
-
@Tito including SFTP
import base64 import os import paramiko import requests import sys # https://towardsdatascience.com/how-to-get-onedrive-direct-download-link-ecb52a62fee4 def create_onedrive_directdownload (onedrive_link): data_bytes64 = base64.b64encode(bytes(onedrive_link, 'utf-8')) data_bytes64_String = data_bytes64.decode('utf-8').replace('/','_').replace('+','-').rstrip("=") resultUrl = f"https://api.onedrive.com/v1.0/shares/u!{data_bytes64_String}/root/content" return resultUrl def main(): path = sys.argv[0] i = path.rfind('/') path = path[:i+1] ip = 'my ip' user = 'user' pwd = 'password' try: sftp_port = 22 transport = paramiko.Transport((ip, sftp_port)) transport.connect(username=user, password=pwd) sftp = paramiko.SFTPClient.from_transport(transport) files = {'a.py':'https://1drv.ms/u/s!AnAqP7kYeMWrgX9L-9sMT08F', 'aa.txt':'https://1drv.ms/t/s!AnAqP7kYeMWrggCb2Umu3Wn_'} for nam_file in files: url_file = create_onedrive_directdownload(files[nam_file]) r = requests.get(url_file) with open(nam_file, mode='wb') as fil: fil.write(r.content) sftp.put(path + nam_file, nam_file) os.remove(nam_file) sftp.close() transport.close() except Exception as e: print(str(e)) if __name__ == '__main__': main()
-
You could even find automatically the file name from its link
. . . for nam_file in files: r = requests.get(files[nam_file]) c = r.content.decode('utf-8') t = 'property="og:title" content="' i = c.find(t) if i >= 0: # <meta property="og:title" content="a.py"/> j = c.find('"/>',i) print(c[i+len(t):j]) url_file = create_onedrive_directdownload(files[nam_file]) . . .
-
@cvp wow this is really awesome!!
Thanks so much, I will study your code and try to use it on my workflow.
Thanks again.
-
for url in files.values(): text = requests.get(url).text tag = 'property="og:title" content="' _, _, content = text.partition(tag) if content: content, _, _ = content.partition('"/>') print(content) url_file = create_onedrive_directdownload(url)
... or maybe ...
from bs4 import BeautifulSoup
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
content = soup.find("content").text