-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
115 lines (89 loc) · 3.35 KB
/
main.py
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
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python3
import requests
import argparse
import json
import os, os.path
import pathlib
configTemplate = {
"key": "--"
}
config = configTemplate
pathlib.Path(os.path.expanduser("~")+"/.config").mkdir(parents=True, exist_ok=True)
configPath = os.path.expanduser("~")+"/.config/pastefycli.json"
def paste(title, content, folder=""):
global config
response = requests.post("https://pastefy.ga/api/v2/paste", json={
"content": content,
"title": title,
"folder": folder
}, headers={
"x-auth-key": config["key"]
})
parsedResponse = response.json()
if parsedResponse["success"]:
return parsedResponse["paste"]["id"]
else:
return False
def writeConfig(content):
global config, configPath
with open(configPath, "w") as configFile:
config = {**configTemplate, **config, **content}
json.dump(config, configFile)
if (not os.path.isfile(configPath)):
writeConfig(config)
else:
with open(configPath, "r") as configFile:
config = json.load(configFile)
if __name__ == '__main__':
argParser = argparse.ArgumentParser(description="Pastefy CLI")
argParser.add_argument("--file", "-f", type=str, help="Paste from file")
argParser.add_argument("--title", "-t", type=str, help="Set Paste title")
argParser.add_argument("--contents", "-c", type=str, help="Set Paste contents")
argParser.add_argument("--key", type=str, help="Set your api-key")
argParser.add_argument("--folder", type=str, help="Set folder")
argParser.add_argument("--delete", type=str, help="Deletes a paste")
parsed = argParser.parse_args()
pasteInformation = {
"create": False,
"title": "",
"folder": "",
"content": None
}
if parsed.file is not None:
pasteInformation["create"] = True
pasteInformation["title"] = parsed.file
pasteInformation["content"] = open(parsed.file).read()
if parsed.title is not None:
pasteInformation["title"] = parsed.title
if parsed.contents is not None:
pasteInformation["create"] = True
pasteInformation["content"] = parsed.contents
if parsed.folder is not None:
pasteInformation["folder"] = parsed.folder
if pasteInformation["create"]:
if pasteInformation["content"].strip() == "":
print("Can't paste. Content is empty")
else:
result = paste(pasteInformation["title"], pasteInformation["content"], pasteInformation["folder"])
if isinstance(result, bool) and result is False:
print("Error during pasting")
else:
print("https://pastefy.ga/"+result)
if parsed.key is not None:
writeConfig({"key": parsed.key})
response = requests.get("https://pastefy.ga/api/v2/user", headers={
"x-auth-key": config["key"]
})
parsedResponse = response.json()
if (parsedResponse["logged_in"]):
print("Welcome "+parsedResponse["name"]+"!")
else:
print("Couldn't log in")
if parsed.delete is not None:
response = requests.delete("https://pastefy.ga/api/v2/paste/"+parsed.delete, headers={
"x-auth-key": config["key"]
})
if (response.json()["success"]):
print("Deleted")
else:
print("Couldn't delete")