starting scraper development
This commit is contained in:
@ -3,13 +3,15 @@ import json
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
from api.models import Account
|
||||
|
||||
# account create and delete
|
||||
|
||||
def create_account(
|
||||
conn: connection,
|
||||
platform: str,
|
||||
login: str,
|
||||
password: int,
|
||||
password: str,
|
||||
metadata: dict
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
@ -44,28 +46,10 @@ def delete_account(
|
||||
|
||||
# account checks
|
||||
|
||||
def check_account_existence_by_id(
|
||||
conn: connection,
|
||||
account_id: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.accounts
|
||||
where account_id = %s
|
||||
);
|
||||
""",
|
||||
(account_id),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
def check_account_existence(
|
||||
conn: connection,
|
||||
platform: str,
|
||||
login: str,
|
||||
password: str
|
||||
groupname: str,
|
||||
platform: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
@ -73,10 +57,10 @@ def check_account_existence(
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.accounts
|
||||
where platform = %s, login = %s, password = %s
|
||||
where groupname = %s and platform = %s
|
||||
);
|
||||
""",
|
||||
(platform, login, password),
|
||||
(groupname, platform),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
@ -85,10 +69,11 @@ def check_account_existence(
|
||||
|
||||
def update_account(
|
||||
conn: connection,
|
||||
account_id: str,
|
||||
groupname: str,
|
||||
author: str,
|
||||
platform: str,
|
||||
login: str,
|
||||
password: int,
|
||||
password: str,
|
||||
metadata: dict
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
@ -96,12 +81,13 @@ def update_account(
|
||||
"""
|
||||
update picrinth.accounts
|
||||
SET platform = %s,
|
||||
author = %s,
|
||||
login = %s,
|
||||
password = %s,
|
||||
metadata = %s
|
||||
where account_id = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(platform, login, password, json.dumps(metadata), account_id),
|
||||
(platform, author, login, password, json.dumps(metadata), groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
@ -109,19 +95,37 @@ def update_account(
|
||||
|
||||
# account receiving
|
||||
|
||||
def get_account(
|
||||
# TODO: fix list comprehension
|
||||
def get_accounts_by_group(
|
||||
conn: connection,
|
||||
account_id: str
|
||||
groupname: str
|
||||
) -> list[Account]:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
from picrinth.accounts
|
||||
where groupname = %s
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
return [Account().fill(account_data) for (account_data,) in cur.fetchall()]
|
||||
|
||||
|
||||
def get_accounts_by_group_platform(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
platform: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select username,
|
||||
account_id, platform, login,
|
||||
password, metadata, created_at
|
||||
select groupname, author,
|
||||
platform, login, password,
|
||||
metadata, created_at
|
||||
from picrinth.accounts
|
||||
where account_id = %s
|
||||
where groupname = %s and platform = %s
|
||||
""",
|
||||
(account_id),
|
||||
(groupname, platform),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
@ -79,6 +79,25 @@ def check_invite_code(
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
|
||||
def check_group_author(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
author: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.groups
|
||||
where groupname = %s and author = %s
|
||||
);
|
||||
""",
|
||||
(groupname, author),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
|
||||
# group updates
|
||||
|
||||
def update_group_groupname(
|
||||
|
||||
@ -29,22 +29,7 @@ def create_picture(
|
||||
return result[0]
|
||||
|
||||
|
||||
def delete_picture_by_url(
|
||||
conn: connection,
|
||||
url: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.pictures
|
||||
where url = %s
|
||||
""",
|
||||
(url,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def delete_picture_by_id(
|
||||
def delete_picture(
|
||||
conn: connection,
|
||||
id: int
|
||||
):
|
||||
@ -62,26 +47,9 @@ def delete_picture_by_id(
|
||||
|
||||
# picture receiving
|
||||
|
||||
def get_picture_by_url(
|
||||
def get_picture(
|
||||
conn: connection,
|
||||
url: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select id, source,
|
||||
external_id, url,
|
||||
metadata, created_at
|
||||
from picrinth.pictures
|
||||
where url = %s
|
||||
""",
|
||||
(url,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def get_picture_by_id(
|
||||
conn: connection,
|
||||
id: str
|
||||
id: int
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
|
||||
Reference in New Issue
Block a user