-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (43 loc) · 1.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, request, jsonify
from bs4 import BeautifulSoup
from requests import get
from json import dump, dumps
from os import path
app = Flask(__name__)
# Directory to save the search results
dir_home = path.expanduser("~")
dir_file = path.join(dir_home, "Desktop", "search_results.json")
# Dictionary containing URLs to be searched
all_url = {
"github": "https://github.com/",
"career.habr": "https://career.habr.com/",
"tiktok": "https://www.tiktok.com/@",
"pikabu": "https://pikabu.ru/@",
"reddit": "https://reddit.com/user/",
"instagram": "https://instagram.com/",
}
# Endpoint to handle search requests
@app.route('/search')
def all_search():
username = request.args.get('username')
search_results = {}
# Iterate over all URLs and check if the username is present in the response
for url in all_url.values():
try:
response = get(url + username, timeout=5)
soup = BeautifulSoup(response.text, 'html.parser')
user_info = soup.find('a', class_='_31VWB3vSkv19o3I7RVE710')
user_info1 = soup.find('title')
except:
continue
if username in str(user_info) or username in str(user_info1):
if username not in search_results:
search_results[username] = []
search_results[username].append(f"{url}{username}")
# Save the search results to a JSON file
with open(dir_file, 'w') as f:
dump(search_results, f, indent=4)
# Return the search results as a JSON response
return dumps(search_results, indent=2)
if __name__ == '__main__':
app.run()