SharryOG commited on
Commit
69b2f98
1 Parent(s): 28159ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -91
app.py CHANGED
@@ -1,117 +1,106 @@
1
- import logging
2
- from typing import Optional
3
-
4
- from flask import Flask, jsonify, request
5
  from webscout import WEBS
6
 
7
- app = Flask(__name__)
8
-
9
- TIMEOUT = 10
10
- PROXY = None
11
-
12
- @app.route('/api/search', methods=['GET'])
13
- def search_text():
14
- query = request.args.get('q', '')
15
- max_results = request.args.get('max_results', 10, type=int)
16
- timelimit = request.args.get('timelimit', None)
17
- safesearch = request.args.get('safesearch', 'moderate')
18
- region = request.args.get('region', 'wt-wt')
19
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  results = []
21
  with WEBS() as webs:
22
- for result in webs.text(query, max_results=max_results, timelimit=timelimit, safesearch=safesearch, region=region):
23
  results.append(result)
 
24
 
25
- return jsonify({'results': results})
26
-
27
-
28
- @app.route('/api/images', methods=['GET'])
29
- def search_images():
30
- query = request.args.get('q', '')
31
- max_results = request.args.get('max_results', 10, type=int)
32
- safesearch = request.args.get('safesearch', 'moderate')
33
- region = request.args.get('region', 'wt-wt')
34
  results = []
35
  with WEBS() as webs:
36
- for result in webs.images(query, max_results=max_results, safesearch=safesearch, region=region):
37
  results.append(result)
 
38
 
39
- return jsonify({'results': results})
40
-
41
- @app.route('/api/videos', methods=['GET'])
42
- def search_videos():
43
- query = request.args.get('q', '')
44
- max_results = request.args.get('max_results', 10, type=int)
45
- safesearch = request.args.get('safesearch', 'moderate')
46
- region = request.args.get('region', 'wt-wt')
47
- timelimit = request.args.get('timelimit', None)
48
- resolution = request.args.get('resolution', None)
49
- duration = request.args.get('duration', None)
50
-
51
  results = []
52
  with WEBS() as webs:
53
- for result in webs.videos(query, max_results=max_results, safesearch=safesearch, region=region, timelimit=timelimit, resolution=resolution, duration=duration):
54
  results.append(result)
 
55
 
56
- return jsonify({'results': results})
57
-
58
- @app.route('/api/news', methods=['GET'])
59
- def search_news():
60
- query = request.args.get('q', '')
61
- max_results = request.args.get('max_results', 10, type=int)
62
- safesearch = request.args.get('safesearch', 'moderate')
63
- region = request.args.get('region', 'wt-wt')
64
- timelimit = request.args.get('timelimit', None)
65
-
66
  results = []
67
  with WEBS() as webs:
68
- for result in webs.news(query, max_results=max_results, safesearch=safesearch, region=region, timelimit=timelimit):
69
  results.append(result)
 
70
 
71
- return jsonify({'results': results})
72
-
73
- @app.route('/api/maps', methods=['GET'])
74
- def search_maps():
75
- query = request.args.get('q', '')
76
- place = request.args.get('place', None)
77
- max_results = request.args.get('max_results', 10, type=int)
78
-
79
  results = []
80
  with WEBS() as webs:
81
- for result in webs.maps(query, place=place, max_results=max_results):
82
  results.append(result)
 
83
 
84
- return jsonify({'results': results})
85
-
86
- @app.route('/api/translate', methods=['GET'])
87
- def translate_text():
88
- query = request.args.get('q', '')
89
- to_lang = request.args.get('to', 'en')
90
-
91
  with WEBS() as webs:
92
- translation = webs.translate(query, to=to_lang)
93
-
94
- return jsonify({'translation': translation})
95
-
96
- @app.route('/api/suggestions', methods=['GET'])
97
- def search_suggestions():
98
- query = request.args.get('q', '')
99
- if not query:
100
- return jsonify({'error': 'Query parameter missing'})
101
 
 
 
 
 
102
  results = []
103
- try:
104
- with WEBS() as webs:
105
- for result in webs.suggestions(query):
106
- results.append(result)
107
- except Exception as e:
108
- return jsonify({'error': str(e)}), 500
109
-
110
- return jsonify({'results': results})
111
-
112
- @app.route('/api/health', methods=['GET'])
113
- def health_check():
114
- return jsonify({'status': 'working'})
115
 
