Abhaykoul commited on
Commit
2f42528
·
verified ·
1 Parent(s): f85d786

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -90,17 +90,17 @@ def _normalize_url(url: str) -> str:
90
  """Unquote URL and replace spaces with '+'."""
91
  return unquote(url.replace(" ", "+")) if url else ""
92
 
93
- logger = logging.getLogger("duckduckgo_search.AsyncDDGS")
94
  # Not working on Windows, NotImplementedError (https://curl-cffi.readthedocs.io/en/latest/faq/)
95
  if sys.platform.lower().startswith("win"):
96
  asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
97
 
98
 
99
- class AsyncDDGS(metaclass=GoogleDocstringInheritanceMeta):
100
  """webscout_search async class to get search results from duckduckgo.com."""
101
 
102
  def __init__(self, headers=None, proxies=None, timeout=10) -> None:
103
- """Initialize the AsyncDDGS object.
104
 
105
  Args:
106
  headers (dict, optional): Dictionary of headers for the HTTP client. Defaults to None.
@@ -115,7 +115,7 @@ class AsyncDDGS(metaclass=GoogleDocstringInheritanceMeta):
115
  )
116
  self._asession.headers["Referer"] = "https://duckduckgo.com/"
117
 
118
- async def __aenter__(self) -> "AsyncDDGS":
119
  """A context manager method that is called when entering the 'with' statement."""
120
  return self
121
 
@@ -938,18 +938,18 @@ class AsyncDDGS(metaclass=GoogleDocstringInheritanceMeta):
938
  page_data = None
939
  return page_data
940
 
941
- logger = logging.getLogger("duckduckgo_search.DDGS")
942
  nest_asyncio.apply()
943
 
944
 
945
- class DDGS(AsyncDDGS):
946
  def __init__(self, headers=None, proxies=None, timeout=10):
947
  if asyncio.get_event_loop().is_running():
948
- warnings.warn("DDGS running in an async loop. This may cause errors. Use AsyncDDGS instead.", stacklevel=2)
949
  super().__init__(headers, proxies, timeout)
950
  self._loop = asyncio.get_event_loop()
951
 
952
- def __enter__(self) -> "DDGS":
953
  return self
954
 
955
  def __exit__(self, exc_type, exc_val, exc_tb) -> None:
@@ -996,7 +996,7 @@ class DDGS(AsyncDDGS):
996
  return self._loop.run_until_complete(async_coro)
997
 
998
  import g4f
999
- from webscout import DDGS
1000
  from time import time as t
1001
  from flask import Flask, jsonify, request
1002
  app = Flask(__name__)
@@ -1009,9 +1009,9 @@ def webscout2_search():
1009
 
1010
  query = data['query']
1011
 
1012
- with DDGS() as ddgs:
1013
  responses = []
1014
- for i, r in enumerate(ddgs.text(query, region='wt-wt', safesearch='off', timelimit='y')):
1015
  if i == 10: # Limiting the results to 10
1016
  break
1017
  responses.append(r)
@@ -1030,9 +1030,9 @@ def webscout_videos():
1030
 
1031
  keywords = params['keywords']
1032
 
1033
- with DDGS() as ddgs:
1034
  responses = []
1035
- for r in ddgs.videos(
1036
  keywords,
1037
  region="wt-wt",
1038
  safesearch="off",
@@ -1052,9 +1052,9 @@ def webscout2_images():
1052
 
1053
  keywords = params['keywords']
1054
 
1055
- with DDGS() as ddgs:
1056
  responses = []
1057
- for r in ddgs.images(
1058
  keywords,
1059
  region="wt-wt",
1060
  safesearch="off",
@@ -1076,9 +1076,9 @@ def webscout_news():
1076
 
1077
  keywords = params['keywords']
1078
 
1079
- with DDGS() as ddgs:
1080
  responses = []
1081
- for r in ddgs.news(
1082
  keywords,
1083
  region="wt-wt",
1084
  safesearch="off",
@@ -1094,10 +1094,10 @@ def webscout3_search():
1094
  if not query:
1095
  return jsonify({'error': 'Query parameter missing'}), 400
1096
 
1097
- # Initialize the DDGS (DuckDuckGo Search) object
1098
- with DDGS() as ddgs:
1099
  responses = []
1100
- for i, r in enumerate(ddgs.text(query, region='wt-wt', safesearch='off', timelimit='y')):
1101
  if i == 2: # Limiting the results to 10
1102
  break
1103
  responses.append(r)
@@ -1116,8 +1116,8 @@ def webscout_translate():
1116
  keywords = params['keywords']
1117
  target_language = params['to']
1118
 
1119
- with DDGS() as ddgs:
1120
- translation = ddgs.translate(keywords, to=target_language)
1121
  return jsonify(translation)
1122
 
1123
  @app.route('/chat', methods=['POST'])
 
90
  """Unquote URL and replace spaces with '+'."""
91
  return unquote(url.replace(" ", "+")) if url else ""
92
 
93
+ logger = logging.getLogger("duckduckgo_search.AsyncWEBS")
94
  # Not working on Windows, NotImplementedError (https://curl-cffi.readthedocs.io/en/latest/faq/)
95
  if sys.platform.lower().startswith("win"):
96
  asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
97
 
98
 
99
+ class AsyncWEBS(metaclass=GoogleDocstringInheritanceMeta):
100
  """webscout_search async class to get search results from duckduckgo.com."""
101
 
102
  def __init__(self, headers=None, proxies=None, timeout=10) -> None:
103
+ """Initialize the AsyncWEBS object.
104
 
