Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: refactor #460

Merged
merged 43 commits into from
Jan 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d42e55b
step1
BennyThink Sep 21, 2024
57b159c
remove
BennyThink Nov 4, 2024
eedb8e3
add pdm
BennyThink Nov 25, 2024
bd47128
going
BennyThink Nov 25, 2024
5cb76e3
going
BennyThink Nov 25, 2024
48c3304
going
BennyThink Nov 26, 2024
4f02bc2
Improved `extract_code_from_instagram_url` function
SanujaNS Nov 27, 2024
e0f095d
Remove terabox download function
SanujaNS Nov 27, 2024
f10b07f
Remove unused `parse_cookie_file` function
SanujaNS Nov 27, 2024
cf0c2b8
define abstract class
BennyThink Nov 27, 2024
991a036
basic upload done?
BennyThink Nov 28, 2024
580c499
db operation
BennyThink Nov 30, 2024
072f1d1
db operation
BennyThink Nov 30, 2024
deb7683
db operation
BennyThink Nov 30, 2024
9ff9b20
db operation
BennyThink Nov 30, 2024
4c1c710
db operation
BennyThink Nov 30, 2024
0f8df41
fix name_pattern regex of `extract_url_and_name` function
SanujaNS Dec 1, 2024
83a8c73
pre-commit
BennyThink Dec 1, 2024
ef0beca
pre-push
BennyThink Dec 1, 2024
f7bd4fe
pre-push
BennyThink Dec 1, 2024
f8dec38
Refactor: Change single quotes to double quotes for name_pattern regex
SanujaNS Dec 1, 2024
e484098
add more methods
BennyThink Dec 3, 2024
6ba0cc2
rename
BennyThink Dec 3, 2024
5346fb6
use self._bot_msg
BennyThink Dec 3, 2024
38ed4c4
download done?
BennyThink Dec 3, 2024
3b1b9c7
update deps
BennyThink Dec 4, 2024
de04b72
add entrance
BennyThink Jan 11, 2025
f5beefd
runnable
BennyThink Jan 11, 2025
611d39a
fixes
BennyThink Jan 11, 2025
1da0edc
fix
BennyThink Jan 12, 2025
14e3368
record usage fix
BennyThink Jan 12, 2025
1546ed3
add cookies
BennyThink Jan 14, 2025
897211f
fix settings
BennyThink Jan 14, 2025
233193f
hint
BennyThink Jan 14, 2025
f4b49af
hint
BennyThink Jan 14, 2025
bd17efa
reset
BennyThink Jan 14, 2025
ae84522
rename
BennyThink Jan 14, 2025
86e896c
formats fix
BennyThink Jan 14, 2025
da8d45f
update README.md
BennyThink Jan 14, 2025
4f9df5a
update
BennyThink Jan 14, 2025
e909640
Merged master into your-branch-name, resolved conflicts in favor of y…
BennyThink Jan 14, 2025
7f2f16e
rename
BennyThink Jan 14, 2025
e30324b
rename
BennyThink Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
db operation
BennyThink committed Nov 30, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 580c49955e7675356ab129e362f4596b237a6a95
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ RUN pdm install

FROM python:3.12-alpine AS runner
WORKDIR /app
ENV TZ=Europe/Stockholm

RUN apk update && apk add --no-cache ffmpeg aria2
COPY --from=pybuilder /build/.venv/lib/ /usr/local/lib/
72 changes: 58 additions & 14 deletions ytdlbot/database/model.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
from sqlalchemy import Column, Enum, Float, ForeignKey, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.dialects.mysql import JSON

# ytdlbot - model.py

@@ -19,11 +20,10 @@ class User(Base):
__tablename__ = "users"

id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(String(255), unique=True, nullable=False) # telegram user id
username = Column(String(255), nullable=True)
name = Column(String(255), nullable=True)
free = Column(Integer, default=0)
user_id = Column(Integer, unique=True, nullable=False) # telegram user id
free = Column(Integer, default=5)
paid = Column(Integer, default=0)
config = Column(JSON)

settings = relationship("Setting", back_populates="user", cascade="all, delete-orphan")
payments = relationship("Payment", back_populates="user", cascade="all, delete-orphan")
@@ -33,8 +33,8 @@ class Setting(Base):
__tablename__ = "settings"

id = Column(Integer, primary_key=True, autoincrement=True)
download = Column(Enum("high", "medium", "low", "audio", "custom"), nullable=False)
upload = Column(Enum("video", "audio", "document"), nullable=False)
download = Column(Enum("high", "medium", "low", "audio", "custom"), nullable=False, default="high")
upload = Column(Enum("video", "audio", "document"), nullable=False, default="video")
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)

user = relationship("User", back_populates="settings")
@@ -82,36 +82,80 @@ def session_manager():

def get_download_settings(uid) -> Literal["high", "medium", "low", "audio", "custom"]:
with session_manager() as session:
return "custom"
data = session.query(Setting).filter(Setting.user_id == uid).first()
if data:
return data.download
return "high"


def get_upload_settings(uid) -> Literal["video", "audio", "document"]:
with session_manager() as session:
data = session.query(Setting).filter(Setting.user_id == uid).first()
if data:
return data.upload
return "video"


def set_user_settings(uid: int, key: str, value: str):
# set download or upload settings
pass
with session_manager() as session:
# upsert
setting = session.query(Setting).filter(Setting.user_id == uid).first()
if setting:
setattr(setting, key, value)
else:
session.add(Setting(user_id=uid, **{key: value}))


def get_free_quota(uid: int):
pass
with session_manager() as session:
data = session.query(User).filter(User.user_id == uid).first()
if data:
return data.free
return 5


def get_paid_quota(uid: int):
if not os.getenv("ENABLE_VIP"):
return math.inf
if os.getenv("ENABLE_VIP"):
with session_manager() as session:
data = session.query(User).filter(User.user_id == uid).first()
if data:
return data.paid

return 0

return math.inf


def reset_free_quota(uid: int):
pass
with session_manager() as session:
data = session.query(User).filter(User.user_id == uid).first()
if data:
data.free = 5


def add_paid_quota(uid: int, amount: int):
pass
with session_manager() as session:
data = session.query(User).filter(User.user_id == uid).first()
if data:
data.paid += amount


def use_quota(uid: int):
# use free first, then paid
pass
with session_manager() as session:
user = session.query(User).filter(User.user_id == uid).first()
if user:
if user.free > 0:
user.free -= 1
elif user.paid > 0:
user.paid -= 1
else:
raise Exception("Quota exhausted")


def init_user(uid: int):
with session_manager() as session:
user = session.query(User).filter(User.user_id == uid).first()
if not user:
session.add(User(user_id=uid))
2 changes: 1 addition & 1 deletion ytdlbot/engine/base.py
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ def __init__(self, client: Types.Client, url: str, user_id: int, _id: int):
self._user_id = user_id
self._id = _id
self._tempdir = tempfile.TemporaryDirectory(prefix="ytdl-")
self._bot_msg = self._client.get_messages(self._user_id, self._id)
self._bot_msg: Types.Message = self._client.get_messages(self._user_id, self._id)

def __del__(self):
self._tempdir.cleanup()