116
- if __name__ == '__main__':
117
- app.run(debug=True)
 
 
1
+ from fastapi import FastAPI, Query, HTTPException
2
+ from pydantic import BaseModel
 
 
3
  from webscout import WEBS
4
 
5
+ app = FastAPI()
6
+
7
+ class SearchQuery(BaseModel):
8
+ q: str
9
+ max_results: int = 10
10
+ timelimit: Optional[int] = None
11
+ safesearch: str = 'moderate'
12
+ region: str = 'wt-wt'
13
+
14
+ class ImageSearchQuery(BaseModel):
15
+ q: str
16
+ max_results: int = 10
17
+ safesearch: str = 'moderate'
18
+ region: str = 'wt-wt'
19
+
20
+ class VideoSearchQuery(BaseModel):
21
+ q: str
22
+ max_results: int = 10
23
+ safesearch: str = 'moderate'
24
+ region: str = 'wt-wt'
25
+ timelimit: Optional[int] = None
26
+ resolution: Optional[str] = None
27
+ duration: Optional[str] = None
28
+
29
+ class NewsSearchQuery(BaseModel):
30
+ q: str
31
+ max_results: int = 10
32
+ safesearch: str = 'moderate'
33
+ region: str = 'wt-wt'
34
+ timelimit: Optional[int] = None
35
+
36
+ class MapSearchQuery(BaseModel):
37
+ q: str
38
+ place: Optional[str] = None
39
+ max_results: int = 10
40
+
41
+ class TranslateQuery(BaseModel):
42
+ q: str
43
+ to: str = 'en'
44
+
45
+ class SuggestionQuery(BaseModel):
46
+ q: str
47
+
48
+ @app.get("/api/search", response_model=List[dict])
49
+ async def search_text(query: SearchQuery):
50
  results = []
51
  with WEBS() as webs:
52
+ for result in webs.text(**query.dict()):
53
  results.append(result)
54
+ return results
55
 
56
+ @app.get("/api/images", response_model=List[dict])
57
+ async def search_images(query: ImageSearchQuery):
 
 
 
 
 
 
 
58
  results = []
59
  with WEBS() as webs:
60
+ for result in webs.images(**query.dict()):
61
  results.append(result)
62
+ return results
63
 
64
+ @app.get("/api/videos", response_model=List[dict])
65
+ async def search_videos(query: VideoSearchQuery):
 
 
 
 
 
 
 
 
 
 
66
  results = []
67
  with WEBS() as webs:
68
+ for result in webs.videos(**query.dict()):
69
  results.append(result)
70
+ return results
71
 
72
+ @app.get("/api/news", response_model=List[dict])
73
+ async def search_news(query: NewsSearchQuery):
 
 
 
 
 
 
 
 
74
  results = []
75
  with WEBS() as webs:
76
+ for result in webs.news(**query.dict()):
77
  results.append(result)
78
+ return results
79
 
80
+ @app.get("/api/maps", response_model=List[dict])
81
+ async def search_maps(query: MapSearchQuery):
 
 
 
 
 
 
82
  results = []
83
  with WEBS() as webs:
84
+ for result in webs.maps(**query.dict()):
85
  results.append(result)
86
+ return results
87
 
88
+ @app.get("/api/translate", response_model=dict)
89
+ async def translate_text(query: TranslateQuery):
 
 
 
 
 
90
  with WEBS() as webs:
91
+ translation = webs.translate(**query.dict())
92
+ return {'translation': translation}
 
 
 
 
 
 
 
93
 
94
+ @app.get("/api/suggestions", response_model=List[str])
95
+ async def search_suggestions(query: SuggestionQuery):
96
+ if not query.q:
97
+ raise HTTPException(status_code=400, detail="Query parameter missing")
98
  results = []
99
+ with WEBS() as webs:
100
+ for result in webs.suggestions(query.q):
101
+ results.append(result)
102
+ return results
 
 
 
 
 
 
 
 
103
 
104
+ @app.get("/api/health", response_model=dict)
105
+ async def health_check():
106
+ return {'status': 'working'}