-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
185 lines (164 loc) · 5.76 KB
/
server.py
File metadata and controls
185 lines (164 loc) · 5.76 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#samantha and ariana
from flask import Flask, render_template, request, render_template_string, send_file, Response, redirect, url_for, jsonify
from parse import plaintext, get_title, get_author, get_notes, get_paste, get_pokes
from team_converter import export_to_packed, packed_to_json
import logging
import csv
import urllib
import datetime
import requests
csv.field_size_limit(131072*2)
app = Flask(__name__)
log = logging.getLogger(__name__)
method_requests_mapping = {
'GET': requests.get,
'HEAD': requests.head,
'POST': requests.post,
'PUT': requests.put,
'DELETE': requests.delete,
'PATCH': requests.patch,
'OPTIONS': requests.options,
}
@app.route("/")
def index():
return render_template("index.html", title="Pokepost v3")
#return render_template("maintenance.html", title="Pokepost.tk/")
@app.route("/data")
def data():
return send_file('data.csv')
@app.route("/download")
def firefox():
return redirect("https://cdn.glitch.me/c7b08ba3-0914-48ee-91c4-a9884f3b0ce9/RTB.xpi?v=1639684912571", code=302)
@app.route("/a")
def a():
return render_template("a.html")
@app.route("/upload")
def upload():
cookie = request.cookies.get('username')
if cookie == None:
cookie = "guest"
return render_template("upload.html", title = "Upload team", username = cookie)
@app.route("/raw/", methods=['POST'])
def json():
#Moving forward code
print(request.args, flush=True)
pack = str(request.args.get('paste'))
team_json = packed_to_json(pack)
return jsonify(team_json)
@app.route('/post', methods=["POST"])
def json2():
print(request.form, flush=True)
#Moving forward code
requests_function = method_requests_mapping[request.method]
rep = requests.post("https://play.pokemonshowdown.com/~~showdown/action.php", data = request.form)
return rep.text
@app.route("/dn/", methods=['POST'])
def dn():
date = datetime.datetime.now()
err = request.args.get('err')
log = open("log.txt" ,"a")
log.write("\n" + str(date) + " -> " + err)
log.close
return "erreur fatale"
@app.route('/result', methods=["POST", "GET"])
def result():
if request.method == 'POST':
#Gather data from form
date = datetime.datetime.now()
userdata = dict(request.form)
tags = ''
isprivate = "[u'off']"
url = userdata["paste"]
pseudo = request.cookies.get('username')
if pseudo == None:
pseudo = "Random strat"
else:
isprivate = request.form.getlist('private')
pseudo = pseudo.replace(",","/")
if ("pokepast.es/" in url):
json = urllib.request.urlopen(url + "/json").read()
rawpaste = urllib.request.urlopen(url + "/raw").read()
rawpaste = plaintext(rawpaste)
#print(rawpaste, flush=True)
rawjson = plaintext(json)
author = get_author(rawjson).replace(",","/comma/")
title = get_title(rawjson).replace(",","/comma/") + " by " + author
notes = get_notes(rawjson).replace(",","/comma/")
print("author " + author, flush=True)
pass
elif ("pastebin.com" in url):
return "only work with pokepast.es for now :("
else:
print("ELSE", flush=True)
rawpaste = str(url.strip())
notes = "[u'raw']"
title = pseudo + "'s super cool team"
myobj = [('paste',url),('author',pseudo),('title',title)]
url = requests.post("https://pokepast.es/create", data = myobj)
url = url.url
if url == "https://pokepast.es/create":
return "No (or Invalid) Paste"
if userdata["title"] != "":
title = userdata["title"]
print("title", flush=True)
print(title, flush=True)
url = url.split("pokepast.es/")[1]
title = plaintext(title)
gen = (userdata["gen"].replace("u","")).split("|")
print("GEN", flush=True)
tier = userdata["tier"]
format = gen[0] + tier
pack = export_to_packed(rawpaste)
print("PACK", flush=True)
print(pack, flush=True)
pokes = get_pokes(rawpaste)
team = ''
for poke in pokes:
print(">",poke, flush=True)
team = team + poke + "|"
n = 6 - team.count("|")
while n>0:
n = n - 1
team = team + "0|"
tags = "||" + gen[0] + "|" + gen[0] + "|" + tier + "|" + title + "|" + pseudo + "|"
#open file and write data to it
with open('data.csv', mode='a') as csv_file:
data = csv.writer(csv_file, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
data.writerow([url, format, date, pseudo, title, notes, team, isprivate, tags, pack])
return render_template("result.html", title ="Success")
else:
cookie = request.cookies.get('username')
if cookie == "Maxouille":
#open and read data
with open('data.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',')
#set a first line variable so loop doesn't run the first time
first_line = True
#art_data list to house data
art_data = []
for row in data:
#assigning data to the list to later be printed on the html
if not first_line:
art_data.append({
"Url": row[0],
"Format": row[1],
"Date": row[2],
"Author": row[3],
"Title": row[4],
"Notes": row[5],
"Team": row[6],
"isPrivate": row[7],
"Tags": row[8],
"Paste": row[9]
})
#print(art_data, flush=True)
#After the first iteration first_line is set to false
else:
first_line = False
else:
art_data = []
pass
#art = art_data for simplicity in html
return render_template("index2.html", art = art_data, title = "CSV")
if __name__ == "__main__":
app.run(debug=False, port=80, host="192.168.1.23")