omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    onedrive ftp
    4
    35
    8833
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Tito
      Tito last edited by

      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.

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @Tito last edited by

        @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

        Tito 1 Reply Last reply Reply Quote 0
        • Tito
          Tito @cvp last edited by

          @cvp what a pity... in that case I’m have to do it manually forever.

          Thanks for your help. :)

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @Tito last edited by

            @Tito said:

            in that case I’m have to do it manually forever.

            Or try via theMS API

            Tito 1 Reply Last reply Reply Quote 0
            • Tito
              Tito @cvp last edited by

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

              cvp 4 Replies Last reply Reply Quote 0
              • cvp
                cvp @Tito last edited by

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

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @Tito last edited by cvp

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

                  1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @Tito last edited by

                    @Tito I'll (try to 🙄)do it this evening

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @Tito last edited by cvp

                      @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()
                      
                      1 Reply Last reply Reply Quote 1
                      • cvp
                        cvp @Tito last edited by cvp

                        @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()
                        
                        1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp last edited by

                          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])
                          .
                          .
                          .
                          
                          1 Reply Last reply Reply Quote 0
                          • Tito
                            Tito last edited by

                            @cvp wow this is really awesome!!

                            Thanks so much, I will study your code and try to use it on my workflow.

                            Thanks again.

                            1 Reply Last reply Reply Quote 0
                            • ccc
                              ccc last edited by ccc

                                      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

                              cvp 3 Replies Last reply Reply Quote 0
                              • cvp
                                cvp @ccc last edited by cvp

                                @ccc I had tried with Eval_js on ui.WebView but there was a lot of "content" and other tries.
                                I'm sure there is a better analysis but my initial challenge was to get the links of the shared files.

                                1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @ccc last edited by

                                  @ccc said:

                                  for url in files.values():

                                  I always forget this function. I'm sure that I become too old and that I'll have to find soon a new hobby 😢

                                  Anyway, as usual, thanks for your advices. Even if I can't use them, other Python beginners will.

                                  1 Reply Last reply Reply Quote 0
                                  • ccc
                                    ccc last edited by

                                    Dude... I am 61 years old... No excuses... Keep pushing ;-)

                                    cvp 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @ccc last edited by

                                      @ccc too late I just bought one

                                      1 Reply Last reply Reply Quote 1
                                      • cvp
                                        cvp @ccc last edited by

                                        @ccc said:

                                        for url in files.values():

                                        But later in the loop, I also use the files key (nam_file), thus...

                                        mikael 1 Reply Last reply Reply Quote 0
                                        • mikael
                                          mikael @cvp last edited by

                                          @cvp

                                          for filename, url in files.items():
                                          

                                          😁😉

                                          1 Reply Last reply Reply Quote 2
                                          • Tito
                                            Tito last edited by

                                            Hi, I’ve been trying but I think this is too complex for my skills.

                                            You’ve been saying you were old guys, I’m felling useless at my almos brand new forties.

                                            I don’t really understand how the function resolves the file and in my procedure I have to deal with an FTP server instead of an SFTP, so I don’t know where to touch the code and switching functions.

                                            Many thanks for your help, and sorry for bothering, I’ll keep trying.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors