PROJET AUTOBLOG


Shaarli - Les discussions de Shaarli

Archivé

Site original : Shaarli - Les discussions de Shaarli du 23/07/2013

⇐ retour index

Utilisation d'HubiC en Python

jeudi 29 août 2013 à 13:37
Famille Michon, le 29/08/2013 à 13:37
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# python 3
#
# By Oros
#
# Licence Public Domaine
#
# apt-get install python3-requests
# http://docs.python-requests.org/en/latest/user/quickstart/

import requests, os, datetime, mimetypes

# /!\ change login and password
data = {"sign-in-email":"YOUR_LOGIN", "sign-in-password":"YOUR_PASSWORD"}


def list_files(hubic):
if hubic['list'] is None:
print("Empty folder")
else:
for f in hubic['list']:
print("{0} ; ".format("File" if f['isFile'] else "Folder")+f['name']+" ; "+f['type']+" ; "+str(f['size']) )


# connection to hubic
s = requests.session()
s.headers['User-Agent']="-"
s.get("https://hubic.com/";)
r = s.post("https://hubic.com/home/actions/nasLogin.php";, data)


# make a test file for upload
file_path='./test.txt'
img = open(file_path,'w')
img.write("Hello world")
img.close()


# upload the test file
dest='/Documents'
r=s.put("https://hubic.com/home/actions/ajax/hubic-browser.php";,  data=open(file_path, 'rb'), headers={ 'X-Action':'upload','X-File-Container':'default','X-File-Dest':dest,'X-File-Modified':datetime.datetime.fromtimestamp(os.stat(file_path)[8]).strftime("%Y-%m-%dT%H:%M:%S"),
'X-File-Name':os.path.basename(file_path),'X-File-Size':os.stat(file_path)[6],'X-File-Type':mimetypes.guess_type(file_path)[0],'X-Requested-With':'XMLHttpRequest',
'Content-Type':'text/plain','Content-Length':os.stat(file_path)[6],'Content-Disposition':'attachment; filename="'+os.path.basename(file_path)+'"','Connection':'keep-alive'}).json()
if r['answer'] is not None:
print("Upload ok.")
else:
print("Upload HS :-( error : "+r['error']['file']['__class']+" ; "+r['error']['file']['message'])


# list files of Documents
data = {"action" : "get","container" : "default","folder" : "/Documents","init" : "false"}
r=s.post("https://hubic.com/home/actions/ajax/hubic-browser.php";, data).json()
list_files(r['answer']['hubic'])


# get first file in Documents
afile=r['answer']['hubic']['list'][0]
r=s.post("https://hubic.com/home/actions/ajax/hubic-browser.php";, {"action":"download","container":"default","isFile0":"true","uri":r['answer']['hubic']['folder']+"/"+afile["name"],"name":afile["name"],"fileCount":1})
# for image, it possible to use :
# r=s.get("https://hubic.com/home/thumbs/original/"+afile["hash"]+"/default"+r['answer']['hubic']['folder']+"/"+afile["name";])
img = open("/tmp/"+afile["name"],'wb')
img.write(r.content)
img.close()


# log off
r=s.get("https://hubic.com/home/actions/logoff.php";)
(Permalink)