genjson.py 13 KB

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