File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ Inline DuckDuckGo web search using the 'duckduckgo-search' library.
3+ """
4+
5+ from albert import *
6+ from pathlib import Path
7+ from duckduckgo_search import DDGS
8+ from itertools import islice
9+ from time import sleep
10+
11+ md_iid = '2.0'
12+ md_version = '1.0'
13+ md_name = 'DuckDuckGo'
14+ md_description = 'Inline DuckDuckGo web search'
15+ md_url = 'https://github.com/albertlauncher/python/duckduckgo'
16+ md_lib_dependencies = "duckduckgo-search"
17+
18+
19+ class Plugin (PluginInstance , TriggerQueryHandler ):
20+
21+ def __init__ (self ):
22+ TriggerQueryHandler .__init__ (self ,
23+ id = md_id ,
24+ name = md_name ,
25+ description = md_description ,
26+ synopsis = "<query>" ,
27+ defaultTrigger = 'ddg ' )
28+ PluginInstance .__init__ (self , extensions = [self ])
29+ self .ddg = DDGS ()
30+ self .iconUrls = [f"file:{ Path (__file__ ).parent } /duckduckgo.svg" ]
31+
32+ def handleTriggerQuery (self , query ):
33+
34+ stripped = query .string .strip ()
35+ if stripped :
36+
37+ # dont flood
38+ for number in range (25 ):
39+ sleep (0.01 )
40+ if not query .isValid :
41+ return
42+
43+ for r in islice (self .ddg .text (stripped , safesearch = 'off' ), 10 ):
44+ query .add (
45+ StandardItem (
46+ id = md_id ,
47+ text = r ['title' ],
48+ subtext = r ['body' ],
49+ iconUrls = self .iconUrls ,
50+ actions = [Action ("open" , "Open link" , lambda u = r ['href' ]: openUrl (u ))]
51+ )
52+ )
You can’t perform that action at this time.
0 commit comments