Compare commits
3 Commits
e37a05b45c
...
76bf8f9f26
| Author | SHA1 | Date | |
|---|---|---|---|
| 76bf8f9f26 | |||
| 4094312c37 | |||
| f53783efd3 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@
|
||||
.DS_Store
|
||||
.vscode/
|
||||
__pycache__/
|
||||
test.py
|
||||
|
||||
@ -3,8 +3,10 @@ from src import config
|
||||
|
||||
|
||||
def get_last_id(cursor):
|
||||
cursor.execute("SELECT MAX(id) FROM usernames")
|
||||
cursor.execute("SELECT MAX(id) FROM Users")
|
||||
id = cursor.fetchall()[0][0]
|
||||
if id == None:
|
||||
return 0
|
||||
return id
|
||||
|
||||
def set_connection():
|
||||
@ -23,19 +25,19 @@ def close_connection(connection, cursor):
|
||||
connection.close()
|
||||
|
||||
#Functions don't close connection automatically, it has to be closed manually
|
||||
def add_user(username, connection, cursor):
|
||||
cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (get_last_id(cursor) + 1, username))
|
||||
def add_user(username, chat_id, connection, cursor):
|
||||
cursor.execute("INSERT INTO Users VALUES (%s, %s, %s);", (get_last_id(cursor) + 1, username, chat_id))
|
||||
connection.commit()
|
||||
|
||||
def delete_user(username, connection, cursor):
|
||||
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,))
|
||||
cursor.execute("DELETE FROM Users WHERE username = %s;", (username,))
|
||||
connection.commit()
|
||||
|
||||
def change_name(old_username, new_username, connection, cursor):
|
||||
cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username))
|
||||
cursor.execute("UPDATE Users SET username = %s WHERE username = %s;", (new_username, old_username))
|
||||
connection.commit()
|
||||
|
||||
def get_user(id, cursor):
|
||||
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,))
|
||||
username = cursor.fetchall()[0][0]
|
||||
return username
|
||||
def get_chat_id(id, cursor):
|
||||
cursor.execute("SELECT chatid FROM Users WHERE id = %s", (id,))
|
||||
chat_id = cursor.fetchall()[0][0]
|
||||
return chat_id
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
from minio import Minio
|
||||
from random import randint
|
||||
import DBwork
|
||||
from datetime import timedelta
|
||||
from src.Backend import DBwork
|
||||
from src import config
|
||||
|
||||
|
||||
@ -23,25 +24,28 @@ def getObjectExtension(client, currentDay, fileNumber):
|
||||
if counter == fileNumber:
|
||||
return obj.object_name.split('.')[-1]
|
||||
|
||||
def getFileNames(currentDay, client, username):
|
||||
def getImageName(currentDay, client):
|
||||
maxFiles = getNumberofObjects(client, currentDay)
|
||||
fileNumber = randint(1, maxFiles)
|
||||
fileExtension = getObjectExtension(client, currentDay, fileNumber)
|
||||
desiredFile = currentDay + '/' + str(fileNumber) + '.' + fileExtension
|
||||
downloadName = username + '.' + fileExtension
|
||||
return desiredFile, downloadName
|
||||
fileExtension = '.' + getObjectExtension(client, currentDay, fileNumber)
|
||||
desiredFile = currentDay + '/' + str(fileNumber) + fileExtension
|
||||
return desiredFile
|
||||
|
||||
def downloadImage(currentDay, username):
|
||||
def getDownloadURL(currentDay):
|
||||
client = _setClient()
|
||||
object_name, file_name = getFileNames(currentDay, client, username)
|
||||
client.fget_object(config.bucket_name, object_name, file_name)
|
||||
object_name = getImageName(currentDay, client)
|
||||
url = client.presigned_get_object(
|
||||
config.bucket_name,
|
||||
object_name,
|
||||
expires=timedelta(days=1))
|
||||
return url
|
||||
|
||||
def downloadForAll(currentDay):
|
||||
cur, conn = DBwork.set_connection()
|
||||
max_id = DBwork.get_last_id(cur)
|
||||
for id in range(1, max_id + 1):
|
||||
# chat_id = DBwork.get_user(id, cur)
|
||||
# image_URL(currentDay, user)
|
||||
chat_id = DBwork.get_chat_id(id, cur)
|
||||
image_URL = getDownloadURL(currentDay)
|
||||
# await bot.send_photo(chat_id = chat_id, photo = image_URL
|
||||
DBwork.close_connection(conn, cur)
|
||||
|
||||
|
||||
6
src/Backend/UserTable.sql
Normal file
6
src/Backend/UserTable.sql
Normal file
@ -0,0 +1,6 @@
|
||||
create table Users(
|
||||
Id serial primary key,
|
||||
username character varying(32),
|
||||
ChatId bigint not null,
|
||||
constraint chat_id_unique unique(ChatId)
|
||||
);
|
||||
Reference in New Issue
Block a user