Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
config.py
flask_session/*
__pycache__/
__pycache__/
venv/
.env
40 changes: 40 additions & 0 deletions dev-server-start.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@echo off
cd %~dp0

setlocal enabledelayedexpansion

REM Define the length of the random hex string
set "hex_length=128"

REM Initialize the hex characters
set "hex_chars=0123456789abcdef"

REM Variable to store the random hex string
set "random_hex="

REM Loop until the required length is reached
for /L %%i in (1,1,%hex_length%) do (
REM Generate a random number between 0 and 15
set /a "rand=((!random! * !random! + !random! %% 281 * %%i) + !time:~-2!) %% 16"
rem echo !rand!

REM Get the hex character from hex_chars based on the random number
for %%r in (!rand!) do set "random_hex=!random_hex!!hex_chars:~%%r,1!"
)

if not exist "config.py" (
echo #!/bin/env python3 > config.py
echo SECRET_KEY = "%random_hex%" >> config.py
)


if not exist "venv/Scripts/activate.bat" (
python3.exe -m venv venv
)

call venv/Scripts/activate.bat
python3 -m pip install -r requirements.txt

python3 server.py

endlocal
27 changes: 27 additions & 0 deletions dev-server-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/env bash
set -o pipefail
set -e

function main()
{
cd "$(dirname "$0")"
if [[ ! -f "config.py" ]]
then
local SECRET_KEY="$(head -c 4096 /dev/urandom | sha512sum | sed 's/ .*//')"
cat > config.py << EOF
#!/bin/env python3
SECRET_KEY = "$SECRET_KEY"
EOF

fi
if [[ ! -f "venv/bin/activate" ]]
then
python3 -m venv venv
fi
. venv/bin/activate
python3 -m pip install -r requirements.txt

python3 server.py
}

main
37 changes: 37 additions & 0 deletions prod-server-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/env bash
set -o pipefail
set -e

function main()
{
cd "$(dirname "$0")"
if [[ ! -f "config.py" ]]
then
local SECRET_KEY="$(head -c 4096 /dev/urandom | sha512sum | sed 's/ .*//')"
cat > config.py << EOF
#!/bin/env python3
SECRET_KEY = "$SECRET_KEY"
EOF

fi
if [[ ! -f "venv/bin/activate" ]]
then
python3 -m venv venv
fi
. venv/bin/activate
python3 -m pip install -r requirements.txt

if [[ -e .env ]]
then
. .env
fi

if [[ -n "$LISTEN" ]]
then
APP_BIND="--bind $LISTEN"
fi

gunicorn $APP_BIND wsgi:app
}

main
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
flask
flask_session
atproto
argparse
requests
gunicorn
49 changes: 25 additions & 24 deletions server.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/env python3
from flask import Flask, request, render_template, redirect, flash, session
from flask_session import Session
from atproto import Client, models
Expand Down Expand Up @@ -128,44 +129,44 @@ 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);

if (firstPost):
# Sending of the first post, which doesn't reference any post

embed_images = []
facet = parse_facets(client, post)
# print("facets : " + str(facet))

# Send with embed (images)
if (images[0].filename != ""):
embed_images = create_embed_images(client, images, alts, embed_images)

print("Premier post avec une image")
embed = models.AppBskyEmbedImages.Main(images=embed_images)
print("embed : " + str(embed))
Expand All @@ -176,7 +177,7 @@ def send_thread (thread, request):
record=models.AppBskyFeedPost.Main(created_at=client.get_current_time_iso(), text=post, embed=embed, langs=langs, facets=facet),
)
))

# Send without embed (images)
else:
print("Premier post sans image")
Expand All @@ -187,23 +188,23 @@ def send_thread (thread, request):
record=models.AppBskyFeedPost.Main(created_at=client.get_current_time_iso(), text=post, 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:
# Sending of another post, replying to the previous one

embed_images = []
facet = parse_facets(client, post)
# print("facets : " + str(facet))

if (images[0].filename != ""): # If there is images
print("Post avec images")
embed_images = create_embed_images(client, images, alts, embed_images)
embed_images = create_embed_images(client, images, alts, embed_images)
embed = models.AppBskyEmbedImages.Main(images=embed_images)

parent_post_ref = models.create_strong_ref(client.com.atproto.repo.create_record(
models.ComAtprotoRepoCreateRecord.Data(
repo=client.me.did,
Expand All @@ -220,9 +221,9 @@ def send_thread (thread, request):
record=models.AppBskyFeedPost.Main(created_at=client.get_current_time_iso(), text=post, reply=models.AppBskyFeedPost.ReplyRef(parent=parent_post_ref, root=root_post_ref), 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
Expand Down Expand Up @@ -262,7 +263,7 @@ def parse_facets(client:Client, text: str) -> List[Dict]:
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(
{
Expand Down Expand Up @@ -307,7 +308,7 @@ def parse_urls(text: str) -> List[Dict]:
}
)
return spans

def parse_mentions(text: str) -> List[Dict]:
spans = []
# regex based on: https://atproto.com/specs/handle#handle-identifier-syntax
Expand All @@ -321,7 +322,7 @@ def parse_mentions(text: str) -> List[Dict]:
"handle": m.group(1)[1:].decode("UTF-8"),
}
)
return spans
return spans

if __name__ == '__main__':
app.run(debug=True)
app.run(debug=True)
4 changes: 4 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/env python
from server import app
if __name__ == '__main__':
app.run()