gen_json_alone.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import os
  2. from typing import Optional
  3. from dataset.util import ResultIter
  4. from datetime import tzinfo
  5. import datetime
  6. #import mysql.connector
  7. #from mysql.connector import Error
  8. import dataset
  9. import json
  10. from pytube import extract
  11. hhhMBPath = '../hhh-home-mb'
  12. hhhPCPath = '../hhh-home-pc'
  13. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  14. # charset='utf8',
  15. # collation='utf8_unicode_ci'
  16. #@app.get("/genjson")
  17. def genjson(filename: str = "realtime.json"):
  18. jData = json.load(open('../json/data.json', encoding='utf8'))
  19. records = db.query("SELECT * FROM _had where (now() between start_time and end_time or ( start_time is null and end_time is null) or ( start_time = '0000-00-00 00:00:00' and end_time = '0000-00-00 00:00:00')) and onoff='1' and adtype like '首八大%' ")
  20. for x in jData:
  21. # 頂部輪播區-新刊頭
  22. if x['id'] == 0:
  23. records = db.query("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp, adlogo_webp dwebp FROM _had
  24. WHERE adtype LIKE '新刊頭%'
  25. AND onoff='1'
  26. AND(NOW() BETWEEN start_time AND end_time OR(start_time='0000-00-00 00:00:00' and end_time='0000-00-00 00:00:00') or (start_time is null and end_time is NULL))
  27. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  28. x["data"] = []
  29. for c in records:
  30. a = {'imgUrl': c['mlo'], 'link': str(
  31. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp']), 'Dwebp': str(c['dwebp'])}
  32. x["data"].append(a)
  33. # print(x["data"])
  34. # 主要輪播區-首八大
  35. if x['id'] == 1:
  36. records = db.query("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp, adlogo_webp dwebp FROM _had
  37. WHERE adtype LIKE '首八大%'
  38. AND onoff='1'
  39. AND(NOW() BETWEEN start_time AND end_time OR(start_time='0000-00-00 00:00:00' and end_time='0000-00-00 00:00:00') or (start_time is null and end_time is NULL))
  40. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  41. x["data"] = []
  42. for c in records:
  43. a = {'imgUrl': c['mlo'], 'link': str(
  44. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp']), 'Dwebp': str(c['dwebp'])}
  45. x["data"].append(a)
  46. # print(x["data"])
  47. #tab區塊-最夯設計, 影音實錄, 專欄文章
  48. if x['id'] == 2:
  49. x["data"] = []
  50. records = db.query("""SELECT caption TT ,cover IMG, CONCAT('https://hhh.com.tw/cases/detail/',hcase_id,'/') LK, short_desc txt
  51. from _hcase
  52. left join _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  53. WHERE
  54. _hcase.onoff='1' AND _hdesigner.onoff='1'
  55. AND(NOW() > sdate)
  56. ORDER BY hcase_id DESC
  57. LIMIT 3""")
  58. a = {'tab': '最夯設計', 'data': []}
  59. for c in records:
  60. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  61. 'title': c['TT'], 'description': c['txt']}
  62. a['data'].append(ad)
  63. x["data"].append(a)
  64. records = db.query("""SELECT title TT,iframe IMG , CONCAT('https://hhh.com.tw/video-post.php?id=',hvideo_id) LK , name
  65. from _hvideo
  66. ORDER BY hvideo_id DESC
  67. LIMIT 4""")
  68. a = {'tab': '影音實錄', 'data': []}
  69. cnt = 0
  70. for c in records:
  71. if cnt == 0:
  72. cnt += 1
  73. continue
  74. tid = extract.video_id(c['IMG'])
  75. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  76. ad = {'imgUrl': timg, 'link': c['LK'],
  77. 'title': c['name'], 'description': c['TT']}
  78. a['data'].append(ad)
  79. x["data"].append(a)
  80. records = db.query("""SELECT ctitle TT,clogo IMG, CONCAT('https://hhh.com.tw/columns/detail/',hcolumn_id,'/') LK, cdesc
  81. from _hcolumn
  82. WHERE onoff='1'
  83. AND NOW() > sdate
  84. ORDER BY hcolumn_id DESC
  85. LIMIT 3""")
  86. a = {'tab': '專欄文章', 'data': []}
  87. for c in records:
  88. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  89. 'title': c['TT'], 'description': c['cdesc']}
  90. a['data'].append(ad)
  91. x["data"].append(a)
  92. # print(x["data"])
  93. # 主題企劃區
  94. if x['id'] == 3:
  95. records = db.query("""SELECT logo lo, CONCAT('https://hhh.com.tw/topic/detail/',htopic_id,'/') lk, `desc`, title FROM _htopic
  96. WHERE onoff = '1'
  97. ORDER BY htopic_id DESC limit 3""")
  98. x["data"] = []
  99. for c in records:
  100. a = {'imgUrl': c['lo'], 'link': str(
  101. c['lk']), 'video': 'false', 'description': c['desc'], 'title': c['title']}
  102. x["data"].append(a)
  103. # print(x["data"])
  104. # 編輯精選
  105. if x['id'] == 4:
  106. records = db.query("""SELECT hcolumn_id, ctitle, clogo,cdesc
  107. FROM homepage_set
  108. LEFT JOIN _hcolumn ON mapping_id = hcolumn_id
  109. WHERE outer_set=8
  110. AND homepage_set.onoff='Y'
  111. AND(NOW() BETWEEN homepage_set.start_time AND homepage_set.end_time OR(homepage_set.start_time='0000-00-00 00:00:00' and homepage_set.end_time='0000-00-00 00:00:00') or (homepage_set.start_time is null and homepage_set.end_time is NULL))
  112. ORDER BY inner_sort""")
  113. x["data"] = []
  114. for c in records:
  115. a = {'imgUrl': c['clogo'], 'link': "https://hhh.com.tw/columns/detail/" + str(
  116. c['hcolumn_id']) + "/", 'title': c['ctitle'], 'video': 'false', 'description': c['cdesc']}
  117. x["data"].append(a)
  118. # print(x["data"])
  119. # 來選好物區
  120. if x['id'] == 6:
  121. records = db.query(
  122. "SELECT max_row from outer_site_set WHERE title='來選好貨'")
  123. maxrow = 1
  124. for c in records:
  125. maxrow = c['max_row']
  126. records = db.query("""(SELECT theme_type, mapping_id, IFNULL(ifnull(ifnull(_hcase.caption,_hcolumn.ctitle),_hproduct.name),_hvideo.title) COLLATE utf8_general_ci caption , IFNULL(ifnull(_hcase.cover,_hcolumn.clogo),_hproduct.cover) COLLATE utf8_general_ci J, iframe , IFNULL(ifnull(ifnull(_hcase.short_desc,_hcolumn.cdesc),_hproduct.descr),_hvideo.`desc`) COLLATE utf8_general_ci short_desc
  127. , (case when theme_type='case' then CONCAT('https://hhh.com.tw/cases/detail/d/',mapping_id) when theme_type='column' then CONCAT('https://hhh.com.tw/columns/detail/',mapping_id) when theme_type='product' then CONCAT('https://hhh.com.tw/product-post.php?id=',mapping_id) when theme_type='video' then CONCAT('https://hhh.com.tw/video-post.php?id=',mapping_id) ELSE '' END) url
  128. -- SELECT *
  129. FROM homepage_set
  130. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  131. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  132. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  133. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  134. WHERE homepage_set.onoff='Y'
  135. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='來選好貨')
  136. AND(NOW() BETWEEN homepage_set.start_time AND homepage_set.end_time OR(homepage_set.start_time='0000-00-00 00:00:00' and homepage_set.end_time='0000-00-00 00:00:00') or (homepage_set.start_time is null and homepage_set.end_time is NULL))
  137. ORDER BY outer_set, inner_sort)
  138. UNION
  139. (SELECT 'product', id, `name`, cover, NULL ,descr ,CONCAT('https://hhh.com.tw/product-post.php?id=',id) FROM _hproduct WHERE onoff='1' ORDER BY id DESC LIMIT """ + str(maxrow) + """)
  140. LIMIT """ + str(maxrow))
  141. x["data"] = []
  142. for c in records:
  143. print(c)
  144. if c['iframe'] is None:
  145. if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  146. c['J'] = c['J'].decode('utf8')
  147. if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  148. c['caption'] = c['caption'].decode('utf8')
  149. if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  150. c['short_desc'] = c['short_desc'].decode('utf8')
  151. a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  152. 'description': c['short_desc'], 'video': 'false'}
  153. else:
  154. tid = extract.video_id(str(c['iframe']))
  155. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  156. ccaption = ""
  157. cdescription = ""
  158. if isinstance(c['caption'], bytearray):
  159. ccaption = str(c['caption'].decode('utf8'))
  160. else:
  161. ccaption = str(c['caption'])
  162. if c['short_desc'] is not None:
  163. if isinstance(c['short_desc'], bytes):
  164. cdescription = str(c['short_desc'].decode('utf8'))
  165. else:
  166. cdescription = str(c['short_desc'])
  167. a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  168. 'description': cdescription, 'video': tid}
  169. x["data"].append(a)
  170. # print(x["data"])
  171. # 本週推薦
  172. if x['id'] == 7:
  173. records = db.query(
  174. "SELECT max_row from outer_site_set WHERE title='本週推薦'")
  175. maxrow = 1
  176. for c in records:
  177. maxrow = c['max_row']
  178. records = db.query("""SELECT theme_type, mapping_id, IFNULL(ifnull(ifnull(_hcase.caption,_hcolumn.ctitle),_hproduct.name),_hvideo.title) caption , IFNULL(ifnull(_hcase.cover,_hcolumn.clogo),_hproduct.cover) J, iframe , IFNULL(ifnull(ifnull(_hcase.short_desc,_hcolumn.cdesc),_hproduct.descr),_hvideo.`desc`) short_desc
  179. , (case when theme_type='case' then CONCAT('https://hhh.com.tw/cases/detail/d/',mapping_id) when theme_type='column' then CONCAT('https://hhh.com.tw/columns/detail/',mapping_id) when theme_type='product' then CONCAT('https://hhh.com.tw/product-post.php?id=',mapping_id) when theme_type='video' then CONCAT('https://hhh.com.tw/video-post.php?id=',mapping_id) ELSE '' END) url
  180. -- SELECT *
  181. FROM homepage_set
  182. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  183. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  184. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  185. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  186. WHERE homepage_set.onoff='Y'
  187. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='本週推薦')
  188. AND(NOW() BETWEEN homepage_set.start_time AND homepage_set.end_time OR(homepage_set.start_time='0000-00-00 00:00:00' and homepage_set.end_time='0000-00-00 00:00:00') or (homepage_set.start_time is null and homepage_set.end_time is NULL))
  189. ORDER BY outer_set, inner_sort
  190. LIMIT """ + str(maxrow))
  191. x["data"] = []
  192. for c in records:
  193. if c['iframe'] is None:
  194. if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  195. c['J'] = c['J'].decode('utf8')
  196. if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  197. c['caption'] = c['caption'].decode('utf8')
  198. if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  199. c['short_desc'] = c['short_desc'].decode('utf8')
  200. a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  201. 'description': c['short_desc'], 'video': 'false'}
  202. else:
  203. tid = extract.video_id(str(c['iframe']))
  204. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  205. ccaption = ""
  206. cdescription = ""
  207. if isinstance(c['caption'], bytearray):
  208. ccaption = str(c['caption'].decode('utf8'))
  209. else:
  210. ccaption = str(c['caption'])
  211. if c['short_desc'] is not None:
  212. if isinstance(c['short_desc'], bytes):
  213. cdescription = str(c['short_desc'].decode('utf8'))
  214. else:
  215. cdescription = str(c['short_desc'])
  216. a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  217. 'description': cdescription, 'video': tid}
  218. x["data"].append(a)
  219. # print(x["data"])
  220. # 粉絲推薦
  221. if x['id'] == 8:
  222. records = db.query(
  223. "SELECT max_row from outer_site_set WHERE title='粉絲推薦'")
  224. maxrow = 1
  225. for c in records:
  226. maxrow = c['max_row']
  227. records = db.query("""SELECT theme_type, mapping_id, IFNULL(ifnull(ifnull(_hcase.caption,_hcolumn.ctitle),_hproduct.name),_hvideo.title) caption , IFNULL(ifnull(_hcase.cover,_hcolumn.clogo),_hproduct.cover) J, iframe , IFNULL(ifnull(ifnull(_hcase.short_desc,_hcolumn.cdesc),_hproduct.descr),_hvideo.`desc`) short_desc
  228. , (case when theme_type='case' then CONCAT('https://hhh.com.tw/cases/detail/d/',mapping_id) when theme_type='column' then CONCAT('https://hhh.com.tw/columns/detail/',mapping_id) when theme_type='product' then CONCAT('https://hhh.com.tw/product-post.php?id=',mapping_id) when theme_type='video' then CONCAT('https://hhh.com.tw/video-post.php?id=',mapping_id) ELSE '' END) url
  229. -- SELECT *
  230. FROM homepage_set
  231. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  232. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  233. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  234. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  235. WHERE homepage_set.onoff='Y'
  236. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='粉絲推薦')
  237. AND(NOW() BETWEEN homepage_set.start_time AND homepage_set.end_time OR(homepage_set.start_time='0000-00-00 00:00:00' and homepage_set.end_time='0000-00-00 00:00:00') or (homepage_set.start_time is null and homepage_set.end_time is NULL))
  238. ORDER BY outer_set, inner_sort
  239. LIMIT """ + str(maxrow))
  240. x["data"] = []
  241. for c in records:
  242. if c['iframe'] is None:
  243. if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  244. c['J'] = c['J'].decode('utf8')
  245. if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  246. c['caption'] = c['caption'].decode('utf8')
  247. if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  248. c['short_desc'] = c['short_desc'].decode('utf8')
  249. a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  250. 'description': c['short_desc'], 'video': 'false'}
  251. else:
  252. tid = extract.video_id(str(c['iframe']))
  253. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  254. ccaption = ""
  255. cdescription = ""
  256. if isinstance(c['caption'], bytearray):
  257. ccaption = str(c['caption'].decode('utf8'))
  258. else:
  259. ccaption = str(c['caption'])
  260. if c['short_desc'] is not None:
  261. if isinstance(c['short_desc'], bytes):
  262. cdescription = str(c['short_desc'].decode('utf8'))
  263. else:
  264. cdescription = str(c['short_desc'])
  265. a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  266. 'description': cdescription, 'video': tid}
  267. x["data"].append(a)
  268. # print(x["data"])
  269. if x['id'] == 9:
  270. records = db.query(
  271. "SELECT id, (case when youtube_title = '' OR youtube_title IS NULL then (SELECT title FROM _hvideo ORDER BY hvideo_id DESC LIMIT 1) ELSE youtube_title END) T, (case when youtube_id = '' OR youtube_id IS NULL then (SELECT iframe FROM _hvideo ORDER BY hvideo_id DESC LIMIT 1) ELSE youtube_id end) Y FROM site_setup")
  272. for c in records:
  273. x['title'] = ""
  274. if isinstance(c['T'], bytearray):
  275. x['title'] = str(c['T'].decode('utf8'))
  276. else:
  277. x['title'] = str(c['T'])
  278. x['yt'] = extract.video_id(str(c['Y']))
  279. # print(id)
  280. if x['id'] == 10:
  281. records = db.query(
  282. "SELECT all_search_tag ast FROM site_setup")
  283. x["data"] = []
  284. for c in records:
  285. x["data"] = c['ast'].split(',')
  286. # print(id)
  287. # print(jData)
  288. if not os.path.exists(hhhMBPath):
  289. os.mkdir(hhhMBPath)
  290. with open('../json/' + filename, 'w', encoding='utf-8') as f:
  291. json.dump(jData, f, ensure_ascii=False, indent=4)
  292. if not os.path.exists(hhhPCPath):
  293. os.mkdir(hhhPCPath)
  294. with open('../json/' + filename, 'w', encoding='utf-8') as f:
  295. json.dump(jData, f, ensure_ascii=False, indent=4)
  296. return jData
  297. genjson("realtime.json")
  298. cmd="""curl -X POST "https://api.cloudflare.com/client/v4/zones/8cb58022ad5743cfd9f088d5e3a261eb/purge_cache" -H "X-Auth-Email: web.dept.hhh@gmail.com" -H "X-Auth-Key:63e51d9a1638d66afcbfc3320aabec52304de" -H "Content-Type: application/json" --data '{"files":["https://m.hhh.com.tw/hhh-home-mb/json/realtime.json"]}'"""
  299. os.system(cmd)
  300. #""" if __name__ == "__main__":
  301. # uvicorn.run(app, host="0.0.0.0", port=8000) """