105
  Args:
106
  headers (dict, optional): Dictionary of headers for the HTTP client. Defaults to None.
 
115
  )
116
  self._asession.headers["Referer"] = "https://duckduckgo.com/"
117
 
118
+ async def __aenter__(self) -> "AsyncWEBS":
119
  """A context manager method that is called when entering the 'with' statement."""
120
  return self
121
 
 
938
  page_data = None
939
  return page_data
940
 
941
+ logger = logging.getLogger("duckduckgo_search.WEBS")
942
  nest_asyncio.apply()
943
 
944
 
945
+ class WEBS(AsyncWEBS):
946
  def __init__(self, headers=None, proxies=None, timeout=10):
947
  if asyncio.get_event_loop().is_running():
948
+ warnings.warn("WEBS running in an async loop. This may cause errors. Use AsyncWEBS instead.", stacklevel=2)
949
  super().__init__(headers, proxies, timeout)
950
  self._loop = asyncio.get_event_loop()
951
 
952
+ def __enter__(self) -> "WEBS":
953
  return self
954
 
955
  def __exit__(self, exc_type, exc_val, exc_tb) -> None:
 
996
  return self._loop.run_until_complete(async_coro)
997
 
998
  import g4f
999
+ from webscout import WEBS
1000
  from time import time as t
1001
  from flask import Flask, jsonify, request
1002
  app = Flask(__name__)
 
1009
 
1010
  query = data['query']
1011
 
1012
+ with WEBS() as WEBS:
1013
  responses = []
1014
+ for i, r in enumerate(WEBS.text(query, region='wt-wt', safesearch='off', timelimit='y')):
1015
  if i == 10: # Limiting the results to 10
1016
  break
1017
  responses.append(r)
 
1030
 
1031
  keywords = params['keywords']
1032
 
1033
+ with WEBS() as WEBS:
1034
  responses = []
1035
+ for r in WEBS.videos(
1036
  keywords,
1037
  region="wt-wt",
1038
  safesearch="off",
 
1052
 
1053
  keywords = params['keywords']
1054
 
1055
+ with WEBS() as WEBS:
1056
  responses = []
1057
+ for r in WEBS.images(
1058
  keywords,
1059
  region="wt-wt",
1060
  safesearch="off",
 
1076
 
1077
  keywords = params['keywords']
1078
 
1079
+ with WEBS() as WEBS:
1080
  responses = []
1081
+ for r in WEBS.news(
1082
  keywords,
1083
  region="wt-wt",
1084
  safesearch="off",
 
1094
  if not query:
1095
  return jsonify({'error': 'Query parameter missing'}), 400
1096
 
1097
+ # Initialize the WEBS (DuckDuckGo Search) object
1098
+ with WEBS() as WEBS:
1099
  responses = []
1100
+ for i, r in enumerate(WEBS.text(query, region='wt-wt', safesearch='off', timelimit='y')):
1101
  if i == 2: # Limiting the results to 10
1102
  break
1103
  responses.append(r)
 
1116
  keywords = params['keywords']
1117
  target_language = params['to']
1118
 
1119
+ with WEBS() as WEBS:
1120
+ translation = WEBS.translate(keywords, to=target_language)
1121
  return jsonify(translation)
1122
 
1123
  @app.route('/chat', methods=['POST'])