Skip to content

Commit ab5095a

Browse files
committed
Fixed all identified bugs
1 parent 5d4af3b commit ab5095a

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
db.json
33
db.json.lock
4+
__pycache__/

main.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class database_:
1818
def __init__(self):
1919
self.db = pysondb.db.getDb("db.json")
2020
def write(self, some: dict) -> int:
21-
if self.db.getBy(some) == []:
21+
if self.db.getByQuery(some) == []:
2222
self.db.add(some)
2323
return 0
2424
return 1
@@ -132,7 +132,6 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
132132

133133
## SENDING ##
134134
def html2md(text: str) -> str:
135-
out = ""
136135
i = 0
137136
while i < len(text):
138137
toReplace = ""
@@ -141,20 +140,21 @@ def html2md(text: str) -> str:
141140
while i < len(text) and text[i] != ">":
142141
toReplace += text[i]
143142
i += 1
144-
if i >= len(text):
145-
out += toReplace
146-
else:
147-
tag = toReplace.split()[0]
143+
if i < len(text):
144+
tag = toReplace.split()[0][1:].replace("/", "")
145+
toReplace += ">"
146+
print(toReplace)
148147
match tag:
149-
case "p":
150-
text.replace(toReplace, "")
151-
text.replace("</p>", "")
152148
case "a":
153-
text.replace(toReplace, "*")
154-
text.replace("</a>", "*")
155-
else: out += text[i]
156-
i += 1
157-
return out
149+
text = text.replace(toReplace, "*")
150+
case "br":
151+
text = text.replace(toReplace, "\n")
152+
case _:
153+
text = text.replace(toReplace, "")
154+
i -= len(toReplace)-1
155+
else:
156+
i += 1
157+
return text
158158

159159
from time import sleep
160160
def sender():
@@ -164,7 +164,6 @@ def sender():
164164
users = db.get()
165165
for user in users:
166166
sleep(1) ### based on Mastodon rate limit
167-
Channel = user["tg_channel_id"]
168167
Id = user["mastodon_id"]
169168
Instance = user["mastodon_instance"]
170169
User = user["mastodon_name"]+"_"+Instance
@@ -173,8 +172,8 @@ def sender():
173172
post["id"] = int(post["id"])
174173
if User in ids.keys() and post["id"] > ids[User]:
175174
ids[User] = post["id"]
176-
postContent = post["content"]
177-
postContent += "\n\n"+post["url"]
175+
postContent = html2md(post["content"]+"\n\n"+post["url"])
176+
media = []
178177
if len(post["media_attachments"]) > 0:
179178
hasPhoto = False
180179
for m in post["media_attachments"]:
@@ -185,14 +184,21 @@ def sender():
185184
media.append(InputMediaPhoto(m["url"]))
186185
else:
187186
media.append(InputMediaDocument(m["url"]))
188-
asyncio.run(application.bot.send_media_group(Channel, media, caption=html2md(postContent), parse_mode="Markdown"))
189-
else:
190-
asyncio.run(application.bot.send_message(Channel, html2md(postContent), parse_mode="Markdown"))
187+
for u in db.get({"tg_user_id": user["tg_user_id"]}):
188+
for _ in range(3):
189+
try:
190+
if len(media) > 0:
191+
asyncio.run(application.bot.send_media_group(u["tg_channel_id"], media, caption=postContent, parse_mode="Markdown"))
192+
break
193+
else:
194+
asyncio.run(application.bot.send_message(u["tg_channel_id"], postContent, parse_mode="Markdown"))
195+
break
196+
except: pass
191197
elif not User in ids.keys():
192198
ids[User] = post["id"]
193199
except Exception as e:
194-
print(e)
195-
sleep(1)
200+
print(e)
201+
sleep(1)
196202

197203
## MAIN ##
198204
from dotenv import load_dotenv

tester.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import main
2+
3+
print(main.html2md("<span kjsadkjkaosdj=sakld><a sjadklks>b</a></span>"))

0 commit comments

Comments
 (0)