forked from Helias/Telegram-DMI-Bot
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: added a telegram mini-app * running web server in main * fix: applied suggestions and pylint complaints
- Loading branch information
1 parent
2ec5b9e
commit 4105e39
Showing
27 changed files
with
5,518 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,3 +130,7 @@ pydrive/* | |
.idea | ||
data/json/subjs.json | ||
**/.DS_Store | ||
|
||
# Webapp | ||
node_modules/ | ||
.parcel-cache/ |
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import yaml | ||
from typing import Optional | ||
from pydrive2.auth import GoogleAuth | ||
from pydrive2.drive import GoogleDrive | ||
from pydrive2.files import GoogleDriveFileList, GoogleDriveFile | ||
from module.debug import log_error | ||
|
||
|
||
class DriveUtils: | ||
def __init__(self): | ||
self._gdrive = None | ||
with open('config/settings.yaml', 'r', encoding='utf-8') as yaml_config: | ||
self.config_map = yaml.load(yaml_config, Loader=yaml.SafeLoader) | ||
|
||
@property | ||
def gdrive(self) -> GoogleDrive: | ||
'Returns the active drive.GoogleDrive instance.' | ||
if self._gdrive is None: | ||
# gauth uses all the client_config of settings.yaml | ||
gauth = GoogleAuth(settings_file="./config/settings.yaml") | ||
gauth.CommandLineAuth() | ||
self._gdrive = GoogleDrive(gauth) | ||
return self._gdrive | ||
|
||
def list_files(self, folder_id: Optional[str] = None) -> Optional[GoogleDriveFileList]: | ||
'Returns a list of files or folders in the given directory' | ||
folder_id = folder_id or self.config_map['drive_folder_id'] | ||
try: | ||
return self.gdrive.ListFile({ | ||
'q': f"'{folder_id}' in parents and trashed=false", | ||
'orderBy': 'folder,title', | ||
}).GetList() | ||
# pylint: disable=broad-except | ||
except Exception as e: | ||
log_error(header="drive_handler", error=e) | ||
return None | ||
|
||
def get_file(self, file_id: str) -> GoogleDriveFile: | ||
'Shorthand for self.gdrive.CreateFile' | ||
return self.gdrive.CreateFile({'id': file_id}) | ||
|
||
|
||
drive_utils = DriveUtils() |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": { | ||
"tailwindcss": {} | ||
} | ||
} |
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 fastapi import FastAPI | ||
from fastapi.staticfiles import StaticFiles | ||
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse | ||
from pydrive2.files import MediaIoReadable | ||
from starlette.responses import ContentStream | ||
from module.utils.drive_utils import drive_utils | ||
|
||
|
||
app = FastAPI() | ||
|
||
@app.get('/favicon.ico') | ||
def favicon(): | ||
'Serve the website favicon' | ||
return FileResponse('webapp/static/assets/logo.ico') | ||
|
||
@app.get('/drive/folder') | ||
def _(folder_id: str): | ||
'Returns content of a folder in the DMI Drive.' | ||
files = drive_utils.list_files(folder_id) or [] | ||
keys = 'id', 'title', 'mimeType' | ||
response = JSONResponse([{key: file[key] for key in keys} for file in files]) | ||
response.headers['Access-Control-Allow-Origin'] = '*' | ||
return response | ||
|
||
@app.get('/drive/file') | ||
def _(file_id: str): | ||
'Returns content of a file in the DMI Drive.' | ||
file = drive_utils.get_file(file_id) | ||
if not file: | ||
return JSONResponse({'error': 'File not found.'}, status_code = 204) | ||
content = file.GetContentIOBuffer() | ||
return StreamingResponse( | ||
stream(content), | ||
media_type = file['mimeType'], | ||
headers = { | ||
# delivering it as a download | ||
'Content-Disposition': f"attachment; filename=\"{file['title']}\"", | ||
# making it accessible from web browsers | ||
'Access-Control-Allow-Origin': '*' | ||
} | ||
) | ||
|
||
def stream(content: MediaIoReadable) -> ContentStream: | ||
chunk = True | ||
while chunk: | ||
chunk = content.read() | ||
if chunk: | ||
yield chunk | ||
|
||
app.mount('/', StaticFiles(directory = 'webapp/dist/', html = True), name = 'dist') |
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,48 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>DMI Drive</title> | ||
<meta name = "viewport" content = "width=device-width, initial-scale=1" /> | ||
<link rel = "stylesheet" href = "static/style.css"> | ||
<script src = "https://telegram.org/js/telegram-web-app.js"></script> | ||
<link href='https://unpkg.com/[email protected]/icons/css/folder.css' rel='stylesheet'> | ||
<script type = "module" src = "static/scripts/index.ts"></script> | ||
<script type = "module" src = "static/scripts/back.ts"></script> | ||
</head> | ||
<body> | ||
<header class = "w-full mt-10"> | ||
<div class = "w-full flex flex-row justify-center"> | ||
<img width = "100" src = "https://upload.wikimedia.org/wikipedia/commons/1/12/Google_Drive_icon_%282020%29.svg"> | ||
<!-- <img width = "150" src = "../static/assets/logo.png"> --> | ||
</div> | ||
</header> | ||
<div class = "w-full flex flex-col items-center justify-center"> | ||
<folders-div id = "folders-list"> | ||
<drive-back-button class = "folder hidden" id = "back-button"></drive-back-button> | ||
<!-- <folder id = "folder" class = "bg-neutral-200 w-full h-10 p-6 flex flex-row items-center shadow-md hover:shadow-lg transition-shadow cursor-pointer rounded-md"> | ||
<i class = "mr-5 gg-folder text-neutral-500"></i> | ||
<folder-name class = "font-bold text-neutral-700">Informatica</folder-name> | ||
</folder> --> | ||
</folders-div> | ||
<files-div id = "files-list"> | ||
<!-- <file id = "file" class = "bg-neutral-300 flex flex-col p-5 shadow-md hover:shadow-lg transition-shadow cursor-pointer rounded-md"> | ||
<img class = "w-32 rounded-md rounded-bl-[28px]" src = "https://telegra.ph/file/cc429f1e5d235f54a4500.png"> | ||
<file-name class = "mt-5 font-bold text-neutral-900">Appunti.pdf</file-name> | ||
</file> --> | ||
</files-div> | ||
</div> | ||
</body> | ||
<!-- <pwa> | ||
<meta name = "theme-color" content = "#ffffff"> | ||
<link rel = "manifest" href = "static/web/manifest.json"> | ||
<link sizes = "500x500" rel = "apple-touch-icon" href = "static/assets/logo.png"> | ||
<script type = "module"> | ||
document.addEventListener('DOMContentLoaded', () => { | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.register(new URL('static/web/sw.js', import.meta.url), {scope: '/'}); | ||
} | ||
}); | ||
</script> | ||
</pwa> --> | ||
</html> |
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,33 @@ | ||
import { Folder, FOLDERMIME } from "./folder"; | ||
import { update_content } from "./update"; | ||
|
||
export class BackButton extends Folder { | ||
foldersHistory = new Array<string>(); | ||
|
||
connectedCallback(): void { | ||
this.update({ | ||
title: '..', | ||
id: 'back', | ||
mimeType: FOLDERMIME | ||
}) | ||
} | ||
|
||
onFolderChange(folderId: string): void { | ||
this.foldersHistory.push(folderId); | ||
if (folderId != '') { | ||
this.show(); | ||
} else { | ||
this.hide(); | ||
} | ||
} | ||
|
||
override onClick(): void { | ||
const previousFolderId = this.foldersHistory[this.foldersHistory.length - 2]; | ||
// Removing the last folder id in the history | ||
this.foldersHistory.splice(this.foldersHistory.length - 2); | ||
// Showing content of the previous folder | ||
update_content(previousFolderId); | ||
} | ||
} | ||
|
||
window.customElements.define('drive-back-button', BackButton); |
Oops, something went wrong.