genhhhjson.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. from typing import Optional
  2. from dataset.util import ResultIter
  3. from fastapi import FastAPI
  4. from fastapi.middleware.cors import CORSMiddleware
  5. from pytrends.request import TrendReq
  6. from datetime import tzinfo
  7. import datetime
  8. import mysql.connector
  9. from mysql.connector import Error
  10. import dataset
  11. import json
  12. import re
  13. from pytube import extract
  14. from opencc import OpenCC
  15. app = FastAPI()
  16. cc = OpenCC('s2t')
  17. origins = [
  18. "*"
  19. ]
  20. app.add_middleware(
  21. CORSMiddleware,
  22. allow_origins=origins,
  23. allow_credentials=True,
  24. allow_methods=["*"],
  25. allow_headers=["*"],
  26. )
  27. @app.get("/")
  28. def read_root():
  29. return {"Hello": "World"}
  30. @app.get("/test")
  31. def test():
  32. db = dataset.connect(
  33. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8', {'pool_recycle': 3600})
  34. jData = json.load(open('data.json', encoding='utf8'))
  35. aa = ""
  36. cc = db.connections
  37. cursor = db.query("select * from site_setup;")
  38. for c in cursor:
  39. aa = c['id']
  40. return {aa}
  41. @app.get("/movexoopstostage")
  42. def movexoopstostage(designerid: str = "0", caseid: str = "0"):
  43. db = dataset.connect(
  44. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8', {'pool_recycle': 3600})
  45. cursor = db.query(
  46. "replace INTO stage._hdesigner SELECT * FROM xoops._hdesigner WHERE hdesigner_id IN ('" + designerid+"');")
  47. cursor = db.query(
  48. "replace INTO stage._hcase SELECT * FROM xoops._hcase WHERE hcase_id IN ('"+caseid.replace(',', "','")+"');")
  49. cursor = db.query(
  50. "replace INTO stage._hcase_img SELECT * FROM xoops._hcase_img WHERE hcase_id IN ('"+caseid.replace(',', "','")+"');")
  51. return {"success"}
  52. @app.get("/genjson")
  53. def genjson(filename: str = "index.json"):
  54. jData = json.load(open('data.json', encoding='utf8'))
  55. db = dataset.connect(
  56. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8')
  57. db.begin()
  58. cursor = db.query(
  59. "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 '首八大%' ")
  60. for x in jData:
  61. # 頂部輪播區-新刊頭
  62. if x['id'] == 0:
  63. cursor = db.query("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp FROM _had
  64. WHERE adtype LIKE '新刊頭%'
  65. AND onoff='1'
  66. 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))
  67. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  68. x["data"] = []
  69. for c in cursor:
  70. a = {'imgUrl': c['mlo'], 'link': str(
  71. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp'])}
  72. x["data"].append(a)
  73. # print(x["data"])
  74. cursor.close()
  75. # 主要輪播區-首八大
  76. if x['id'] == 1:
  77. cursor = db.query("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp FROM _had
  78. WHERE adtype LIKE '首八大%'
  79. AND onoff='1'
  80. 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))
  81. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  82. x["data"] = []
  83. for c in cursor:
  84. a = {'imgUrl': c['mlo'], 'link': str(
  85. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp'])}
  86. x["data"].append(a)
  87. # print(x["data"])
  88. cursor.close()
  89. #tab區塊-最夯設計, 影音實錄, 專欄文章
  90. if x['id'] == 2:
  91. x["data"] = []
  92. cursor = db.query("""SELECT caption TT ,cover IMG, CONCAT('https://www.hhh.com.tw/cases/detail/',hcase_id,'/') LK
  93. from _hcase
  94. left join _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  95. WHERE
  96. _hcase.onoff='1' AND _hdesigner.onoff='1'
  97. AND(NOW() > sdate)
  98. ORDER BY hcase_id DESC
  99. LIMIT 3""")
  100. a = {'tab': '最夯設計', 'data': []}
  101. for c in cursor:
  102. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  103. 'description': c['TT']}
  104. a['data'].append(ad)
  105. x["data"].append(a)
  106. cursor.close()
  107. cursor = db.query("""SELECT title TT,iframe IMG , CONCAT('https://www.hhh.com.tw/video-post.php?id=',hvideo_id) LK
  108. from _hvideo
  109. ORDER BY hvideo_id DESC
  110. LIMIT 3""")
  111. a = {'tab': '影音實錄', 'data': []}
  112. for c in cursor:
  113. tid = extract.video_id(c['IMG'])
  114. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  115. ad = {'imgUrl': timg, 'link': c['LK'],
  116. 'description': c['TT']}
  117. a['data'].append(ad)
  118. x["data"].append(a)
  119. cursor.close()
  120. cursor = db.query("""SELECT ctitle TT,clogo IMG, CONCAT('https://www.hhh.com.tw/column/detail/',hcolumn_id,'/') LK
  121. from _hcolumn
  122. WHERE onoff='1'
  123. AND NOW() > sdate
  124. ORDER BY hcolumn_id DESC
  125. LIMIT 3""")
  126. a = {'tab': '專欄文章', 'data': []}
  127. for c in cursor:
  128. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  129. 'description': c['TT']}
  130. a['data'].append(ad)
  131. x["data"].append(a)
  132. cursor.close()
  133. # print(x["data"])
  134. # 主題企劃區
  135. if x['id'] == 3:
  136. cursor = db.query("""SELECT CONCAT('https://www.hhh.com.tw/',theme_type,'/',mapping_id,'/detail/') lk, ifnull(_hcase.caption,_hcolumn.ctitle) tt, ifnull(_hcase.cover,_hcolumn.clogo) lo
  137. FROM homepage_set
  138. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'
  139. left join _hcolumn ON _hcolumn.hcolumn_id=homepage_set.mapping_id AND theme_type='column'
  140. WHERE outer_set=1
  141. AND homepage_set.onoff='Y'
  142. 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))
  143. ORDER BY inner_sort""")
  144. x["data"] = []
  145. for c in cursor:
  146. a = {'imgUrl': c['lo'], 'link': str(c['lk'])}
  147. x["data"].append(a)
  148. cursor.close()
  149. # print(x["data"])
  150. # 編輯精選
  151. if x['id'] == 4:
  152. cursor = db.query("""SELECT hcolumn_id, ctitle, clogo
  153. FROM homepage_set
  154. LEFT JOIN _hcolumn ON mapping_id = hcolumn_id
  155. WHERE outer_set=8
  156. AND homepage_set.onoff='Y'
  157. 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))
  158. ORDER BY inner_sort""")
  159. x["data"] = []
  160. for c in cursor:
  161. a = {'imgUrl': c['clogo'], 'link': "https://www.hhh.com.tw/columns/detail/" + str(
  162. c['hcolumn_id']) + "/", 'description': c['ctitle']}
  163. x["data"].append(a)
  164. cursor.close()
  165. # print(x["data"])
  166. # 來選好物區
  167. if x['id'] == 6:
  168. db = dataset.connect(
  169. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8')
  170. cursor = db.query(
  171. "SELECT max_row from outer_site_set WHERE title='來選好貨'")
  172. maxrow = 1
  173. for c in cursor:
  174. maxrow = c['max_row']
  175. db = dataset.connect(
  176. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8')
  177. cursor = db.query("""SELECT id, cover, name
  178. FROM _hproduct
  179. where onoff ='1'
  180. ORDER BY id desc
  181. LIMIT """ + str(maxrow))
  182. x["data"] = []
  183. for fk in cursor:
  184. a = {'imgUrl': fk['cover'], 'link': "https://www.hhh.com.tw/product-post.php?id=" + str(
  185. fk['id']) + "/", 'description': fk['name']}
  186. x["data"].append(a)
  187. cursor.close()
  188. # print(x["data"])
  189. # 本週推薦
  190. if x['id'] == 7:
  191. cursor = db.query(
  192. "SELECT max_row from outer_site_set WHERE title='本週推薦'")
  193. maxrow = 1
  194. for c in cursor:
  195. maxrow = c['max_row']
  196. db = dataset.connect(
  197. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8')
  198. cursor = db.query("""SELECT homepage_set.inner_sort, homepage_set.outer_set, _hcase_img.name as J, _hcase_img.name, _hcase.caption, _hcase.hcase_id, _hcase.hdesigner_id, _hdesigner.name, theme_type
  199. FROM homepage_set
  200. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id
  201. LEFT JOIN _hcase_img ON _hcase.hcase_id=_hcase_img.hcase_id
  202. LEFT JOIN _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  203. WHERE homepage_set.onoff='Y'
  204. AND _hcase.onoff='1'
  205. AND is_cover='1'
  206. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='粉絲推薦')
  207. 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))
  208. ORDER BY inner_sort
  209. LIMIT """ + str(maxrow))
  210. x["data"] = []
  211. for fk in cursor:
  212. a = {'imgUrl': fk['J'], 'link': "https://www.hhh.com.tw/cases/detail/" + str(
  213. fk['hcase_id']) + "/", 'description': fk['caption'], "video": "false"}
  214. x["data"].append(a)
  215. cursor.close()
  216. # print(x["data"])
  217. # 粉絲推薦
  218. if x['id'] == 8:
  219. db = dataset.connect(
  220. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8')
  221. cursor = db.query(
  222. "SELECT max_row from outer_site_set WHERE title='粉絲推薦'")
  223. maxrow = 1
  224. for c in cursor:
  225. maxrow = c['max_row']
  226. db = dataset.connect(
  227. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8')
  228. cursor = db.query("""SELECT homepage_set.inner_sort, homepage_set.outer_set, _hcase_img.name as J, _hcase_img.name, _hcase.caption, _hcase.hcase_id, _hcase.hdesigner_id, _hdesigner.name, theme_type
  229. FROM homepage_set
  230. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id
  231. LEFT JOIN _hcase_img ON _hcase.hcase_id=_hcase_img.hcase_id
  232. LEFT JOIN _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  233. WHERE homepage_set.onoff='Y'
  234. AND _hcase.onoff='1'
  235. AND is_cover='1'
  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 inner_sort
  239. LIMIT """ + str(maxrow))
  240. x["data"] = []
  241. for fk in cursor:
  242. a = {'imgUrl': fk['J'], 'link': "https://www.hhh.com.tw/cases/detail/" + str(
  243. fk['hcase_id']) + "/", 'description': fk['caption'], "video": "false"}
  244. x["data"].append(a)
  245. cursor.close()
  246. # print(x["data"])
  247. if x['id'] == 9:
  248. cursor = db.query(
  249. "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")
  250. for c in cursor:
  251. x['title'] = c['T']
  252. x['yt'] = extract.video_id(c['Y'])
  253. # print(id)
  254. cursor.close()
  255. # print(jData)
  256. db.close()
  257. with open(filename, 'w', encoding='utf-8') as f:
  258. json.dump(jData, f, ensure_ascii=False, indent=4)
  259. return jData