-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
332 lines (279 loc) · 10.9 KB
/
server.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from flask import Flask, request, render_template, redirect, flash, session
from flask_session import Session
from atproto import Client, models
import math
import os
import argparse
import re
import requests
from typing import Dict, List
app = Flask(__name__)
app.config.from_pyfile('config.py')
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_USE_SIGNER"] = "True"
Session(app)
@app.route('/')
def index():
print('/')
if not session.get("name"):
print("Utilisateur non connecté, chargement de la page de connexion")
return render_template('index.html')
else:
print("Déjà connecté, on va sur /thread")
return redirect('/thread')
@app.route("/login", methods=['GET', 'POST'])
def fctn_login():
client = Client()
print ("/login")
if request.method == 'POST':
login = request.form.get('login')
password = request.form.get('password')
if (connection(client, login, password) == 0):
return redirect('/thread')
else:
error="Connection error, please check the login and password."
return render_template("index.html", error=error)
elif request.method == 'GET':
if not session.get("name"):
print("Utilisateur non connecté, redirection vers la page de connexion")
return redirect('/')
else:
print("Déjà connecté, on va sur /thread")
return redirect('/thread')
# Just in case
return redirect('/')
@app.route("/logout", methods=['GET'])
def logout():
print("- Déconnexion")
session["name"] = None
session["password"] = None
session["profile"] = None
return redirect('/')
@app.route('/thread', methods=['GET', 'POST'])
def thread():
print ('/thread')
thread=[]
if not session.get("name"):
print("Utilisateur non connecté, redirection vers la page de connexion")
return redirect("/")
if request.method == 'POST':
print ('Entrée avec la méthode POST')
print("- Envoi du thread")
# Getting the posts
thread=request.form.getlist("post")
print(thread)
if (session.get("profile") is not None): # If the client has a valid connection to Bluesky
post_url=send_thread(thread, request)
if (post_url != -1):
print("Thread envoyé sur le compte bluesky de " + session.get("name") + " !")
#return render_template('thread_sent.html', post_url=post_url)
return {"status" : 200, "name" : session.get("name"), "url" : post_url};
else :
print("Erreur lors de l'envoi...")
flash ("Error when sending the thread...")
return {"status" : 500};
else:
print("Utilisateur non connecté, envoi impossible")
flash ("User not logged in, the thread could not be sent. Please log in again.")
# Affichage de la page thread
return render_template("thread.html")
@app.errorhandler(404)
def page_not_found(error):
return ('Page not found :(')
# Connects the client using the login and password
# Input : client, login, password
# Returns 0 if connection ok, else returns 1
def connection(client, login, password):
try:
print("- Connexion...")
profile = client.login(login, password)
session["profile"] = profile # Keeps the infos of the Bluesky profile
session["name"] = login
session["password"] = password
except Exception as error:
print("Erreur de connexion. Veuillez vérifier l'identifiant et le mot de passe.", type(error).__name__, error)
session["profile"] = None
session["name"] = None
session["password"] = None
return 1;
else:
print("- Connecté en tant que "+session.get("profile").handle)
return 0;
# Posts the thread on Bluesky
# Input : array of strings, images, form
# Returns the thread first post's url if ok, else returns -1
def send_thread (thread, request):
firstPost=True
str_nb_posts = str(len(thread))
langs = [request.form.get("lang")]
print("nb posts : "+str_nb_posts)
client = Client()
login=session["name"]
password=session["password"]
print("- Envoi_thread : ", thread)
print("form : "+str(request.form));
if (connection(client, login, password) == 0):
for index, post in enumerate(thread):
#print("index : "+str(index))
numerotation = str(index+1)+"/"+str_nb_posts
try: # Trying to get an alt for this post
alts = request.form.getlist("alt"+str(index+1))
#print("alts : ", alts)
except Exception:
alts = "" # no alt for this post
#print("pas de alt récupéré")
#print("input_images"+str(index+1))
images = request.files.getlist("input_images"+str(index+1))
print(images);
embed_images = []
facet = parse_facets(client, post)
embed = None
if (images[0].filename != ""):
embed_images = create_embed_images(client, images, alts, embed_images)
embed = models.AppBskyEmbedImages.Main(images=embed_images)
if (firstPost):
root_post_ref = client.send_post(text=post, embed=embed, langs=langs, facets=facet)
print ("root_post_ref : " + str(root_post_ref))
parent_post_ref = root_post_ref # The first post ref becomes the ref for the parent post
firstPost=False
else:
parent_post_ref = client.send_post(text=post, reply_to=models.AppBskyFeedPost.ReplyRef(parent=models.create_strong_ref(parent_post_ref), root=models.create_strong_ref(root_post_ref)), embed=embed, langs=langs, facets=facet)
print("- Post "+numerotation+" envoyé")
# Once all the thread has been sent : we get the first post's url to return it
post_id = re.match(r"^.*\/(.*)$", root_post_ref.uri).group(1)
post_url = "https://bsky.app/profile/"+session.get("name")+"/post/"+post_id
else:
print("Erreur de connexion du client lors de l'envoi...")
flash ("connection error when trying to send...")
post_url=-1
return post_url
# Uploads the image blobs and returns a embed_images list
def create_embed_images(client, images, alts, embed_images) :
# Loop over the post images to add them to the embed object
for (image_index, image) in enumerate(images):
print("index de l'image : "+str(image_index))
img_data=images[image_index].read()
#print("Juste avant ''upload_blob''")
upload = client.com.atproto.repo.upload_blob(img_data)
#print("Juste après ''upload_blob''")
embed_images.append(models.AppBskyEmbedImages.Image(alt=alts[image_index], image=upload.blob))
return embed_images
def parse_facets(client:Client, text: str) -> List[Dict]:
"""
parses post text and returns a list of app.bsky.richtext.facet objects for any mentions (@handle.example.com) or URLs (https://example.com)
indexing must work with UTF-8 encoded bytestring offsets, not regular unicode string offsets, to match Bluesky API expectations
"""
facets = []
for m in parse_mentions(text):
try:
resp = client.resolve_handle(m["handle"])
print(resp)
except Exception as error: # if handle couldn't be resolved, just skip it! will be text in the post
print("Error trying to resolve handle " + m["handle"] + " :", error)
continue
did = resp["did"]
facets.append(
{
"index": {
"byteStart": m["start"],
"byteEnd": m["end"],
},
"features": [{"$type": "app.bsky.richtext.facet#mention", "did": did}],
}
)
for u in parse_urls(text):
facets.append(
{
"index": {
"byteStart": u["start"],
"byteEnd": u["end"],
},
"features": [
{
"$type": "app.bsky.richtext.facet#link",
# NOTE: URI ("I") not URL ("L")
"uri": u["url"],
}
],
}
)
for h in parse_hashtags(text):
facets.append(
{
"index": {
"byteStart": h["start"],
"byteEnd": h["end"],
},
"features": [
{
"$type": "app.bsky.richtext.facet#tag",
"tag": h["tag"],
}
],
}
)
return facets
def parse_urls(text: str) -> List[Dict]:
spans = []
# partial/naive URL regex based on: https://stackoverflow.com/a/3809435
# tweaked to disallow some training punctuation
url_regex = rb"\b(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(url_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"url": m.group(1).decode("UTF-8"),
}
)
return spans
def parse_mentions(text: str) -> List[Dict]:
spans = []
# regex based on: https://atproto.com/specs/handle#handle-identifier-syntax
mention_regex = rb"(@([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(mention_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"handle": m.group(1)[1:].decode("UTF-8"),
}
)
return spans
def parse_hashtags(text: str) -> List[Dict]:
spans = []
hashtag_regex = rb"(#[^\s]+)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(hashtag_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"tag": m.group(1)[1:].decode("UTF-8").replace("/^#/", ""),
}
)
return spans
locales = {
"ar": "🇸🇦 Arabic",
"zh": "🇨🇳 Chinese",
"en": "🇬🇧 English",
"fr": "🇫🇷 French",
"de": "🇩🇪 German",
"hi": "🇮🇳 Hindi",
"it": "🇮🇹 Italian",
"ja": "🇯🇵 Japanese",
"po": "🇵🇹 Portuguese",
"ru": "🇷🇺 Russian",
"es": "🇪🇸 Spanish",
}
def get_locale():
return request.accept_languages.best_match(locales.keys())
app.jinja_env.globals['get_locale'] = get_locale
def get_locales():
return locales
app.jinja_env.globals['get_locales'] = get_locales
if __name__ == '__main__':
app.run(debug=True)