wt002 commited on
Commit
e3d2492
·
verified ·
1 Parent(s): 7bc778b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -29,19 +29,14 @@ class BasicAgent:
29
  }
30
 
31
  def _get_random_user_agent(self) -> str:
32
- """Fallback user-agent generator if fake-useragent isn't installed"""
33
  browsers = [
34
- # Chrome
35
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
36
- # Firefox
37
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
38
- # Safari
39
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15'
40
  ]
41
  return random.choice(browsers)
42
 
43
  def search(self, query: str, num_results: int = 3) -> List[Dict]:
44
- """Perform Google search and return structured results"""
45
  encoded_query = urllib.parse.quote_plus(query)
46
  url = f"https://www.google.com/search?q={encoded_query}&num={num_results + 2}"
47
 
@@ -54,11 +49,9 @@ class BasicAgent:
54
  return []
55
 
56
  def _parse_results(self, html: str, max_results: int) -> List[Dict]:
57
- """Parse HTML and extract search results"""
58
  soup = BeautifulSoup(html, 'html.parser')
59
  results = []
60
 
61
- # Current Google result selectors (July 2024)
62
  for i, result in enumerate(soup.select('.tF2Cxc, .g')[:max_results]):
63
  title = result.select_one('h3, .LC20lb')
64
  link = result.find('a')['href']
@@ -71,11 +64,9 @@ class BasicAgent:
71
  'link': link if link.startswith('http') else f"https://www.google.com{link}",
72
  'snippet': snippet.get_text() if snippet else None
73
  })
74
-
75
  return results
76
 
77
  def pretty_print(self, results: List[Dict]) -> str:
78
- """Format results for human-readable output"""
79
  output = []
80
  for res in results:
81
  output.append(
@@ -85,21 +76,20 @@ class BasicAgent:
85
  )
86
  return "\n".join(output)
87
 
 
 
 
 
88
  if __name__ == "__main__":
89
- scraper = BasicAgent()
90
-
91
- # Example search
92
- query = "Python programming language"
93
- print(f"🔍 Searching Google for: '{query}'")
94
 
95
- results = scraper.search(query, num_results=3)
 
 
96
 
97
- if results:
98
- print("\nTop Results:")
99
- print(scraper.pretty_print(results))
100
- else:
101
- print("❌ No results found or search failed")
102
-
103
 
104
  def run_and_submit_all( profile: gr.OAuthProfile | None):
105
  """
 
29
  }
30
 
31
  def _get_random_user_agent(self) -> str:
 
32
  browsers = [
 
33
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
 
34
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
 
35
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15'
36
  ]
37
  return random.choice(browsers)
38
 
39
  def search(self, query: str, num_results: int = 3) -> List[Dict]:
 
40
  encoded_query = urllib.parse.quote_plus(query)
41
  url = f"https://www.google.com/search?q={encoded_query}&num={num_results + 2}"
42
 
 
49
  return []
50
 
51
  def _parse_results(self, html: str, max_results: int) -> List[Dict]:
 
52
  soup = BeautifulSoup(html, 'html.parser')
53
  results = []
54
 
 
55
  for i, result in enumerate(soup.select('.tF2Cxc, .g')[:max_results]):
56
  title = result.select_one('h3, .LC20lb')
57
  link = result.find('a')['href']
 
64
  'link': link if link.startswith('http') else f"https://www.google.com{link}",
65
  'snippet': snippet.get_text() if snippet else None
66
  })
 
67
  return results
68
 
69
  def pretty_print(self, results: List[Dict]) -> str:
 
70
  output = []
71
  for res in results:
72
  output.append(
 
76
  )
77
  return "\n".join(output)
78
 
79
+ def __call__(self, query: str) -> str:
80
+ """Added this to make the agent callable"""
81
+ return self.pretty_print(self.search(query))
82
+
83
  if __name__ == "__main__":
84
+ agent = BasicAgent()
 
 
 
 
85
 
86
+ # Now you can use either approach:
87
+ # Option 1: Call as function
88
+ print(agent("Python programming language"))
89
 
90
+ # Option 2: Use methods directly
91
+ results = agent.search("Python programming language")
92
+ print(agent.pretty_print(results))
 
 
 
93
 
94
  def run_and_submit_all( profile: gr.OAuthProfile | None):
95
  """