Skip to content

Conversation

@spontanurlaub
Copy link
Contributor

@spontanurlaub spontanurlaub commented Oct 18, 2017

Adding new subfilters to Filters.documents that allow to filter for different mime_type categories or file types.

Adding new Category Filter for these file categories:
application/
audio/
image/
video/
text/

Adding Filters for these file types:
apk, docx, exe, gif, jpg, mp3, pdf, png, py, svg, txt, targz, wav, xml, zip

Adding Filters.FileSize(min=0, max=sys.maxsize) to filter all types of media by it's file_size.

The Category Filter and the FileType Filter are visible, because it should be possible to create own Filters for other mime_types.

This solves Issue #875

Contact for questions: t.me/bergfreak

@codecov
Copy link

codecov bot commented Oct 19, 2017

Codecov Report

Merging #880 into master will increase coverage by 0.06%.
The diff coverage is 74.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #880      +/-   ##
==========================================
+ Coverage   91.31%   91.38%   +0.06%     
==========================================
  Files         101      101              
  Lines        4055     4132      +77     
  Branches      620      628       +8     
==========================================
+ Hits         3703     3776      +73     
- Misses        204      217      +13     
+ Partials      148      139       -9
Flag Coverage Δ
#Appveyor 86.64% <74.35%> (?)
#Travis 90.97% <74.35%> (-0.35%) ⬇️
Impacted Files Coverage Δ
telegram/constants.py 100% <100%> (ø) ⬆️
telegram/ext/filters.py 92.14% <64.28%> (-6.97%) ⬇️
telegram/ext/jobqueue.py 91.35% <0%> (-0.55%) ⬇️
telegram/utils/request.py 66.96% <0%> (+0.89%) ⬆️
telegram/message.py 96.22% <0%> (+5.28%) ⬆️
telegram/games/game.py 100% <0%> (+5.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bb5357a...ef6861e. Read the comment docs.

Copy link
Member

@jsmnbom jsmnbom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preliminary review.
Mostly looks good, seems like useful filters to have in many situations :D
I have a few comments I'd like you to review :)

text = "text/"


class Document:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good reason you're not using the stdlib mimetype?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just didn't found them. I will fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stdlib mimetype doesn't work for many mimetypes in telegram. ".exe" documents for example have the mimetype "application/x-ms-dos-executable" if you send it with an official telegram app, but ".exe" is matched to "application/octet-stream" in the stdlib mimetype.

if message.document:
return bool(message.document.mime_type.startswith(self.category))

application = Category(constants.MediaCategory.application)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we perhaps expose these as filters.document.category.application instead of just filters.document.application?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bomjacob
is this nesting really necessary? I think that filters.document.application is much more easier to use.

if message.document:
return bool(message.document.mime_type == self.filetype)

apk = FileType(constants.Document.apk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we perhaps expose these as filters.document.filetype.apk instead of just filters.document.apk?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's much more nesting. Not sure we really need this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no nesting is okay :)

"""
name = "Filters.FileSize()"

def __init__(self, min=0, max=sys.maxsize):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of using sys.maxsize. Could you instead have it default to None and if it is None just don't compare upper limit?

self.max = max

def filter(self, message):
if bool(message.audio):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for the bool() in these if statements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will leave it

Examples:
Filters.documents.FileType("audio/mpeg") filters all audio in mp3 format.
"""
name = "Filters.document.Filetype()"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix it

document = _Document()
""":obj:`Filter`: Messages that contain :class:`telegram.Document`."""

class FileSize(BaseFilter):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowercasing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again PEP8

class FileSize(BaseFilter):
"""This Filter filters all messages with a `file_size` attribute.
"""
name = "Filters.FileSize()"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about name

filesize = message.voice.file_size
else:
return False
return bool(self.min <= filesize <= self.max)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also no need for that bool()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave it too. Fixed PR will come tomorrow. Thank you for your feedback.

MAX_INLINE_QUERY_RESULTS = 50


class MediaCategory:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put these constants in the filters.py, since that's the only place they are used and they aren't meant to be exported to the general user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary if I use built-in mimetypes

@jsmnbom
Copy link
Member

jsmnbom commented Oct 26, 2017

Does #889 mean this one should be closed, @code1mountain ?

@Eldinnie Eldinnie assigned Eldinnie and unassigned Eldinnie Nov 1, 2017
@Eldinnie Eldinnie added the 📋 pending-reply work status: pending-reply label Nov 1, 2017
@Eldinnie
Copy link
Member

Eldinnie commented Dec 5, 2017

@code1mountain please respond

@jsmnbom
Copy link
Member

jsmnbom commented Dec 29, 2017

@code1mountain we're gonna go ahead and close this fairly soon if we receive no reply. It's been pending reply for almost 2 months now. Just give us a heads up if you're still working on it ^^

@tsnoam
Copy link
Member

tsnoam commented Feb 12, 2018

closing as of no response from the author of the PR

@tsnoam tsnoam closed this Feb 12, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Aug 22, 2020
@Bibo-Joshi Bibo-Joshi added 🔌 enhancement pr description: enhancement and removed enhancement labels Nov 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

🔌 enhancement pr description: enhancement 📋 pending-reply work status: pending-reply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants