diff --git a/MANIFEST.in b/MANIFEST.in index 100375d7..4a750253 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,7 +1,6 @@ ## Include include README.md COPYING COPYING.lesser NOTICE requirements.txt recursive-include compiler *.py *.tl *.tsv *.txt -recursive-include pyrogram mime.types schema.sql ## Exclude prune pyrogram/errors/exceptions diff --git a/pyrogram/mime.types b/pyrogram/mime_types.py similarity index 98% rename from pyrogram/mime.types rename to pyrogram/mime_types.py index bb8f6fdb..69e69df8 100644 --- a/pyrogram/mime.types +++ b/pyrogram/mime_types.py @@ -1,3 +1,24 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2020 Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +# From https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types. +# Extended with extra mime types specific to Telegram. +mime_types = """ # This file maps Internet media types to unique file extension(s). # Although created for httpd, this file is used by many software systems # and has been placed in the public domain for unlimited redistribution. @@ -1855,4 +1876,6 @@ video/x-smv smv x-conference/x-cooltalk ice # Telegram animated stickers -application/x-tgsticker tgs +application/x-bad-tgsticker tgs +application/x-tgsticker tgs +""" diff --git a/pyrogram/scaffold.py b/pyrogram/scaffold.py index 395ad7d3..7d679668 100644 --- a/pyrogram/scaffold.py +++ b/pyrogram/scaffold.py @@ -21,6 +21,7 @@ import os import platform import re import sys +from io import StringIO from mimetypes import MimeTypes from pathlib import Path @@ -28,6 +29,7 @@ import pyrogram from pyrogram import __version__ from pyrogram.parser import Parser from pyrogram.session.internals import MsgId +from .mime_types import mime_types class Scaffold: @@ -46,7 +48,8 @@ class Scaffold: PARSE_MODES = ["combined", "markdown", "md", "html", None] - mimetypes = MimeTypes((f"{os.path.dirname(__file__)}/mime.types",)) + mimetypes = MimeTypes() + mimetypes.readfp(StringIO(mime_types)) def __init__(self): try: diff --git a/pyrogram/storage/schema.sql b/pyrogram/storage/schema.sql deleted file mode 100644 index 39c986b5..00000000 --- a/pyrogram/storage/schema.sql +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Pyrogram - Telegram MTProto API Client Library for Python - * Copyright (C) 2017-2020 Dan - * - * This file is part of Pyrogram. - * - * Pyrogram is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Pyrogram is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Pyrogram. If not, see . - */ - -CREATE TABLE sessions -( - dc_id INTEGER PRIMARY KEY, - test_mode INTEGER, - auth_key BLOB, - date INTEGER NOT NULL, - user_id INTEGER, - is_bot INTEGER -); - -CREATE TABLE peers -( - id INTEGER PRIMARY KEY, - access_hash INTEGER, - type INTEGER NOT NULL, - username TEXT, - phone_number TEXT, - last_update_on INTEGER NOT NULL DEFAULT (CAST(STRFTIME('%s', 'now') AS INTEGER)) -); - -CREATE TABLE version -( - number INTEGER PRIMARY KEY -); - -CREATE INDEX idx_peers_id ON peers (id); -CREATE INDEX idx_peers_username ON peers (username); -CREATE INDEX idx_peers_phone_number ON peers (phone_number); - -CREATE TRIGGER trg_peers_last_update_on - AFTER UPDATE - ON peers -BEGIN - UPDATE peers - SET last_update_on = CAST(STRFTIME('%s', 'now') AS INTEGER) - WHERE id = NEW.id; -END; \ No newline at end of file diff --git a/pyrogram/storage/sqlite_storage.py b/pyrogram/storage/sqlite_storage.py index 2f3768df..e9f401b6 100644 --- a/pyrogram/storage/sqlite_storage.py +++ b/pyrogram/storage/sqlite_storage.py @@ -19,7 +19,6 @@ import inspect import sqlite3 import time -from pathlib import Path from threading import Lock from typing import List, Tuple, Any @@ -27,6 +26,47 @@ from pyrogram import raw from .storage import Storage from .. import utils +# language=SQLite +SCHEMA = """ +CREATE TABLE sessions +( + dc_id INTEGER PRIMARY KEY, + test_mode INTEGER, + auth_key BLOB, + date INTEGER NOT NULL, + user_id INTEGER, + is_bot INTEGER +); + +CREATE TABLE peers +( + id INTEGER PRIMARY KEY, + access_hash INTEGER, + type INTEGER NOT NULL, + username TEXT, + phone_number TEXT, + last_update_on INTEGER NOT NULL DEFAULT (CAST(STRFTIME('%s', 'now') AS INTEGER)) +); + +CREATE TABLE version +( + number INTEGER PRIMARY KEY +); + +CREATE INDEX idx_peers_id ON peers (id); +CREATE INDEX idx_peers_username ON peers (username); +CREATE INDEX idx_peers_phone_number ON peers (phone_number); + +CREATE TRIGGER trg_peers_last_update_on + AFTER UPDATE + ON peers +BEGIN + UPDATE peers + SET last_update_on = CAST(STRFTIME('%s', 'now') AS INTEGER) + WHERE id = NEW.id; +END; +""" + def get_input_peer(peer_id: int, access_hash: int, peer_type: str): if peer_type in ["user", "bot"]: @@ -61,8 +101,7 @@ class SQLiteStorage(Storage): def create(self): with self.lock, self.conn: - with open(str(Path(__file__).parent / "schema.sql"), "r") as schema: - self.conn.executescript(schema.read()) + self.conn.executescript(SCHEMA) self.conn.execute( "INSERT INTO version VALUES (?)",