-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
from datetime import datetime | ||
from typing import Literal, TypedDict | ||
|
||
from pydantic import BaseModel | ||
import peewee | ||
import shortuuid | ||
|
||
db = peewee.SqliteDatabase('database.db') | ||
db.autoconnect = True | ||
|
||
|
||
class BaseModel(peewee.Model): | ||
id = peewee.CharField(primary_key=True,max_length=128, default=lambda: shortuuid.uuid(pad_length=128)) | ||
|
||
class Meta: | ||
database = db | ||
|
||
|
||
class File(BaseModel): | ||
folder: bool | ||
name: str | ||
path: str | ||
id: str | ||
name = peewee.CharField(max_length=128) | ||
path = peewee.CharField(max_length=128) | ||
folder = peewee.BooleanField(default=False) | ||
|
||
|
||
class Folder(BaseModel): | ||
this = peewee.ForeignKeyField(File, backref="this_folder", on_delete="CASCADE") | ||
sub_folders = peewee.ManyToManyField(File, backref="parent_folder", on_delete="CASCADE") | ||
files = peewee.ManyToManyField(File, backref="folder", on_delete="CASCADE") | ||
|
||
|
||
class Task(BaseModel): | ||
id: str | ||
name: str | ||
status: Literal["running", "stopped", "finished"] | ||
name = peewee.CharField(max_length=128) | ||
status = peewee.CharField(choices=["running", "completed", "failed"], default="running") | ||
schedule_time = peewee.DateTimeField(default=datetime.now) | ||
|
||
|
||
FolderSubFolder = Folder.sub_folders.get_through_model() | ||
FolderFile = Folder.files.get_through_model() | ||
|
||
db.create_tables([File, FolderSubFolder, FolderFile, Folder, Task]) | ||
|
||
TaskRequest = TypedDict("TaskRequest", {"path": str, "type": Literal["sync", "upload"]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from pathlib import Path | ||
|
||
from telethon import TelegramClient | ||
from telethon.tl.types import InputMessagesFilterDocument | ||
|
||
from gramup.models import File, Folder, Task | ||
|
||
|
||
async def pull_all_to_db(task: Task): | ||
client = TelegramClient("session", 1234, "asdf") | ||
|
||
if not client.is_connected(): | ||
await client.connect() | ||
|
||
if not await client.is_user_authorized(): | ||
task.status = "failed" | ||
task.save() | ||
return | ||
|
||
for message in client.iter_messages("me", filter=InputMessagesFilterDocument): | ||
m_id = message.id | ||
caption: str = message.caption | ||
path = Path(caption if "/" in caption else f"external/{caption}") | ||
|
||
file = File.create( | ||
id=m_id, | ||
path=str(path), | ||
name=path.name | ||
) | ||
|
||
file.save() | ||
first = True | ||
|
||
for folder in path.parents: | ||
folder_new, _ = Folder.get_or_create( | ||
path=str(folder), | ||
name=folder.name | ||
) | ||
|
||
if first: | ||
first = False | ||
folder_new.files.add(file) | ||
else: | ||
folder_new.sub_folders.add(file) | ||
|
||
folder_new.save() | ||
file = folder_new | ||
|
||
task.status = "completed" | ||
task.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
fastapi==0.96.0 | ||
starlette~=0.27.0 | ||
Telethon~=1.28.5 | ||
|
||
pydantic~=1.10.8 | ||
shortuuid~=1.0.11 | ||
|
||
peewee~=3.16.2 |