-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fixed version to PR #880 #889
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
Changes from all commits
4872373
94a9d6e
697769d
9ae0b04
6d35725
b6719f6
a5f8f9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,8 @@ | |
| # You should have received a copy of the GNU Lesser Public License | ||
| # along with this program. If not, see [http://www.gnu.org/licenses/]. | ||
| """This module contains the Filters for use with the MessageHandler class.""" | ||
| from telegram import Chat | ||
| from future.utils import string_types | ||
| from telegram import Chat | ||
|
|
||
|
|
||
| class BaseFilter(object): | ||
|
|
@@ -192,12 +192,126 @@ def filter(self, message): | |
| class _Document(BaseFilter): | ||
| name = 'Filters.document' | ||
|
|
||
| class category(BaseFilter): | ||
| """This Filter filters documents by their category in the mime-type attribute | ||
|
|
||
| Note: | ||
| This Filter only filters by the mime_type of the document, | ||
| it doesn't check the validity of the document. | ||
| The user can manipulate the mime-type of a message and | ||
| send media with wrong types that don't fit to this handler. | ||
|
|
||
| Examples: | ||
| Filters.documents.category("audio/") returnes `True` for all types | ||
| of audio sent as file, for example "audio/mpeg" or "audio/x-wav" | ||
| """ | ||
|
|
||
| def __init__(self, category): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps it would be clearer if we renamed the |
||
| """Initialize the category you want to filter | ||
|
|
||
| Args: | ||
| category (str, optional): category of the media you want to filter""" | ||
| self.category = category | ||
| self.name = "Filters.document.category(\"{}\")".format(self.category) | ||
|
|
||
| def filter(self, message): | ||
| if message.document: | ||
| return message.document.mime_type.startswith(self.category) | ||
|
|
||
| application = category("application/") | ||
| audio = category("audio/") | ||
| image = category("image/") | ||
| video = category("video/") | ||
| text = category("text/") | ||
|
|
||
| class file_type(BaseFilter): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and perhaps even name the filter
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will change |
||
| """This Filter filters documents by their mime-type attribute | ||
|
|
||
| Note: | ||
| This Filter only filters by the mime_type of the document, | ||
| it doesn't check the validity of document. | ||
| The user can manipulate the mime-type of a message and | ||
| send media with wrong types that don't fit to this handler. | ||
|
|
||
| Examples: | ||
| Filters.documents.file_type("audio/mpeg") filters all audio in mp3 format. | ||
| """ | ||
|
|
||
| def __init__(self, filetype): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, makes sense |
||
| """Initialize the category you want to filter | ||
|
|
||
| Args: | ||
| filetype (str, optional): mime_type of the media you want to filter""" | ||
| self.filetype = filetype | ||
| self.name = "Filters.document.Filetype(\"{}\")".format(self.filetype) | ||
|
|
||
| def filter(self, message): | ||
| if message.document: | ||
| return message.document.mime_type == self.filetype | ||
|
|
||
| apk = file_type("application/vnd.android.package-archive") | ||
| doc = file_type("application/msword") | ||
| docx = file_type("application/vnd.openxmlformats-officedocument.wordprocessingml.document") | ||
| exe = file_type("application/x-ms-dos-executable") | ||
| gif = file_type("video/mp4") | ||
| jpg = file_type("image/jpeg") | ||
| mp3 = file_type("audio/mpeg") | ||
| pdf = file_type("application/pdf") | ||
| py = file_type("text/x-python") | ||
| svg = file_type("image/svg+xml") | ||
| txt = file_type("text/plain") | ||
| targz = file_type("application/x-compressed-tar") | ||
| wav = file_type("audio/x-wav") | ||
| xml = file_type("application/xml") | ||
| zip = file_type("application/zip") | ||
|
|
||
| def filter(self, message): | ||
| return bool(message.document) | ||
|
|
||
| document = _Document() | ||
| """:obj:`Filter`: Messages that contain :class:`telegram.Document`.""" | ||
|
|
||
| class file_size(BaseFilter): | ||
| """This Filter filters all messages with a `file_size` attribute.""" | ||
|
|
||
| def __init__(self, min=None, max=None): | ||
| """Initialize the limits of the file_size in the `__init__` method. | ||
|
|
||
| Note: | ||
| If no limits are given, the filter returns `True` for every message | ||
| with file_size attritute | ||
|
|
||
| Args: | ||
| min (int, optional): Minimum `file_size` of the message media in Byte. | ||
| max (int, optional): Maximum `file_size` of the message media in Byte. | ||
| """ | ||
| self.min = min | ||
| self.max = max | ||
| self.name = "Filters.file_size(min={0}, max={1})".format(self.min, self.max) | ||
|
|
||
| def filter(self, message): | ||
| if message.audio: | ||
| filesize = message.audio.file_size | ||
| elif message.document: | ||
| filesize = message.document.file_size | ||
| elif message.photo: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, thanks. Didn't thought about that. What about just using the photo in the highest resolution? |
||
| filesize = message.photo.file_size | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this line will fail |
||
| elif message.sticker: | ||
| filesize = message.sticker.file_size | ||
| elif message.video: | ||
| filesize = message.video.file_size | ||
| elif message.voice: | ||
| filesize = message.voice.file_size | ||
| else: | ||
| return False | ||
| if self.min is not None: | ||
| if filesize < self.min: | ||
| return False | ||
| if self.max is not None: | ||
| if filesize > self.max: | ||
| return False | ||
| return True | ||
|
|
||
| class _Photo(BaseFilter): | ||
| name = 'Filters.photo' | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't put an
Args:section, and you should specifically mention thatmime_type.startswithis usedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see you put the
Args:in the__init__, sorry