-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (90 loc) · 3.66 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
#!/usr/bin/env python3
class main:
def clear(self):
self.headersOutput.delete('1.0', 'end')
self.curlEntry.delete('1.0', 'end')
self.cookiesOutput.delete('1.0', 'end')
def copy(self):
self.app.clipboard_clear()
if self.cookieVal is None:
clipboard = f'self.headers = {self.convertedVal}'
else:
clipboard = f'self.headers = {self.convertedVal}\n\nself.cookies = {self.cookieVal}'
self.app.clipboard_append(clipboard)
self.app.update()
def convert(self):
curlVal = self.curlEntry.get('1.0', 'end')
try:
if '--data-raw' in curlVal:
curlVal = curlVal.split(' --data-raw ')[0]
mode = self.mode.get()
match mode:
case 'firefox':
self.convertedVal = ast.literal_eval('{' + ',\n'.join(curlVal.split(' -H ')[1:]).replace(': ', "': '") + '}')
if 'Cookie' in self.convertedVal:
self.convertedVal.pop('Cookie')
case 'chrome':
self.convertedVal = ast.literal_eval('{' + ',\n'.join(curlVal.split(' -H ')[1:]).replace(': ', "': '") + '}')
if 'cookie' in self.convertedVal:
self.convertedVal.pop('cookie')
case _:
self.convertedVal = 'WHAT??? N/AW AINT NO WAY FR FR ONG BRUH'
if "-H 'cookie: " in curlVal:
cookieHeader = "-H 'cookie: "
elif "-H 'Cookie: " in curlVal:
cookieHeader = "-H 'Cookie: "
else:
cookieHeader = None
if cookieHeader is not None:
self.cookieVal = json.dumps(ast.literal_eval("{'" + "',\n'".join(curlVal.split(cookieHeader)[1].split(' -H ')[0].split('; ')).replace('=', "': '") + "}"), indent='\t')
else:
self.cookieVal = None
self.convertedVal = json.dumps(self.convertedVal, indent='\t')
except SyntaxError as syntaxError:
self.convertedVal = 'Error!'
self.cookieVal = 'Error!'
self.headersOutput.delete('1.0', 'end')
self.headersOutput.insert('end', self.convertedVal)
self.cookiesOutput.delete('1.0', 'end')
self.cookiesOutput.insert('end', str(self.cookieVal))
def __init__(self):
self.app = ttk.Window(themename = 'vapor')
#self.app.iconphoto(False, ttk.PhotoImage(file = '~/code/python/curl2Headers/jeff.png'))
self.app.title('Convert dev tools copy as curl to headers and cookies dictionary to be used in a python program for web scrapping etc')
self.app.geometry('1300x600')
self.app.resizable(False, False)
self.convertedVal = None
labelFrame = ttk.Frame(self.app, padding=5, width=500)
convertButton = ttk.Button(labelFrame, text='Convert', command=self.convert)
convertButton.pack(padx = 5, side='left')
copyButton = ttk.Button(labelFrame, text='Copy', command=self.copy)
copyButton.pack(padx = 5, side='left')
clearButton = ttk.Button(labelFrame, text='Clear', command=self.clear)
clearButton.pack(padx = 5, side='left')
modes = [
'firefox',
'firefox',
'chrome',
]
self.mode = ttk.StringVar()
# self.mode.set('firefox')
modeSelect = ttk.OptionMenu(labelFrame, self.mode, *modes)
modeSelect.pack(padx = 5, side='left')
labelFrame.pack()
curlEntry = ttk.Labelframe(self.app, text='Curl Input:', padding=1)
headersOutput = ttk.Labelframe(self.app, text='Headers: ', padding=1)
cookiesOutput = ttk.Labelframe(self.app, text='Cookies: ', padding=1)
self.curlEntry = ttk.Text(curlEntry, width=50, height=30)
self.curlEntry.pack(side='left', padx=10, pady=10)
self.headersOutput = ttk.Text(headersOutput, width=50, height=30)
self.headersOutput.pack(side='left', padx=10, pady=10)
self.cookiesOutput = ttk.Text(cookiesOutput, width=50, height=30)
self.cookiesOutput.pack(side='left', padx=10, pady=10)
curlEntry.pack(side='left')
headersOutput.pack(side='left')
cookiesOutput.pack(side='left')
self.app.mainloop()
if __name__ == '__main__':
import ttkbootstrap as ttk
import json, ast
main()