diff --git a/app/locales/ca.txt b/app/locales/ca.txt new file mode 100644 index 0000000..e8c2110 --- /dev/null +++ b/app/locales/ca.txt @@ -0,0 +1,17 @@ +current_users_str: usuaris actuals +users_before_str: usuaris abans +users_hour_str: usuaris per hora +new_users_last_hour_str: nous usuaris darrera hora +new_users_last_day_str: nous usuaris darrer dia +new_users_last_week_str: nous usuaris darrera setmana +hourly_users_evolution_str: evolució horaria d'usuaris +in_the_last_hour_str: en la darrera hora +daily_users_evolution_str: evolució diaria d'usuaris +in_the_last_day_str: en el darrer dia +weekly_users_evolution_str: evolució setmanal d'usuaris +in_the_last_week_str: en la darrera setmana +daily_evolution_str: Evolució diaria +weekly_evolution_str: Evolució setmanal +welcomes_str: dona la benvinguda a +we_have_str: Ja som +users_str: usuaris diff --git a/app/locales/en.txt b/app/locales/en.txt new file mode 100644 index 0000000..3b47762 --- /dev/null +++ b/app/locales/en.txt @@ -0,0 +1,17 @@ +current_users_str: current users +users_before_str: users before +users_hour_str: users per hour +new_users_last_hour_str: new users last hour +new_users_last_day_str: new users last day +new_users_last_week_str: new users last week +hourly_users_evolution_str: hourly users evolution +in_the_last_hour_str: in the last hour +daily_users_evolution_str: daily users evolution +in_the_last_day_str: in the last day +weekly_users_evolution_str: weekly users evolution +in_the_last_week_str: in the last week +daily_evolution_str: Daily evolution +weekly_evolution_str: Weekly evolution +welcomes_str: welcomes +we_have_str: We have +users_str: users diff --git a/app/locales/es.txt b/app/locales/es.txt new file mode 100644 index 0000000..455a357 --- /dev/null +++ b/app/locales/es.txt @@ -0,0 +1,17 @@ +current_users_str: usuarios actuales +users_before_str: usuarios anteriores +users_hour_str: usuarios por hora +new_users_last_hour_str: nuevos usuarios última hora +new_users_last_day_str: nuevos usuarios último dia +new_users_last_week_str: nuevos usuaris última setmana +hourly_users_evolution_str: evolución horaria de usuarios +in_the_last_hour_str: en la última hora +daily_users_evolution_str: evolución diaria de usuarios +in_the_last_day_str: en el último dia +weekly_users_evolution_str: evolución semanal de usuarios +in_the_last_week_str: en la última semana +daily_evolution_str: Evolución diaria +weekly_evolution_str: Evolución semanal +welcomes_str: da la bienvenida a +we_have_str: Ya somos +users_str: usuarios diff --git a/welcome.py b/welcome.py index f0660a0..093f193 100644 --- a/welcome.py +++ b/welcome.py @@ -45,6 +45,30 @@ def db_config(): return (mastodon_db, mastodon_db_user, welcome_db, welcome_db_user) +def get_locale( parameter, welcome_lang): + + if welcome_lang not in ['ca','es','en']: + print("lang must be 'ca', 'es' or 'en'") + sys.exit(0) + + language_filepath = f"app/locales/{welcome_lang}.txt" + + if not os.path.isfile(language_filepath): + print("File %s not found, exiting."%language_filepath) + sys.exit(0) + + with open( language_filepath ) as f: + for line in f: + if line.startswith( parameter ): + return line.replace(parameter + ":", "").strip() + + print(language_filepath + " Missing parameter %s "%parameter) + sys.exit(0) + +def usage(): + + print('usage: python ' + sys.argv[0] + ' --ca (or --es or --en)') + # Returns the parameter from the specified file def get_parameter( parameter, file_path ): # Check if secrets file exists @@ -67,319 +91,377 @@ def get_parameter( parameter, file_path ): if __name__ == '__main__': - mastodon, mastodon_hostname = mastodon() + # usage modes - mastodon_db, mastodon_db_user, welcome_db, welcome_db_user = db_config() + if len(sys.argv) == 1: - ############################################################################### - # current registered users - ############################################################################### + usage() - try: + elif len(sys.argv) >= 2: - conn = None + welcome_lang = '' - conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") + if sys.argv[1] == '--ca': - cur = conn.cursor() + welcome_lang = 'ca' - cur.execute("SELECT count(*) FROM users WHERE disabled='f' and approved='t'") + if sys.argv[1] == '--es': - row = cur.fetchone() + welcome_lang = 'es' - if row != None: + elif sys.argv[1] == '--en': - current_id = row[0] + welcome_lang = 'en' - else: + if not welcome_lang in ['ca', 'es', 'en']: - current_id = 0 + print("\nOnly 'ca', 'es' and 'en' languages are supported.\n") - cur.close() + sys.exit(0) - except (Exception, psycopg2.DatabaseError) as error: + mastodon, mastodon_hostname = mastodon() - print (error) + mastodon_db, mastodon_db_user, welcome_db, welcome_db_user = db_config() - finally: + ############################################################################### + # current registered users + ############################################################################### - if conn is not None: + try: - conn.close() + conn = None - ############################################################################### + conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") - now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + cur = conn.cursor() - users_before = current_id - users_hour = 0 + cur.execute("SELECT count(*) FROM users WHERE disabled='f' and approved='t'") - conn = None + row = cur.fetchone() - try: + if row != None: - conn = psycopg2.connect(database = welcome_db, user = welcome_db_user, password = "", host = "/var/run/postgresql", port = "5432") + current_id = row[0] - cur = conn.cursor() + else: - cur.execute("SELECT DISTINCT ON (datetime) users FROM welcome WHERE datetime > current_timestamp - INTERVAL '70 minutes' ORDER BY datetime asc LIMIT 1") + current_id = 0 - row = cur.fetchone() + cur.close() - users_before = row[0] + except (Exception, psycopg2.DatabaseError) as error: - cur.close() + print (error) - users_hour = current_id - users_before + finally: - except (Exception, psycopg2.DatabaseError) as error: + if conn is not None: - print (error) + conn.close() - finally: + ############################################################################### - if conn is not None: + now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - conn.close() + users_before = current_id + users_hour = 0 - print("-----------------") - print("current users: "+str(current_id)) - print("users before: "+str(users_before)) - print("users / hour: "+str(users_hour)) + conn = None - insert_sql = """INSERT INTO welcome(datetime, users, users_hour) - VALUES(%s,%s,%s) RETURNING datetime;""" - conn = None + try: - now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + conn = psycopg2.connect(database = welcome_db, user = welcome_db_user, password = "", host = "/var/run/postgresql", port = "5432") - try: + cur = conn.cursor() - conn = psycopg2.connect(database = welcome_db, user = welcome_db_user, password = "", host = "/var/run/postgresql", port = "5432") + cur.execute("SELECT DISTINCT ON (datetime) users FROM welcome WHERE datetime > current_timestamp - INTERVAL '70 minutes' ORDER BY datetime asc LIMIT 1") - cur = conn.cursor() + row = cur.fetchone() - cur.execute(insert_sql, (now, current_id, users_hour)) + users_before = row[0] - datetime = cur.fetchone()[0] + cur.close() - conn.commit() + users_hour = current_id - users_before - cur.close() + except (Exception, psycopg2.DatabaseError) as error: - except (Exception, psycopg2.DatabaseError) as error: + print (error) - print(error) + finally: - finally: + if conn is not None: - if conn is not None: + conn.close() - conn.close() + current_users_str = get_locale("current_users_str", welcome_lang) + users_before_str = get_locale("users_before_str", welcome_lang) + users_hour_str = get_locale("users_hour_str", welcome_lang) + + print("-----------------") + print(current_users_str + ': ' + str(current_id)) + print(users_before_str + ': ' + str(users_before)) + print(users_hour_str + ': ' + str(users_hour)) - ############################################################################### - # WORK OUT THE TOOT TEXT - ############################################################################### + insert_sql = """INSERT INTO welcome(datetime, users, users_hour) + VALUES(%s,%s,%s) RETURNING datetime;""" + conn = None - # Calculate difference in times + now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - hourly_change_string = "" - daily_change_string = "" - weekly_change_string = "" - users_hourly_change_string = "" + try: - try: + conn = psycopg2.connect(database = welcome_db, user = welcome_db_user, password = "", host = "/var/run/postgresql", port = "5432") - conn = None + cur = conn.cursor() + + cur.execute(insert_sql, (now, current_id, users_hour)) + + datetime = cur.fetchone()[0] + + conn.commit() + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) - conn = psycopg2.connect(database = welcome_db, user = welcome_db_user, password = "", host = "/var/run/postgresql", port = "5432") + finally: - cur = conn.cursor() + if conn is not None: - cur.execute("SELECT DISTINCT ON (datetime) users, datetime FROM welcome WHERE datetime > current_timestamp - INTERVAL '62 minutes' ORDER BY datetime asc LIMIT 1") + conn.close() - row = cur.fetchone() + ############################################################################### + # WORK OUT THE TOOT TEXT + ############################################################################### - if row[0] == None: + # Calculate difference in times - users_last_hour = 0 + hourly_change_string = "" + daily_change_string = "" + weekly_change_string = "" + users_hourly_change_string = "" - else: + try: - users_last_hour = row[0] + conn = None - cur.execute("SELECT DISTINCT ON (datetime) users,datetime FROM welcome WHERE datetime > current_timestamp - INTERVAL '1 day' ORDER BY datetime asc LIMIT 1") + conn = psycopg2.connect(database = welcome_db, user = welcome_db_user, password = "", host = "/var/run/postgresql", port = "5432") - row = cur.fetchone() + cur = conn.cursor() - if row[0] == None: + cur.execute("SELECT DISTINCT ON (datetime) users, datetime FROM welcome WHERE datetime > current_timestamp - INTERVAL '62 minutes' ORDER BY datetime asc LIMIT 1") - users_last_day = 0 + row = cur.fetchone() - else: + if row[0] == None: - users_last_day = row[0] + users_last_hour = 0 - cur.execute("SELECT DISTINCT ON (datetime) users,datetime FROM welcome WHERE datetime > current_timestamp - INTERVAL '7 days' ORDER BY datetime asc LIMIT 1") + else: - row = cur.fetchone() + users_last_hour = row[0] - if row[0] == None: + cur.execute("SELECT DISTINCT ON (datetime) users,datetime FROM welcome WHERE datetime > current_timestamp - INTERVAL '1 day' ORDER BY datetime asc LIMIT 1") - users_last_week = 0 - else: + row = cur.fetchone() - users_last_week = row[0] + if row[0] == None: - cur.close() + users_last_day = 0 - except (Exception, psycopg2.DatabaseError) as error: + else: - print (error) + users_last_day = row[0] - finally: + cur.execute("SELECT DISTINCT ON (datetime) users,datetime FROM welcome WHERE datetime > current_timestamp - INTERVAL '7 days' ORDER BY datetime asc LIMIT 1") - if conn is not None: + row = cur.fetchone() - conn.close() + if row[0] == None: - ################################################################################# + users_last_week = 0 + else: - welcome_users = [] + users_last_week = row[0] - if time.localtime().tm_isdst == 0: + cur.close() - interval_time = '60 minutes' + except (Exception, psycopg2.DatabaseError) as error: - elif time.localtime().tm_isdst == 1: + print (error) - interval_time = '120 minutes' + finally: - conn = None + if conn is not None: - try: + conn.close() - conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") + ################################################################################# + + welcome_users = [] + + if time.localtime().tm_isdst == 0: + + interval_time = '60 minutes' + + elif time.localtime().tm_isdst == 1: + + interval_time = '120 minutes' + + conn = None - cur = conn.cursor() + try: - cur.execute("select username, domain from accounts where (created_at + interval '" + interval_time + "' > (now() - interval '1 hour')) and domain is null and id in (select account_id from users where approved and not disabled)") + conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") - rows = cur.fetchall() + cur = conn.cursor() - for row in rows: + cur.execute("select username, domain from accounts where (created_at + interval '" + interval_time + "' > (now() - interval '1 hour')) and domain is null and id in (select account_id from users where approved and not disabled)") - if row[1] == None: + rows = cur.fetchall() - welcome_users.append(row[0]) + for row in rows: - cur.close() + if row[1] == None: - except (Exception, psycopg2.DatabaseError) as error: + welcome_users.append(row[0]) - print(error) + cur.close() - finally: + except (Exception, psycopg2.DatabaseError) as error: - if conn is not None: + print(error) - conn.close() + finally: - i = 0 + if conn is not None: - new_users_string = "" + conn.close() - while i < len(welcome_users): + i = 0 - new_users_string = new_users_string+"@"+welcome_users[i] + new_users_string = "" - i += 1 + while i < len(welcome_users): - if i < len(welcome_users): + new_users_string = new_users_string+"@"+welcome_users[i] - new_users_string = new_users_string+", " + i += 1 - ######################################################################################################### + if i < len(welcome_users): - hour_inc = current_id - users_last_hour - day_inc = current_id - users_last_day - week_inc = current_id - users_last_week + new_users_string = new_users_string+", " - print("------------------------") - print("new users last hour: "+str(hour_inc)) - print("new users last day: "+str(day_inc)) - print("new users last week: "+str(week_inc)) + ######################################################################################################### - ################################################################################### + hour_inc = current_id - users_last_hour + day_inc = current_id - users_last_day + week_inc = current_id - users_last_week - # Hourly change - if hour_inc != 0: + new_users_last_hour_str = get_locale("new_users_last_hour_str", welcome_lang) + new_users_last_day_str = get_locale("new_users_last_day_str", welcome_lang) + new_users_last_week_str = get_locale("new_users_last_week_str", welcome_lang) + + print("------------------------") + print(new_users_last_hour_str + ': ' + str(hour_inc)) + print(new_users_last_day_str + ': ' + str(day_inc)) + print(new_users_last_week_str + ': ' + str(week_inc)) - users_hourly_change = current_id - users_last_hour - print("Hourly users evolution: %s"%users_hourly_change) - if users_hourly_change > 0: - hourly_change_string = "+" + format(users_hourly_change, ",d") + " en la darrera hora\n" + ################################################################################### - # Daily change - if day_inc != 0: + # Hourly change + if hour_inc != 0: - daily_change = current_id - users_last_day - print("Daily users evolution: %s"%daily_change) - if daily_change > 0: - daily_change_string = "+" + format(daily_change, ",d") + " in the last day\n" + users_hourly_change = current_id - users_last_hour + + hourly_users_evolution_str = get_locale("hourly_users_evolution_str", welcome_lang) + in_the_last_hour_str = get_locale("in_the_last_hour_str", welcome_lang) + print(hourly_users_evolution_str + ': %s'%users_hourly_change) + + if users_hourly_change > 0: + hourly_change_string = "+" + format(users_hourly_change, ",d") + in_the_last_hour_str + "\n" - # Weekly change - if week_inc != 0: + # Daily change + if day_inc != 0: - weekly_change = current_id - users_last_week - print("Weekly users evolution: %s"%weekly_change) - if weekly_change > 0: - weekly_change_string = "+" + format(weekly_change, ",d") + " in the last week\n" + daily_change = current_id - users_last_day - elif users_hourly_change < 0: + daily_users_evolution_str = get_locale("daily_users_evolution_str", welcome_lang) + in_the_last_day_str = get_locale("in_the_last_day_str", welcome_lang) + print(daily_users_evolution_str + ': %s'%daily_change) + + if daily_change > 0: + daily_change_string = "+" + format(daily_change, ",d") + in_the_last_day_str + "\n" - hourly_change_string = format(users_hourly_change, ",d") + " in the last hour\n" + # Weekly change + if week_inc != 0: - # Daily change - if day_inc != 0: - daily_change = current_id - users_last_day - print("Daily evolution: %s"%daily_change) - if daily_change < 0: - daily_change_string = format(daily_change, ",d") + " in the last day\n" + weekly_change = current_id - users_last_week + + weekly_users_evolution_str = get_locale("weekly_users_evolution_str", welcome_lang) + in_the_last_week_str = get_locale("in_the_last_week_str", welcome_lang) + print(weekly_users_evolution_str + ': %s'%weekly_change) - # Weekly change - if week_inc != 0: + if weekly_change > 0: + weekly_change_string = "+" + format(weekly_change, ",d") + in_the_last_week_str + "\n" - weekly_change = current_id - users_last_week - print("Weekly evolution: %s"%weekly_change) - if weekly_change < 0: - weekly_change_string = format(weekly_change, ",d") + " in the last week\n" + elif users_hourly_change < 0: - ############################################################################### - # TOOT IT! - ############################################################################### + hourly_change_string = format(users_hourly_change, ",d") + in_the_last_hour_str + "\n" - if len(welcome_users) > 0: + # Daily change + if day_inc != 0: + daily_change = current_id - users_last_day + + daily_evolution_str = get_locale("daily_evolution_str", welcome_lang) + print(daily_evolution_str + ': %s'%daily_change) + + if daily_change < 0: + daily_change_string = format(daily_change, ",d") + in_the_last_day_str + "\n" - toot_text = "\n" - toot_text += mastodon_hostname + " welcomes:" + "\n\n" - toot_text += new_users_string + "\n" - toot_text += "\n" - toot_text += "\n\nWe have " + str(current_id) + " users\n\n" - toot_text += hourly_change_string - toot_text += daily_change_string - toot_text += weekly_change_string - toot_text += "\n" + # Weekly change + if week_inc != 0: - print("Tooting...") - print(toot_text) - if len(toot_text) < 500: - mastodon.status_post(toot_text, in_reply_to_id=None, ) - else: - toot_text1, toot_text2 = toot_text[:int(len(toot_text)/2)], toot_text[int(len(toot_text)/2):] - toot_id = mastodon.status_post(toot_text1, in_reply_to_id=None,) - mastodon.status_post(toot_text2, in_reply_to_id=toot_id,) - print(toot_text1) - print(toot_text2) + weekly_change = current_id - users_last_week + + weekly_evolution_str = get_locale("weekly_evolution_str", welcome_lang) + print(weekly_evolution_str + ': %s'%weekly_change) + + if weekly_change < 0: + weekly_change_string = format(weekly_change, ",d") + in_the_last_week_str + "\n" - print("Done!") + ############################################################################### + # TOOT IT! + ############################################################################### + + if len(welcome_users) > 0: + + welcomes_str = get_locale("welcomes_str", welcome_lang) + we_have_str = get_locale("we_have_str", welcome_lang) + users_str = get_locale("users_str", welcome_lang) + + toot_text = "\n" + toot_text += mastodon_hostname + ' ' + welcomes_str + ':\n\n' + toot_text += new_users_string + "\n" + toot_text += "\n" + toot_text += "\n\n" + we_have_str + ' ' + str(current_id) + users_str + "\n\n" + toot_text += hourly_change_string + toot_text += daily_change_string + toot_text += weekly_change_string + toot_text += "\n" + + print("Tooting...") + print(toot_text) + if len(toot_text) < 500: + mastodon.status_post(toot_text, in_reply_to_id=None, ) + else: + toot_text1, toot_text2 = toot_text[:int(len(toot_text)/2)], toot_text[int(len(toot_text)/2):] + toot_id = mastodon.status_post(toot_text1, in_reply_to_id=None,) + mastodon.status_post(toot_text2, in_reply_to_id=toot_id,) + print(toot_text1) + print(toot_text2) + + print("Done!")