implemented most of the pixiv scraper
This commit is contained in:
+7
-10
@@ -39,15 +39,13 @@ async def read_accounts_by_group_platform(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
account_data = db.get_accounts_by_group_platform(conn, groupname, platform)
|
||||
account_data = db.get_accounts_by_group_platform(conn, groupname, platform.lower())
|
||||
if account_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such account",
|
||||
)
|
||||
account = Account()
|
||||
account.fill(account_data)
|
||||
return account
|
||||
return Account().fill(account_data)
|
||||
|
||||
|
||||
@accounts_router.post("/add")
|
||||
@@ -65,13 +63,13 @@ async def add_account(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
if db.check_account_existence(conn, groupname, platform):
|
||||
if db.check_account_existence(conn, groupname, platform.lower()):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Account already exists",
|
||||
)
|
||||
hashed_password = encode_str(password)
|
||||
return db.create_account(conn, platform, login, hashed_password, metadata)
|
||||
return db.create_account(conn, groupname, current_user.username, platform.lower(), login, hashed_password, metadata)
|
||||
|
||||
|
||||
@accounts_router.post("/update")
|
||||
@@ -85,18 +83,17 @@ async def update_account(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
account_data = db.get_accounts_by_group_platform(conn, groupname, platform)
|
||||
account_data = db.get_accounts_by_group_platform(conn, groupname, platform.lower())
|
||||
if account_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such account",
|
||||
)
|
||||
account = Account()
|
||||
account.fill(account_data)
|
||||
account = Account().fill(account_data)
|
||||
|
||||
if current_user.username != account.author and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.update_account(conn, groupname, author, platform, login, password, metadata)
|
||||
return db.update_account(conn, groupname, author, platform.lower(), login, password, metadata)
|
||||
|
||||
+4
-5
@@ -23,14 +23,13 @@ async def read_feed(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
feed = Feed()
|
||||
feed_data = db.get_feed(conn, feed_id)
|
||||
if feed_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed",
|
||||
)
|
||||
feed.fill(feed_data)
|
||||
feed = Feed().fill(feed_data)
|
||||
|
||||
groupname = get_groupname_by_feed_id(conn, feed_id)
|
||||
if groupname is None:
|
||||
@@ -48,7 +47,7 @@ async def read_feed(
|
||||
return feed
|
||||
|
||||
|
||||
# TODO: most logic + exception
|
||||
# TODO: review exception
|
||||
@feeds_router.post("/new")
|
||||
async def new_feed(
|
||||
groupname: str,
|
||||
@@ -63,11 +62,11 @@ async def new_feed(
|
||||
|
||||
accounts = get_accounts_by_group(conn, groupname)
|
||||
feed = generate_feed(conn, accounts)
|
||||
if not isinstance(feed, Exception):
|
||||
if feed:
|
||||
return db.create_feed(conn, groupname, feed)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
status_code=status.HTTP_424_FAILED_DEPENDENCY,
|
||||
detail="Failed to generate feed",
|
||||
)
|
||||
|
||||
|
||||
+1
-3
@@ -27,15 +27,13 @@ async def read_any_group(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
group = Group()
|
||||
group_data = db.get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such group",
|
||||
)
|
||||
group.fill(group_data)
|
||||
return group
|
||||
return Group().fill(group_data)
|
||||
|
||||
@groups_router.post("/invite_code")
|
||||
async def read_group_invite_code(
|
||||
|
||||
+10
-1
@@ -22,6 +22,7 @@ class User(BaseModel):
|
||||
self.disabled = params["disabled"]
|
||||
self.last_seen_at = params["last_seen_at"]
|
||||
self.created_at = params["created_at"]
|
||||
return self
|
||||
username: str = ""
|
||||
password: str = ""
|
||||
role: str = "user"
|
||||
@@ -39,6 +40,7 @@ class Group(BaseModel):
|
||||
self.feed_interval_minutes = params["feed_interval_minutes"]
|
||||
self.last_feed_id = params["last_feed_id"]
|
||||
self.created_at = params["created_at"]
|
||||
return self
|
||||
groupname: str = ""
|
||||
author: str = ""
|
||||
invite_code: str = ""
|
||||
@@ -53,6 +55,7 @@ class Membership(BaseModel):
|
||||
self.groupname = params["groupname"]
|
||||
self.username = params["username"]
|
||||
self.joined_at = params["joined_at"]
|
||||
return self
|
||||
groupname: str = ""
|
||||
username: str = ""
|
||||
joined_at: datetime | None = None
|
||||
@@ -66,11 +69,12 @@ class Picture(BaseModel):
|
||||
self.url = params["url"]
|
||||
self.metadata = params["metadata"]
|
||||
self.created_at = params["created_at"]
|
||||
return self
|
||||
id: int = -1
|
||||
source: str = ""
|
||||
external_id: str = ""
|
||||
url: str = ""
|
||||
metadata: dict | None = None
|
||||
metadata: dict = {}
|
||||
created_at: datetime | None = None
|
||||
|
||||
|
||||
@@ -81,6 +85,7 @@ class Swipe(BaseModel):
|
||||
self.picture_id = params["picture_id"]
|
||||
self.value = params["value"]
|
||||
self.created_at = params["created_at"]
|
||||
return self
|
||||
username: str = ""
|
||||
feed_id: int = -1
|
||||
picture_id: int = -1
|
||||
@@ -94,6 +99,7 @@ class Feed(BaseModel):
|
||||
self.groupname = params["groupname"]
|
||||
self.image_ids = params["image_ids"]
|
||||
self.created_at = params["created_at"]
|
||||
return self
|
||||
id: int = -1
|
||||
groupname: str = ""
|
||||
image_ids: list[int] = []
|
||||
@@ -103,13 +109,16 @@ class Feed(BaseModel):
|
||||
class Account(BaseModel):
|
||||
def fill(self, params):
|
||||
self.id = params["id"]
|
||||
self.groupname = params["groupname"]
|
||||
self.author = params["author"]
|
||||
self.platform = params["platform"]
|
||||
self.login = params["login"]
|
||||
self.password = decode_str(params["password"])
|
||||
self.metadata = params["metadata"]
|
||||
self.created_at = params["created_at"]
|
||||
return self
|
||||
id: int = -1
|
||||
groupname: str = ""
|
||||
author: str = ""
|
||||
platform: str = ""
|
||||
login: str = ""
|
||||
|
||||
+1
-3
@@ -18,15 +18,13 @@ async def read_picture(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
picture = Picture()
|
||||
picture_data = db.get_picture(conn, id)
|
||||
if picture_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such picture",
|
||||
)
|
||||
picture.fill(picture_data)
|
||||
return picture
|
||||
return Picture().fill(picture_data)
|
||||
|
||||
|
||||
@pictures_router.post("/add")
|
||||
|
||||
+2
-5
@@ -38,15 +38,13 @@ async def read_swipe(
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
swipe = Swipe()
|
||||
swipe_data = db.get_swipe(conn, username, feed_id, picture_id)
|
||||
if swipe_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such swipe",
|
||||
)
|
||||
swipe.fill(swipe_data)
|
||||
return swipe
|
||||
return Swipe().fill(swipe_data)
|
||||
|
||||
|
||||
@swipes_router.post("/swipe/picture_id")
|
||||
@@ -106,14 +104,13 @@ async def add_swipe(
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
|
||||
group = Group()
|
||||
group_data = get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
group.fill(group_data)
|
||||
group = Group().fill(group_data)
|
||||
|
||||
# Check for trying to skip in
|
||||
# a group with skips disabled
|
||||
|
||||
+1
-2
@@ -27,14 +27,13 @@ async def read_users_any(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
user = User()
|
||||
user_data = db.get_user(conn, username)
|
||||
if user_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such user",
|
||||
)
|
||||
user.fill(user_data)
|
||||
user = User().fill(user_data)
|
||||
return user
|
||||
|
||||
|
||||
|
||||
+2
-5
@@ -98,12 +98,11 @@ async def get_current_user(
|
||||
except InvalidTokenError:
|
||||
raise credentials_exception
|
||||
|
||||
user = User()
|
||||
user_data = db.users.get_user(conn, token_data.username)
|
||||
if user_data is None:
|
||||
raise credentials_exception
|
||||
|
||||
user.fill(user_data)
|
||||
user = User().fill(user_data)
|
||||
|
||||
if user.disabled:
|
||||
raise HTTPException(
|
||||
@@ -122,10 +121,8 @@ def get_group_by_name(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such group"
|
||||
)
|
||||
group = Group()
|
||||
group_data = db.groups.get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
raise group_exception
|
||||
|
||||
group.fill(group_data)
|
||||
return group
|
||||
return Group().fill(group_data)
|
||||
|
||||
Reference in New Issue
Block a user