data package

Description

Modules that work with the data section

data_reader

Read data from files

modules.data.data_reader.get_abs_path(*root_file_path: str) → str

Get the abs path from the root directory of the project to the requested path

Parameters

root_file_path (*str) – path from the root project directory

Returns

corrisponding abs path

Return type

str

modules.data.data_reader.read_file(*root_file_path: str) → str

Read the contens of the file

Parameters

root_file_path (*str) – path of the file to read from the root project directory

Returns

contents of the file

Return type

str

modules.data.data_reader.read_md(file_name: str) → str

Read the contens of a markdown file. The path is data/markdown

Parameters

file_name (str) – name of the file

Returns

contents of the file

Return type

str

db_manager

Handles the management of databases

class modules.data.db_manager.DbManager

Bases: object

Class that handles the management of databases

static count_from(table_name: str, select: str = '*', where: str = '', where_args: tuple = None) → int

Returns the number of rows from SELECT COUNT(*) FROM table_name WHERE where

Parameters
  • table_name (str) – name of the table used in the FROM

  • select (str, optional) – columns considered for the query. Defaults to “*”.

  • where (str, optional) – where clause, with %s placeholders for the where_args. Defaults to “”.

  • where_args (tuple, optional) – args used in the where clause. Defaults to None.

Returns

number of rows

Return type

int

static delete_from(table_name: str, where: str = '', where_args: tuple = None)

Deletes the rows from the specified table, where the condition, when set, is satisfied Execute “DELETE FROM table_name [WHERE where (with where_args)]”

Parameters
  • table_name (str) – name of the table used in the DELETE FROM

  • where (str, optional) – where clause, with %s placeholders for the where args. Defaults to “”.

  • where_args (tuple, optional) – args used in the where clause. Defaults to None.

static get_db() → Tuple[sqlite3.Connection, sqlite3.Cursor]

Creates the connection to the database. It can be sqlite or postgres

Returns

sqlite database connection and cursor Tuple[psycopg2.Connection, psycopg2.Cursor]psycopg2.connection: postgres database connection and cursor

Return type

Tuple[sqlite3.Connection, sqlite3.Cursor]

static insert_into(table_name: str, values: tuple, columns: tuple = '')

Inserts the specified values in the database

Parameters
  • table_name (str) – name of the table used in the INSERT INTO

  • values (tuple) – values to be inserted

  • columns (tuple, optional) – columns that will be inserted, as a tuple of strings. Defaults to None.

static query_from_file(*file_path: str)

Commits all the queries in the specified file. The queries must be separated by a —– string Should not be used to select something

Parameters

file_path (str) – path of the text file containing the queries

static query_from_string(*queries: str)

Commits all the queries in the string Should not be used to select something

Parameters

queries (str) – tuple of queries

static select_from(table_name: str, select: str = '*', where: str = '', where_args: tuple = None) → list

Returns the result of a SELECT select FROM table_name [WHERE where (with where_args)]

Parameters
  • table_name (str) – name of the table used in the FROM

  • select (str, optional) – columns considered for the query. Defaults to “*”.

  • where (str, optional) – where clause, with %s placeholders for the where_args. Defaults to “”.

  • where_args (tuple, optional) – args used in the where clause. Defaults to None.

Returns

rows from the select

Return type

list

use_remote_db = False
modules.data.db_manager.dict_factory(cursor: sqlite3.Cursor, row: sqlite3.Row) → dict

Makes so that the cursor is a list of dictionaries with column names as keys

Parameters
  • cursor (sqlite3.Cursor) – cursor generated by the database

  • row (sqlite3.Row) – rows of the database

Returns

structure of the database used used by the cursor

Return type

dict

meme_data

Data management for the meme bot

class modules.data.meme_data.MemeData

Bases: object

Class that handles the management of persistent data fetch or manipulation in the meme bot

static ban_user(user_id: int)

Adds the user to the banned list

Parameters

user_id (int) – id of the user to ban

static become_anonym(user_id: int) → bool

Removes the user from the credited list, if he was present

Parameters

