-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (177 loc) · 6.33 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import requests
from bs4 import BeautifulSoup
from flask import Flask, json, request
import random
from dotenv import load_dotenv
import urllib.parse
import logging
import instaloader
from TikTokApi import TikTokApi
from api.ddg import search
from api.tiktok import getPost
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
MAX_POST = 10
L = instaloader.Instaloader()
api = TikTokApi.get_instance(use_selenium=True, use_test_endpoints=True)
app = Flask(__name__)
@app.route("/")
def summary():
page = requests.get("https://www.kompas.com/covid-19").text.encode("utf-8")
soup = BeautifulSoup(page, "html.parser")
# covid summary
summary = soup.find("div", {"class": "covid__summary"})
cells = summary.findAll("div")
confirm, up, _ = cells[1].text[13:].replace(",", "").split(" ")
confirm = int(confirm)
up = int(up[1:])
rawat = int(cells[2].text.split(" ")[0][7:].replace(",", ""))
meninggal = int(cells[3].text.split(" ")[0][9:].replace(",", ""))
sembuh = int(cells[4].text.split(" ")[0][6:].replace(",", ""))
summary_response = {
"up": up,
"confirm": confirm,
"death": meninggal,
"recovered": sembuh,
"hospitalized": rawat,
}
logger.info(f"get corona summary {up} {confirm} {meninggal} {sembuh} {rawat}")
response = app.response_class(
response=json.dumps(summary_response), status=200, mimetype="application/json"
)
return response
@app.route("/detail")
def detail():
page = requests.get("https://www.kompas.com/covid-19").text.encode("utf-8")
soup = BeautifulSoup(page, "html.parser")
# covid summary
cities = []
tables = soup.find("div", {"class": "covid__table"})
for table in tables.findAll("div", {"class": "covid__row"}):
city = table.find("div", {"class": "covid__prov"}).text
strongs = table.findAll("strong")
confirm = int(strongs[0].text)
death = int(strongs[1].text)
recovered = int(strongs[2].text)
city_detail = {
"city": city,
"confirm": confirm,
"death": death,
"recovered": recovered,
}
cities.append(city_detail)
logger.info(f"get corona detail")
response = app.response_class(
response=json.dumps(cities), status=200, mimetype="application/json"
)
return response
@app.route("/ig")
def ig():
try:
username = request.args.get("username")
profile = instaloader.Profile.from_username(L.context, username)
# print("get profile")
if profile.is_private:
response = app.response_class(status=400, mimetype="application/json")
return response
posts = profile.get_posts()
print("get post")
count = posts.count
if count == 0:
response = app.response_class(status=400, mimetype="application/json")
return response
count = min(MAX_POST, count)
count = random.randint(1, count)
i = 0
ret_json = []
for post in posts:
print(post)
i += 1
# print(f"get post - {i}")
if i == count:
caption = post.caption
typename = post.typename
if typename == "GraphSidecar":
for sidecard in post.get_sidecar_nodes():
is_video = sidecard.is_video
if is_video:
src = sidecard.video_url
else:
src = sidecard.display_url
print(src)
ret_json.append({"src": src, "video": is_video})
else:
is_video = post.is_video
if is_video:
src = post.video_url
else:
src = post.url
ret_json.append({"src": src, "video": is_video})
break
# logger.info(f"get ig post {username} {caption} {src}")
ig_response = {"caption": caption, "result": ret_json}
response = app.response_class(
response=json.dumps(ig_response), status=200, mimetype="application/json"
)
return response
except Exception:
response = app.response_class(status=404, mimetype="application/json")
return response
@app.route("/igp")
def igp():
try:
username = request.args.get("username")
profile = instaloader.Profile.from_username(L.context, username)
igp_response = {
"src": profile.profile_pic_url,
}
logger.info(f"get ig profile {username}")
response = app.response_class(
response=json.dumps(igp_response), status=200, mimetype="application/json"
)
return response
except Exception:
response = app.response_class(status=404, mimetype="application/json")
return response
@app.route("/ddg")
def ddg():
query = request.args.get("search")
query = urllib.parse.unquote(query)
index = request.args.get("index")
if not index is None:
try:
index = int(index)
except:
response = app.response_class(status=400, mimetype="application/json")
return response
title, src = search(keywords=query, index=index)
ddg_response = {
"title": title,
"src": src,
}
response = app.response_class(
response=json.dumps(ddg_response), status=200, mimetype="application/json"
)
return response
@app.route("/tiktok")
def tiktok():
try:
username = request.args.get("username")
caption, url = getPost(api, username)
if caption == None:
response = app.response_class(status=400, mimetype="application/json")
return response
tiktok_response = {"caption": caption, "url": url}
logger.info(f"get tiktok post username {username} caption {caption} url {url}")
response = app.response_class(
response=json.dumps(tiktok_response),
status=200,
mimetype="application/json",
)
return response
except Exception:
response = app.response_class(status=404, mimetype="application/json")
return response
if __name__ == "__main__":
logger.info("Starting application")
app.run(host="0.0.0.0")