Created new plugin type custom_results. Added new plugin bang_redirect #2027
Conversation
Interesting, really like the idea. |
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.
Overall it's a good PR. I've added some minor notes, please check it.
searx/external_bang.py
Outdated
return full_query | ||
|
||
|
||
# Dont use this variable directly but access via _get_bangs_data |
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.
Why don't use the variable directly? In the end this variable is being used anyway since _get_bang_data()
returns this variable.
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.
I prefer getters and setter but with your example below it is alternative way (your parser is way nicer/cleaner tho!)
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.
Fixed it!
searx/external_bang.py
Outdated
bangs_data = None | ||
|
||
|
||
def _get_bangs_data(): |
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.
a bit more compact and pythonish definition of this section:
bangs_data = {}
with open(join(searx_dir, 'data/bangs.json')) as json_file:
for bang in json.load(json_file)['bang']:
for trigger in bang["triggers"]:
bangs_data[trigger] = {x:y for x,y in bang.items() if x != "triggers"}
No need to create a loader function, this way bangs_data
is populated immediately after starting the application, then we can use it freely.
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.
Fixed it!
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.
bangs_data[trigger] = {x:y for x,y in bang.items() if x != "triggers"}
Wow I did not know you could do that in 1 line btw. Learned something new thanks!
searx/external_bang.py
Outdated
:param user_bang: The parsed user bang. For example yt | ||
:return: Returns a dict with bangs data (check bangs_data.json for the structure) | ||
""" | ||
try: |
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.
Here you can simply write return _get_bangs_data().get(user_bang)
. dict().get(key)
returns None
if the given key doesn't found.
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.
true that.
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.
Fixed it!
Tests are failing because the |
…arch with external_bang
Oh got it! Added the mock all tests are passing now. Thanks for the tip!
I agree and I refactored it.
I did not know this existed but I tried to implement it(don't know If I did it correctly). The you link sended for fixup was helpful for the explanation but I did not got it working. I followed this video maybe is good to add to reference in future PR's maybe? @asciimoo |
Looks pretty good to me! |
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.
Looks good to me, thanks!
Im really grateful to have worked with you guys @dalf and @asciimoo ! You provided good feedback and gave really good insights of how the code should be structured to made this PR work (learned some stuff to apply to my own projects). One last thing. Should I add documentation and if so what should I include? |
I can write the documentation .. I want to auto generate the list of bangs in the docs, this might be to complicate if this is the first time you are using the sphinx framework (reST). |
@return42 If you want to do that would be awesome, since my English and writting skills are not so great..... But if you have any questions I am always available to help! |
@lukasvdberk believe me, it is better than mine ;) To document the bangs I send PR #2046. You might have been noticed that I also opened issue #2045 in which I call for a significant reduction of bangs (redirect URLs) .. your thoughts and remarks are welcome / thanks! |
TLDR
Detailed version
I really like Searx and have only been using it for 2 weeks. Coming from DuckDuckGo I really liked their implementation of bangs and wanted to recreate that for Searx. Their implementation is when you enter a bang it redirects directly to the site with your submitted query. Maybe I have missed something but I could not find that for Searx. I also wanted more bangs out of the box. Thats why I used by own bangs config data (located in searx > plugins > bangs_data > bangs.json).
For all of this to work I created a new plugin type called custom_results (which is documented and anyone can use ). With this you can return a custom flask return type so you can for example redirect (like I used) or show a custom page. It runs when a user submitted a query (just like post_search) and will cancel the rest of the usual search flow when it does not return None.
I also added unit_tests in tests > unit > test_plugins > SelfBangTest. This will test my Bang redirect plugin for all of its features. The tests ran successfully on my machine with python 3.7
I retrieved my bangs data from the following project , the data file (I converted the TOML file to a JSON file).
If you want to see all of this working you can checkout my instance at https://searx.lukashisprojects.rocks/. Make sure to enable the plugin in the preferences section.
Personal note
I am still a cs-student and this is my first opensource contribution. I really love this project and I hope I have made a useful contribution. If there is anything I can do to make this PR work I am willing to go the extra mile.