Comparar commits
1 commit
Autor | SHA1 | Fecha | |
---|---|---|---|
|
fe9215c70a |
59
corpus.py
59
corpus.py
|
@ -1,7 +1,6 @@
|
||||||
from mastodonbot import Mastodonbot
|
from mastodonbot import Mastodonbot
|
||||||
from mastodon import MastodonNetworkError
|
from mastodon import MastodonNetworkError
|
||||||
from database import Database
|
from database import Database
|
||||||
from datetime import datetime, time
|
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import pdb
|
import pdb
|
||||||
|
@ -35,7 +34,13 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
for notif in notifications:
|
for notif in notifications:
|
||||||
|
|
||||||
if notif.type == 'mention':
|
if notif.type != 'mention' and notif.type != 'status':
|
||||||
|
|
||||||
|
print(f"Dismissing notification id {notif.id}")
|
||||||
|
|
||||||
|
bot.mastodon.notifications_dismiss(notif.id)
|
||||||
|
|
||||||
|
elif notif.type == 'mention':
|
||||||
|
|
||||||
mention = bot.get_data(notif)
|
mention = bot.get_data(notif)
|
||||||
|
|
||||||
|
@ -143,13 +148,18 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
bot.mastodon.status_post(f'@{mention.acct}, error al esborrar tots els tuts.', in_reply_to_id=mention.status_id, visibility='direct')
|
bot.mastodon.status_post(f'@{mention.acct}, error al esborrar tots els tuts.', in_reply_to_id=mention.status_id, visibility='direct')
|
||||||
|
|
||||||
elif notif.type == 'status':
|
|
||||||
|
|
||||||
if notif.status != None:
|
print(f"Dismissing notification id {mention.id}")
|
||||||
|
|
||||||
if 'visibility' in notif.status:
|
bot.mastodon.notifications_dismiss(mention.id)
|
||||||
|
|
||||||
if notif.status.visibility == 'public' and notif.status.language == 'ca':
|
else:
|
||||||
|
|
||||||
|
print(f"Dismissing notification id {mention.id}")
|
||||||
|
|
||||||
|
bot.mastodon.notifications_dismiss(mention.id)
|
||||||
|
|
||||||
|
elif notif.type == 'status' and notif.status.visibility == 'public' and notif.status.language == 'ca':
|
||||||
|
|
||||||
found_it, pending = db.check_user(notif.account.acct)
|
found_it, pending = db.check_user(notif.account.acct)
|
||||||
|
|
||||||
|
@ -160,40 +170,3 @@ if __name__ == '__main__':
|
||||||
print(f"Dismissing notification id {notif.id}")
|
print(f"Dismissing notification id {notif.id}")
|
||||||
|
|
||||||
bot.mastodon.notifications_dismiss(notif.id)
|
bot.mastodon.notifications_dismiss(notif.id)
|
||||||
|
|
||||||
now = datetime.now()
|
|
||||||
|
|
||||||
print(now.hour, now.minute)
|
|
||||||
|
|
||||||
if now.hour == 18 or now.hour == 0 or now.hour == 6 or now.hour == 12:
|
|
||||||
|
|
||||||
if now.minute == 0:
|
|
||||||
|
|
||||||
post = 'Vols ajudar fàcilment a desenvolupar i millorar les tecnologies de llengua per el #català?\n'
|
|
||||||
|
|
||||||
post += "Només cal demanar-me l'alta i començaré a enregistrar tots els teus tuts públics. "
|
|
||||||
|
|
||||||
post += "Els teus tuts públics podran alimentar projectes lingüistics com per exemple el de Common Voice de #Mozilla.\n"
|
|
||||||
|
|
||||||
post += "Com donar-te d'alta?\n\n@corpus alta"
|
|
||||||
|
|
||||||
print(post)
|
|
||||||
|
|
||||||
bot.mastodon.status_post(post, in_reply_to_id=None, visibility='public')
|
|
||||||
|
|
||||||
elif now.hour == 20:
|
|
||||||
|
|
||||||
if now.minute == 0:
|
|
||||||
|
|
||||||
total_users = db.total_users()
|
|
||||||
|
|
||||||
total_posts = db.total_posts()
|
|
||||||
|
|
||||||
post = f"\nUsuaris donats d'alta: {total_users}\n"
|
|
||||||
|
|
||||||
post += f'Missatges públics cedits: {total_posts}\n'
|
|
||||||
|
|
||||||
print(post)
|
|
||||||
|
|
||||||
bot.mastodon.status_post(post, in_reply_to_id=None, visibility='public')
|
|
||||||
|
|
||||||
|
|
221
database.py
221
database.py
|
@ -249,221 +249,6 @@ class Database():
|
||||||
|
|
||||||
return are_deleted
|
return are_deleted
|
||||||
|
|
||||||
def users(self):
|
|
||||||
|
|
||||||
user_list = []
|
|
||||||
|
|
||||||
sql = "select distinct username from corpus order by 1 asc"
|
|
||||||
|
|
||||||
conn = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
conn = psycopg2.connect(database = self.corpus_db, user = self.corpus_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
cur.execute(sql)
|
|
||||||
|
|
||||||
rows = cur.fetchall()
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
|
|
||||||
user_list.append(row)
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
|
||||||
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
|
|
||||||
if conn is not None:
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return user_list
|
|
||||||
|
|
||||||
def user_posts(self, username):
|
|
||||||
|
|
||||||
posts = []
|
|
||||||
|
|
||||||
sql = "select text from corpus where username = (%s)"
|
|
||||||
|
|
||||||
conn = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
conn = psycopg2.connect(database = self.corpus_db, user = self.corpus_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
cur.execute(sql, (username,))
|
|
||||||
|
|
||||||
rows = cur.fetchall()
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
|
|
||||||
posts.append(row)
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
|
||||||
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
|
|
||||||
if conn is not None:
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return posts
|
|
||||||
|
|
||||||
def total_users(self):
|
|
||||||
|
|
||||||
total_posts = 0
|
|
||||||
|
|
||||||
sql = "select count(username) from users"
|
|
||||||
|
|
||||||
conn = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
conn = psycopg2.connect(database = self.corpus_db, user = self.corpus_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
cur.execute(sql)
|
|
||||||
|
|
||||||
row = cur.fetchone()
|
|
||||||
|
|
||||||
if row != None:
|
|
||||||
|
|
||||||
total_users = row[0]
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
|
||||||
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
|
|
||||||
if conn is not None:
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return total_users
|
|
||||||
|
|
||||||
def total_posts(self):
|
|
||||||
|
|
||||||
total_posts = 0
|
|
||||||
|
|
||||||
sql = "select count(text) from corpus"
|
|
||||||
|
|
||||||
conn = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
conn = psycopg2.connect(database = self.corpus_db, user = self.corpus_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
cur.execute(sql)
|
|
||||||
|
|
||||||
row = cur.fetchone()
|
|
||||||
|
|
||||||
if row != None:
|
|
||||||
|
|
||||||
total_posts = row[0]
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
|
||||||
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
|
|
||||||
if conn is not None:
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return total_posts
|
|
||||||
|
|
||||||
def export(self, username, post, lt_errors, language):
|
|
||||||
|
|
||||||
is_saved = False
|
|
||||||
|
|
||||||
sql = "INSERT INTO exported(id, username, text, lt_errors, language, exported_at) VALUES(%s, %s, %s, %s, %s, %s)"
|
|
||||||
|
|
||||||
unique_id = str(uuid.uuid4())
|
|
||||||
|
|
||||||
now = datetime.now()
|
|
||||||
|
|
||||||
conn = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
conn = psycopg2.connect(database = self.corpus_db, user = self.corpus_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
cur.execute(sql, (unique_id, username, post, lt_errors, language, now))
|
|
||||||
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
is_saved = True
|
|
||||||
|
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
|
||||||
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
|
|
||||||
if conn is not None:
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return is_saved
|
|
||||||
|
|
||||||
def csv_save(self, filename):
|
|
||||||
|
|
||||||
is_saved = False
|
|
||||||
|
|
||||||
sql = "copy (select * from exported) to stdout with csv delimiter ';'"
|
|
||||||
|
|
||||||
conn = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
conn = psycopg2.connect(database = self.corpus_db, user = self.corpus_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
with open(filename, "w") as file:
|
|
||||||
cur.copy_expert(sql, file)
|
|
||||||
|
|
||||||
is_saved = True
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
|
||||||
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
|
|
||||||
if conn is not None:
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return is_saved
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __check_dbsetup(self):
|
def __check_dbsetup(self):
|
||||||
|
|
||||||
|
@ -521,17 +306,13 @@ class Database():
|
||||||
def __dbtables_schemes(self):
|
def __dbtables_schemes(self):
|
||||||
|
|
||||||
table = "corpus"
|
table = "corpus"
|
||||||
sql = "create table "+table+" (id uuid, username varchar(50), text varchar(500), created_at timestamptz, exported boolean default False, PRIMARY KEY (id))"
|
sql = "create table "+table+" (id uuid, username varchar(50), text varchar(500), created_at timestamptz, PRIMARY KEY (id))"
|
||||||
self.__create_table(self, table, sql)
|
self.__create_table(self, table, sql)
|
||||||
|
|
||||||
table = "users"
|
table = "users"
|
||||||
sql = "create table "+table+" (id uuid, username varchar(50), pending boolean default True, post_id bigint, PRIMARY KEY (id))"
|
sql = "create table "+table+" (id uuid, username varchar(50), pending boolean default True, post_id bigint, PRIMARY KEY (id))"
|
||||||
self.__create_table(self, table, sql)
|
self.__create_table(self, table, sql)
|
||||||
|
|
||||||
table = "exported"
|
|
||||||
sql = "create table "+table+" (id uuid, username varchar(50), text varchar(500), lt_errors int, language varchar(2), PRIMARY KEY (id))"
|
|
||||||
self.__create_table(self, table, sql)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __create_table(self, table, sql):
|
def __create_table(self, table, sql):
|
||||||
|
|
||||||
|
|
|
@ -266,14 +266,7 @@ class Mastodonbot:
|
||||||
|
|
||||||
visibility = notif.status.visibility
|
visibility = notif.status.visibility
|
||||||
|
|
||||||
if " " in text:
|
|
||||||
|
|
||||||
reply, question = self.__get_question(self, text)
|
reply, question = self.__get_question(self, text)
|
||||||
else:
|
|
||||||
|
|
||||||
reply = False
|
|
||||||
|
|
||||||
question = ''
|
|
||||||
|
|
||||||
mention_dict = {'reply': reply, 'question': question, 'id': id, 'account_id': account_id, 'acct': acct, 'status_id': status_id, 'text': text, 'visibility': visibility}
|
mention_dict = {'reply': reply, 'question': question, 'id': id, 'account_id': account_id, 'acct': acct, 'status_id': status_id, 'text': text, 'visibility': visibility}
|
||||||
|
|
||||||
|
@ -325,10 +318,6 @@ class Mastodonbot:
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
||||||
reply = False
|
|
||||||
|
|
||||||
question = ''
|
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return (reply, question)
|
return (reply, question)
|
||||||
|
|
|
@ -2,5 +2,3 @@ Mastodon.py
|
||||||
unidecode
|
unidecode
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
pytz
|
pytz
|
||||||
language_tool_python
|
|
||||||
langdetect
|
|
||||||
|
|
Loading…
Referencia en una nova incidència