123456789101112131415161718192021222324252627282930313233343536373839 |
- import argparse
- import json
- import os
-
- from beaker.cache import CacheManager
- from beaker.util import parse_cache_config_options
- from ddg import search
-
- if not os.path.exists('cache'):
- os.makedirs('cache')
- if not os.path.exists('cache/data'):
- os.makedirs('cache/data')
- if not os.path.exists('cache/lock'):
- os.makedirs('cache/lock')
-
- cache_opts = {
- 'cache.type': 'file',
- 'cache.data_dir': 'cache/data',
- 'cache.lock_dir': 'cache/lock'
- }
-
- cache = CacheManager(**parse_cache_config_options(cache_opts))
- search_result_cache = cache.get_cache('search-results')
-
- parser = argparse.ArgumentParser(
- description='Search duckduckgo and return JSON results'
- )
- parser.add_argument('query', metavar='Q', type=str,
- help='The query to search on duckduckgo')
- args = parser.parse_args()
-
-
- def get_results():
- return list(search(args.query, max_results=10))
-
- results = search_result_cache.get(key=args.query, createfunc=get_results)
-
- print(json.dumps(results))
|