-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathputio.py
More file actions
106 lines (75 loc) · 2.87 KB
/
putio.py
File metadata and controls
106 lines (75 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests, json, os
def config():
confDict = dict()
try:
print("Config file found!")
with open(".config", "r") as confFile:
for confLine in confFile:
listOfConf = confLine.replace(" ", "").split(":",1)
confDict.update({listOfConf[0]:listOfConf[1]})
except IOError:
print("No .config file! Let's make one!")
confFile = open(".config", "w")
oAuthToken = raw_input("OAuthToken: ")
confFile.write("oauth: "+oAuthToken)
confDict.update({"oauth":oAuthToken})
confFile.close()
return confDict
def get_filelist(confDict, id):
payload = {'oauth_token':confDict['oauth']}
if id != 0:
payload.update({'parent_id': id})
result = requests.get("https://api.put.io/v2/files/list", params=payload)
parsed = json.loads(result.text)
return parsed
def search(confDict, word):
payload = {'oauth_token':confDict['oauth']}
query = "from:me " + word
payload["query"] = query
result = requests.get("https://api.put.io/v2/files/search/", params=payload)
parsed = json.loads(result.text)
return parsed
def get_infos(confDict,id):
payload = {'oauth_token':confDict['oauth']}
result = requests.get("https://api.put.io/v2/files/"+ str(id) , params=payload)
parsed = json.loads(result.text)
if parsed['status'] == 'OK':
return parsed['file']
else:
return "ERR"
def download_file(confDict, id, path = "."):
payload = {'oauth_token':confDict['oauth']}
url = "https://api.put.io/v2/files/" + str(id) + "/download"
try:
local_file = get_infos(confDict,id)
except requests.exceptions.ConnectionError:
return -1
local_filename = local_file['name']
if not os.path.exists(path):
os.makedirs(path)
r = requests.get(url, params = payload,stream = True)
with open((path+"/"+local_filename), 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
return local_filename
def upload_file(confDict, path_to_file = ".", parent_id=0):
payload = {'oauth_token' : confDict['oauth']}
payload["parent_id"] = parent_id
try:
files = {'file' : open(path_to_file,'rb')}
url = 'https://upload.put.io/v2/files/upload'
result = requests.post(url, params=payload, files= files )
files['file'].close
parsed = json.loads(result.text)
return parsed
except NameError:
return None
def move_file(confDict, file_id, parent_id):
url = 'https://api.put.io/v2/files/move'
payload = {'oauth_token' : confDict['oauth']}
payload.update( {'file_ids': str(file_id), 'parent_id': str(parent_id)})
result = requests.post(url, data=payload)
parsed = json.loads(result.text)
return parsed