-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
193 lines (171 loc) · 7.41 KB
/
server.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
from flask import Flask, request
from data_export import sheet2json, gql2json, upload_data
from rss_generator import gql2rss
from scheduled_update import status_update
from podcast import mirrorvoice_filter
import sitemap
import os
import json
import pytz
import utils.query as query
from datetime import datetime, timedelta
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
from sitemap import generate_web_sitemaps, generate_sitemap_index, generate_news_sitemaps
app = Flask(__name__)
gql_endpoint = os.environ['GQL_ENDPOINT']
BUCKET = os.environ.get('BUCKET', None)
GQL_ENDPOINT = os.environ.get('GQL_ENDPOINT', None)
@app.route("/forum_data")
def merge_json_data():
final = {}
sheet_url = request.args.get('sheet_url')
sheet_name = request.args.get('sheet_name')
sheet_data = sheet2json(sheet_url, sheet_name)
final["metadata"] = sheet_data
gql_string = request.args.get('gql_string')
bucket = request.args.get('bucket')
dest_file = request.args.get('dest_file')
json_data = gql2json(GQL_ENDPOINT, gql_string)
if "allPosts" in json_data and isinstance(json_data["allPosts"], list):
final["relatedPost"] = []
for post in json_data["allPosts"]:
post["url"] = "https://www.mnews.tw/story/" + post["slug"]
final["relatedPost"].append(post)
upload_data(bucket, json.dumps(final, ensure_ascii=False).encode('utf8'), 'application/json', dest_file)
return "ok"
@app.route("/gql_to_json")
def generate_json_from_gql():
gql_string = request.args.get('gql_string')
bucket = request.args.get('bucket')
dest_file = request.args.get('dest_file')
alt_gql_endpoint = request.args.get('gql_endpoint')
if alt_gql_endpoint:
gql_endpoint = alt_gql_endpoint
else:
gql_endpoint = os.environ['GQL_ENDPOINT']
json_data = gql2json(gql_endpoint, gql_string)
upload_data(bucket, json.dumps(json_data, ensure_ascii=False).encode('utf8'), 'application/json', dest_file)
return "ok"
@app.route("/sheet_to_json")
def generate_json_from_sheet():
sheet_url = request.args.get('sheet_url')
sheet_name = request.args.get('sheet_name')
bucket = request.args.get('bucket')
dest_file = request.args.get('dest_file')
json_data = sheet2json(sheet_url, sheet_name)
upload_data(bucket, json.dumps(json_data, ensure_ascii=False).encode('utf8'), 'application/json', dest_file)
return "ok"
@app.route("/cron_update")
def scheduled_publish():
return_message = status_update()
return return_message
@app.route("/sitemap/generator", methods=['POST'])
def sitemap_generator():
'''
You should provide two arguments in payload json
(1) target_objects: eg.['show', 'topic', ...etc], you cand find them at CMS
(2) chunk_size[opt]: Upper limit for a single sitemap xml
'''
msg = request.get_json()
target_objects = msg.get('target_objects', None)
chunk_size = msg.get('chunk_size', 1000)
if target_objects==None:
return "query parameters error"
objects = [obj.strip() for obj in target_objects]
app = os.environ.get('PROJECT_NAME', 'mnews')
sitemap_news_days = os.environ.get('SITEMAP_NEWS_DAYS', 2)
timezone = pytz.timezone('Asia/Taipei')
### Generate sitemap for website
sitemap_files = []
folder = os.path.join('rss', 'sitemap')
for object_name in objects:
# post should be handled by other method
if object_name == 'post':
continue
gql_string = query.sitemap_object_mapping[object_name]
gql_result = query.gql_fetch(gql_endpoint=gql_endpoint, gql_string=gql_string)
xml_strings = generate_web_sitemaps(
rows = gql_result['items'],
app = app,
object_name = object_name,
chunk_size = chunk_size
)
for index, sitemap_xml in enumerate(xml_strings):
filename = f'sitemap_{object_name}{index+1}.xml'
upload_data(BUCKET, sitemap_xml, "Application/xml", os.path.join(folder, filename))
time = datetime.now(timezone)
lastmod = time.strftime("%Y-%m-%d")
sitemap_files.append({
'filename': os.path.join(folder, filename),
'lastmod': lastmod
})
if len(sitemap_files)>0:
sitemap_index_xml = generate_sitemap_index(sitemap_files)
upload_data(BUCKET, sitemap_index_xml, "Application/xml", os.path.join(folder, 'sitemap_index_web_others.xml'))
### Generate sitemap for google tab news
sitemap_files = []
object_name = 'post'
if object_name in objects:
alias_object_name = 'story'
time = datetime.now(timezone)
# use ISO 8601 time format
lastmod = time.isoformat()
previous_time = time - timedelta(hours=int(sitemap_news_days)*24)
publish_gt_time = previous_time.isoformat()
# lastmod = time.strftime("%Y-%m-%d")
# publish_gt_time = previous_time.strftime("%Y-%m-%d")
# In news sitemap, practically you can only parse the posts within 2 days
gql_string = ""
if app == 'mnews':
gql_string = query.get_allPosts_string(publish_gt_time)
else:
gql_string = query.get_Posts_string(publish_gt_time)
print(gql_string)
gql_result = query.gql_fetch(gql_endpoint=gql_endpoint, gql_string=gql_string)
xml_strings = generate_news_sitemaps(
rows = gql_result['items'],
app = app,
language = 'zh-tw',
chunk_size = chunk_size
)
for index, sitemap_xml in enumerate(xml_strings):
filename = f'sitemap_{alias_object_name}{index+1}.xml'
upload_data(BUCKET, sitemap_xml, "Application/xml", os.path.join(folder, filename))
sitemap_files.append({
'filename': os.path.join(folder, filename),
'lastmod': lastmod
})
if len(sitemap_files)>0:
sitemap_index_xml = generate_sitemap_index(sitemap_files)
if app == 'mnews':
upload_data(BUCKET, sitemap_index_xml, "Application/xml", os.path.join(folder, 'sitemap_index_newstab.xml'))
else:
upload_data(BUCKET, sitemap_index_xml, "Application/xml", os.path.join(folder, 'index.xml'))
return "ok"
@app.route("/k6_to_rss")
def generate_rss_from_k6():
gql_string = request.args.get('gql_string')
bucket = request.args.get('bucket')
schema_type = request.args.get('schema_type')
dest_file = request.args.get('dest_file')
relatedPost = request.args.get('relatedPost')
rm_ytbiframe = request.args.get('rm_ytbiframe')
relatedPost_number = request.args.get('relatedPost_number')
rss_data = gql2rss(gql_endpoint, gql_string, schema_type, relatedPost, rm_ytbiframe, relatedPost_number)
if rss_data:
upload_data(bucket, rss_data, 'application/xml', dest_file)
return "ok"
return "fail"
@app.route("/mirrormedia_podcast")
def get_podcasts_from_mirrorvoice():
feedurl = request.args.get('feed_url', default = 'https://feed.mirrorvoice.com.tw/rss/mnews.xml')
bucket = request.args.get('bucket')
dest_file = request.args.get('dest_file')
author_filter = []
podcast_json = mirrorvoice_filter(author_filter, feedurl)
json_data = json.dumps(podcast_json, ensure_ascii=False).encode('utf8')
upload_data(bucket, json_data, 'application/json', dest_file)
return "ok"
if __name__ == "__main__":
app.run()