user_id (int) – id of the user to make anonym

Returns

whether the user was already anonym

Return type

bool

static become_credited(user_id: int) → bool

Adds the user to the credited list, if he wasn’t already credited

Parameters

user_id (int) – id of the user to credit

Returns

whether the user was already credited

Return type

bool

static get_admin_list_votes(g_message_id: int, group_id: int, approve: bool) → Tuple[str]

Gets the vote of a specific admin on a pending post

Parameters
  • admin_id (int) – id of the admin that voted

  • g_message_id (int) – id of the post in question in the group

  • group_id (int) – id of the admin group

Returns

a bool representing the vote or None if a vote was not yet made

Return type

Optional[bool]

static get_pending_votes(g_message_id: int, group_id: int, vote: bool) → int

Gets all the votes of a specific kind (approve or reject) on a pending post

Parameters
  • g_message_id (int) – id of the post in question in the group

  • group_id (int) – id of the admin group

  • vote (bool) – whether you look for the approve or reject votes

Returns

number of votes

Return type

int

static get_published_votes(c_message_id: int, channel_id: int, vote: bool) → int

Gets all the votes of a specific kind (upvote or downvote) on a published post

Parameters
  • c_message_id (int) – id of the post in question in the channel

  • channel_id (int) – id of the channel

  • vote (bool) – whether you look for upvotes or downvotes

Returns

number of votes

Return type

int

static get_user_id(g_message_id: int, group_id: int) → Optional[int]

Gets the user_id of the user that made the pending post

Parameters
  • g_message_id (int) – id of the post in question in the group

  • group_id (int) – id of the admin group

Returns

user_id, if found

Return type

Optional[int]

static insert_pending_post(user_message: telegram.message.Message, admin_message: telegram.message.Message)

Insert a new post in the table of pending posts

Parameters
  • user_message (Message) – message sent by the user that contains the post

  • admin_message (Message) – message recieved in the admin group that references the post

static insert_published_post(channel_message: telegram.message.Message)

Inserts a new post in the table of pending posts

Parameters

channel_message (Message) – message approved to be published

static is_banned(user_id: int) → bool

Checks if the user is banned or not

Parameters

user_id (int) – id of the user to check

Returns

whether the user is banned or not

Return type

bool

static is_credited(user_id: int) → bool

Checks if the user is in the credited list

Parameters

user_id (int) – id of the user to check

Returns

whether the user is to be credited or not

Return type

bool

static is_pending(user_id: int) → bool

Checks if the user still has a post pending

Parameters

user_id (int) – id of the user to check

Returns

whether the user still has a post pending or not

Return type

bool

static remove_pending_meme(g_message_id: int, group_id: int)

Removes all entries on a post that is no longer pending

Parameters
  • g_message_id (int) – id of the no longer pending post in the group

  • group_id (int) – id of the admin group

static sban_user(user_id: int) → bool

Removes the user from the banned list

Parameters

user_id (int) – id of the user to sban

Returns

whether the user was present in the banned list before the sban or not

Return type

bool

static set_admin_vote(admin_id: int, g_message_id: int, group_id: int, approval: bool) → int

Adds the vote of the admin on a specific post, or update the existing vote, if needed

Parameters
  • admin_id (int) – id of the admin that voted

  • g_message_id (int) – id of the post in question in the group

  • group_id (int) – id of the admin group

  • approval (bool) – whether the vote is approval or reject

Returns

number of similar votes (all the approve or the reject), or -1 if the vote wasn’t updated

Return type

int

static set_user_vote(user_id: int, c_message_id: int, channel_id: int, vote: bool) → Tuple[int, bool]

Adds the vote of the user on a specific post, or update the existing vote, if needed

Parameters
  • user_id (int) – id of the user that voted

  • c_message_id (int) – id of the post in question in the channel

  • channel_id (int) – id of the channel

  • vote (bool) – whether it is an upvote or a downvote

Returns

number of similar votes (all the upvotes or the downvotes), or -1 if the vote wasn’t updated, whether or not the vote was added or removed

Return type

Tuple[int, bool]