many changes for spc
This commit is contained in:
parent
1d98f9f4f1
commit
f525310f1c
|
@ -1,3 +1,4 @@
|
||||||
__pycache__
|
__pycache__
|
||||||
list
|
list
|
||||||
token
|
token
|
||||||
|
./*.png
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
cd /var/lib/bots/vcs/int/mastoes-bots
|
||||||
|
/var/lib/bots/venvs/faggotree/bin/python3 feditree.py
|
86
feditree.py
86
feditree.py
|
@ -1,8 +1,16 @@
|
||||||
from PIL import Image
|
from PIL import Image, ImageFile
|
||||||
from common import get_api, get_new_notifications, list_read, list_append
|
from common import get_api, get_new_notifications, list_read, list_append
|
||||||
import datetime
|
import datetime
|
||||||
import gettext
|
import gettext
|
||||||
import requests
|
import requests
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||||
|
|
||||||
|
bot_name = "faggotree"
|
||||||
|
|
||||||
coordinates = [
|
coordinates = [
|
||||||
(350,200),
|
(350,200),
|
||||||
|
@ -17,44 +25,72 @@ coordinates = [
|
||||||
(575,650),
|
(575,650),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def generate_random_filename():
|
||||||
|
length = random.randint(10, 15) # Random length between 5-15 characters
|
||||||
|
chars = string.ascii_lowercase + string.digits
|
||||||
|
filename = ''.join(random.choice(chars) for _ in range(length))
|
||||||
|
return filename + '.png'
|
||||||
|
|
||||||
|
temp_image_name = generate_random_filename()
|
||||||
|
print("generated random image filename: " + temp_image_name)
|
||||||
|
|
||||||
def get_ordered_accounts_ids(account_id):
|
def get_ordered_accounts_ids(account_id):
|
||||||
|
print("getting ordered accounts ids")
|
||||||
current_year = datetime.date.today().year
|
current_year = datetime.date.today().year
|
||||||
accounts = {}
|
accounts = {}
|
||||||
stop = False
|
stop = False
|
||||||
max_id=None
|
max_id=None
|
||||||
loops = 0
|
loops = 0
|
||||||
while stop == False:
|
while stop == False:
|
||||||
statuses = api.account_statuses(account_id, exclude_reblogs=True, max_id=max_id, limit=80)
|
statuses = api.account_statuses(account_id, exclude_reblogs=True, max_id=max_id, limit=40)
|
||||||
for status in statuses:
|
for status in statuses:
|
||||||
if(status.created_at.year < current_year):
|
if(status.created_at.year < current_year):
|
||||||
stop = True
|
stop = True
|
||||||
break
|
break
|
||||||
|
print("mentions: " + str(len(status.mentions)))
|
||||||
for mention in status.mentions:
|
for mention in status.mentions:
|
||||||
if mention.id not in accounts:
|
print(mention.username)
|
||||||
accounts[mention.id] = 1
|
if mention.username == bot_name or mention.username == status.account.username:
|
||||||
|
print("skipping: " + mention.username)
|
||||||
else:
|
else:
|
||||||
accounts[mention.id] = accounts[mention.id] + 1
|
if mention.id not in accounts:
|
||||||
|
accounts[mention.id] = 1
|
||||||
|
else:
|
||||||
|
accounts[mention.id] = accounts[mention.id] + 1
|
||||||
max_id=status.id
|
max_id=status.id
|
||||||
loops = loops + 1
|
loops = loops + 1
|
||||||
if loops > 10 :
|
if loops > 10 :
|
||||||
stop = True
|
stop = True
|
||||||
|
print("stopping loop")
|
||||||
accounts_list = sorted(accounts, key=accounts.get, reverse=True)
|
accounts_list = sorted(accounts, key=accounts.get, reverse=True)
|
||||||
accounts_list.insert(0,account_id)
|
accounts_list.insert(0,account_id)
|
||||||
accounts_list.pop
|
accounts_list.pop
|
||||||
return accounts_list
|
return accounts_list
|
||||||
|
|
||||||
def get_accounts(accounts_ids):
|
def get_accounts(accounts_ids):
|
||||||
|
print("getting accounts: " + str(len(accounts_ids)))
|
||||||
accounts = []
|
accounts = []
|
||||||
for i in range(len(accounts_ids)):
|
for i in range(len(accounts_ids)):
|
||||||
account = api.account(accounts_ids[i])
|
account = api.account(accounts_ids[i])
|
||||||
if "nobot" not in account.note:
|
accounts.append(account)
|
||||||
accounts.append(account)
|
|
||||||
if len(accounts) == len(coordinates):
|
if len(accounts) == len(coordinates):
|
||||||
break
|
break
|
||||||
return accounts
|
return accounts
|
||||||
|
|
||||||
|
def get_avatar():
|
||||||
|
try:
|
||||||
|
print("trying to open: " + temp_image_name)
|
||||||
|
with Image.open(temp_image_name) as avatar:
|
||||||
|
avatar.load()
|
||||||
|
return avatar
|
||||||
|
except:
|
||||||
|
print("failed to use avatar so using a default")
|
||||||
|
with Image.open('feditree/default.png') as avatar:
|
||||||
|
avatar.load()
|
||||||
|
return avatar
|
||||||
|
|
||||||
def create_image(accounts):
|
def create_image(accounts):
|
||||||
|
print("Creating image now")
|
||||||
with Image.open("feditree/fediverse-christmas-tree.png") as feditree:
|
with Image.open("feditree/fediverse-christmas-tree.png") as feditree:
|
||||||
feditree.load()
|
feditree.load()
|
||||||
with Image.open("feditree/bola_radio.png") as bola_radio:
|
with Image.open("feditree/bola_radio.png") as bola_radio:
|
||||||
|
@ -65,10 +101,9 @@ def create_image(accounts):
|
||||||
account = api.account(accounts[i])
|
account = api.account(accounts[i])
|
||||||
avatar_url = account.avatar_static
|
avatar_url = account.avatar_static
|
||||||
avatar_data = requests.get(avatar_url).content
|
avatar_data = requests.get(avatar_url).content
|
||||||
with open('feditree/avatar.png', 'wb') as handler:
|
with open(temp_image_name, 'wb') as handler:
|
||||||
handler.write(avatar_data)
|
handler.write(avatar_data)
|
||||||
with Image.open('feditree/avatar.png') as avatar:
|
avatar = get_avatar()
|
||||||
avatar.load()
|
|
||||||
avatar = avatar.resize((100,100)).convert("RGB")
|
avatar = avatar.resize((100,100)).convert("RGB")
|
||||||
avatar.paste(bola_radio, (0,0), bola_radio)
|
avatar.paste(bola_radio, (0,0), bola_radio)
|
||||||
feditree.paste(avatar, coordinates[i], bola_mask)
|
feditree.paste(avatar, coordinates[i], bola_mask)
|
||||||
|
@ -85,32 +120,43 @@ def create_image(accounts):
|
||||||
return api.media_post("feditree/feditree.png", description=description)
|
return api.media_post("feditree/feditree.png", description=description)
|
||||||
|
|
||||||
|
|
||||||
bot_name = "feditree"
|
|
||||||
localedir = './locales'
|
localedir = './locales'
|
||||||
api = get_api('masto.es', bot_name)
|
api = get_api('shitposter.world', bot_name)
|
||||||
|
print("getting notifications")
|
||||||
notifications = get_new_notifications(api, bot_name, ["mention"])
|
notifications = get_new_notifications(api, bot_name, ["mention"])
|
||||||
|
print("getting previous ids")
|
||||||
previous_ids = list_read(bot_name + "_previous_ids")
|
previous_ids = list_read(bot_name + "_previous_ids")
|
||||||
|
|
||||||
|
print("to handle: " + str(len(notifications)))
|
||||||
for notification in notifications:
|
for notification in notifications:
|
||||||
i18n = gettext.translation(bot_name, localedir, fallback=True, languages=[notification.status.language])
|
i18n = gettext.translation(bot_name, localedir, fallback=True, languages=['en'])
|
||||||
i18n.install()
|
i18n.install()
|
||||||
|
print("handling for: " + notification.account.username)
|
||||||
try:
|
try:
|
||||||
if str(notification.account.id) in previous_ids:
|
if str(notification.account.id) in previous_ids:
|
||||||
status = "@" + notification.account.acct + " "
|
status = "@" + notification.account.acct + " "
|
||||||
status += _("I have already generated a feditree for you this year. Try again next year!")
|
status += _("I have already generated a faggotree for you this year. Try again next year!")
|
||||||
api.status_post(status, visibility="direct", in_reply_to_id=notification.status.id)
|
api.status_post(status, visibility="direct", in_reply_to_id=notification.status.id)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
list_append(bot_name + "_previous_ids", previous_ids)
|
list_append(bot_name + "_previous_ids", str(notification.account.id))
|
||||||
accounts_ids = get_ordered_accounts_ids(notification.account.id)
|
accounts_ids = get_ordered_accounts_ids(notification.account.id)
|
||||||
accounts = get_accounts(accounts_ids)
|
accounts = get_accounts(accounts_ids)
|
||||||
image = create_image(accounts)
|
image = create_image(accounts)
|
||||||
status = _("These are the people who have adorned the #FediTree of") + " @" + notification.account.acct + ":"
|
status = _("These are the fags who have adorned the #FaggoTree of") + " @" + notification.account.acct + ":\n\n"
|
||||||
for account in accounts:
|
for account in accounts:
|
||||||
if account.acct == notification.account.acct:
|
status += " " + account.acct + "\n"
|
||||||
continue
|
|
||||||
status += " @/" + account.acct
|
|
||||||
api.status_post(status, media_ids=image, visibility="unlisted", in_reply_to_id=notification.status.id)
|
api.status_post(status, media_ids=image, visibility="unlisted", in_reply_to_id=notification.status.id)
|
||||||
previous_ids.append(notification.account.id)
|
previous_ids.append(notification.account.id)
|
||||||
except:
|
except Exception as e:
|
||||||
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||||
|
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
||||||
|
print(exc_type, fname, exc_tb.tb_lineno)
|
||||||
|
print(e)
|
||||||
api.status_post(_("An error ocurred. Please try again or contact my creator"), visibility="direct", in_reply_to_id=notification.status.id)
|
api.status_post(_("An error ocurred. Please try again or contact my creator"), visibility="direct", in_reply_to_id=notification.status.id)
|
||||||
|
|
||||||
|
print("deleting temp image file")
|
||||||
|
try:
|
||||||
|
os.remove(temp_image_name)
|
||||||
|
except Exception:
|
||||||
|
print(f"File {temp_image_name} does not exist")
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 7.0 KiB |
Binary file not shown.
Before Width: | Height: | Size: 196 KiB After Width: | Height: | Size: 206 KiB |
|
@ -48,9 +48,9 @@ msgstr ""
|
||||||
|
|
||||||
#: feditree.py:98
|
#: feditree.py:98
|
||||||
msgid ""
|
msgid ""
|
||||||
"I have already generated a feditree for you this year. Try again next year!"
|
"I have already generated a faggotree for you this year. Try again next year!"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: feditree.py:103
|
#: feditree.py:103
|
||||||
msgid "These are the people who have adorned the #FediTree of"
|
msgid "These are the fags who have adorned the #FaggoTree of"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
Loading…
Reference in New Issue