test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. jData = json.load(open(hhhHomePath+'/json/data.json', encoding='utf8'))
  52. 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 '首八大%' ")
  53. for x in jData:
  54. # 頂部輪播區-新刊頭
  55. if x['id'] == 0:
  56. records = ExecuteQuery("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp FROM _had
  57. WHERE adtype LIKE '新刊頭%'
  58. AND onoff='1'
  59. 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))
  60. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  61. x["data"] = []
  62. for c in records:
  63. a = {'imgUrl': c['mlo'], 'link': str(
  64. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp'])}
  65. x["data"].append(a)
  66. # print(x["data"])
  67. # 主要輪播區-首八大
  68. if x['id'] == 1:
  69. records = ExecuteQuery("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp FROM _had
  70. WHERE adtype LIKE '首八大%'
  71. AND onoff='1'
  72. 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))
  73. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  74. x["data"] = []
  75. for c in records:
  76. a = {'imgUrl': c['mlo'], 'link': str(
  77. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp'])}
  78. x["data"].append(a)
  79. # print(x["data"])
  80. #tab區塊-最夯設計, 影音實錄, 專欄文章
  81. if x['id'] == 2:
  82. x["data"] = []
  83. records = ExecuteQuery("""SELECT caption TT ,cover IMG, CONCAT('https://hhh.com.tw/cases/detail/',hcase_id,'/') LK, short_desc txt
  84. from _hcase
  85. left join _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  86. WHERE
  87. _hcase.onoff='1' AND _hdesigner.onoff='1'
  88. AND(NOW() > sdate)
  89. ORDER BY hcase_id DESC
  90. LIMIT 3""")
  91. a = {'tab': '最夯設計', 'data': []}
  92. for c in records:
  93. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  94. 'title': c['TT'], 'description': c['txt']}
  95. a['data'].append(ad)
  96. x["data"].append(a)
  97. records = ExecuteQuery("""SELECT title TT,iframe IMG , CONCAT('https://hhh.com.tw/video-post.php?id=',hvideo_id) LK , name
  98. from _hvideo
  99. ORDER BY hvideo_id DESC
  100. LIMIT 3""")
  101. a = {'tab': '影音實錄', 'data': []}
  102. for c in records:
  103. tid = extract.video_id(c['IMG'])
  104. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  105. ad = {'imgUrl': timg, 'link': c['LK'],
  106. 'title': c['name'], 'description': c['TT']}
  107. a['data'].append(ad)
  108. x["data"].append(a)
  109. records = ExecuteQuery("""SELECT ctitle TT,clogo IMG, CONCAT('https://hhh.com.tw/column/detail/',hcolumn_id,'/') LK, cdesc
  110. from _hcolumn
  111. WHERE onoff='1'
  112. AND NOW() > sdate
  113. ORDER BY hcolumn_id DESC
  114. LIMIT 3""")
  115. a = {'tab': '專欄文章', 'data': []}
  116. for c in records:
  117. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  118. 'title': c['TT'], 'description': c['cdesc']}
  119. a['data'].append(ad)
  120. x["data"].append(a)
  121. # print(x["data"])
  122. # 主題企劃區
  123. if x['id'] == 3:
  124. records = ExecuteQuery("""SELECT logo lo, CONCAT('https://hhh.com.tw/topic/detail/',htopic_id,'/') lk, `desc`, title FROM _htopic
  125. WHERE onoff = '1'
  126. ORDER BY htopic_id DESC limit 3""")
  127. x["data"] = []
  128. for c in records:
  129. a = {'imgUrl': c['lo'], 'link': str(
  130. c['lk']), 'video': 'false', 'description': c['desc'], 'title': c['title']}
  131. x["data"].append(a)
  132. # print(x["data"])
  133. # 編輯精選
  134. if x['id'] == 4:
  135. records = ExecuteQuery("""SELECT hcolumn_id, ctitle, clogo,cdesc
  136. FROM homepage_set
  137. LEFT JOIN _hcolumn ON mapping_id = hcolumn_id
  138. WHERE outer_set=8
  139. AND homepage_set.onoff='Y'
  140. 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))
  141. ORDER BY inner_sort""")
  142. x["data"] = []
  143. for c in records:
  144. a = {'imgUrl': c['clogo'], 'link': "https://hhh.com.tw/columns/detail/" + str(
  145. c['hcolumn_id']) + "/", 'title': c['ctitle'], 'video': 'false', 'description': c['cdesc']}
  146. x["data"].append(a)
  147. # print(x["data"])
  148. # 來選好物區
  149. if x['id'] == 6:
  150. records = ExecuteQuery(
  151. "SELECT max_row from outer_site_set WHERE title='來選好貨'")
  152. maxrow = 1
  153. for c in records:
  154. maxrow = c['max_row']
  155. records = ExecuteQuery("""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
  156. , (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
  157. -- SELECT *
  158. FROM homepage_set
  159. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  160. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  161. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  162. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  163. WHERE homepage_set.onoff='Y'
  164. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='來選好貨')
  165. 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))
  166. ORDER BY outer_set, inner_sort
  167. LIMIT """ + str(maxrow))
  168. x["data"] = []
  169. for c in records:
  170. if c['iframe'] is None:
  171. if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  172. c['J'] = c['J'].decode('utf8')
  173. if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  174. c['caption'] = c['caption'].decode('utf8')
  175. if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  176. c['short_desc'] = c['short_desc'].decode('utf8')
  177. a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  178. 'description': c['short_desc'], 'video': 'false'}
  179. else:
  180. tid = extract.video_id(str(c['iframe']))
  181. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  182. ccaption = ""
  183. cdescription = ""
  184. if isinstance(c['caption'], bytearray):
  185. ccaption = str(c['caption'].decode('utf8'))
  186. else:
  187. ccaption = str(c['caption'])
  188. if c['short_desc'] is not None:
  189. if isinstance(c['short_desc'], bytes):
  190. cdescription = str(c['short_desc'].decode('utf8'))
  191. else:
  192. cdescription = str(c['short_desc'])
  193. a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  194. 'description': cdescription, 'video': tid}
  195. x["data"].append(a)
  196. # print(x["data"])
  197. # 本週推薦
  198. if x['id'] == 7:
  199. records = ExecuteQuery(
  200. "SELECT max_row from outer_site_set WHERE title='本週推薦'")
  201. maxrow = 1
  202. for c in records:
  203. maxrow = c['max_row']
  204. records = ExecuteQuery("""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
  205. , (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
  206. -- SELECT *
  207. FROM homepage_set
  208. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  209. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  210. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  211. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  212. WHERE homepage_set.onoff='Y'
  213. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='本週推薦')
  214. 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))
  215. ORDER BY outer_set, inner_sort
  216. LIMIT """ + str(maxrow))
  217. x["data"] = []
  218. for c in records:
  219. if c['iframe'] is None:
  220. if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  221. c['J'] = c['J'].decode('utf8')
  222. if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  223. c['caption'] = c['caption'].decode('utf8')
  224. if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  225. c['short_desc'] = c['short_desc'].decode('utf8')
  226. a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  227. 'description': c['short_desc'], 'video': 'false'}
  228. else:
  229. tid = extract.video_id(str(c['iframe']))
  230. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  231. ccaption = ""
  232. cdescription = ""
  233. if isinstance(c['caption'], bytearray):
  234. ccaption = str(c['caption'].decode('utf8'))
  235. else:
  236. ccaption = str(c['caption'])
  237. if c['short_desc'] is not None:
  238. if isinstance(c['short_desc'], bytes):
  239. cdescription = str(c['short_desc'].decode('utf8'))
  240. else:
  241. cdescription = str(c['short_desc'])
  242. a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  243. 'description': cdescription, 'video': tid}
  244. x["data"].append(a)
  245. # print(x["data"])
  246. # 粉絲推薦
  247. if x['id'] == 8:
  248. records = ExecuteQuery(
  249. "SELECT max_row from outer_site_set WHERE title='粉絲推薦'")
  250. maxrow = 1
  251. for c in records:
  252. maxrow = c['max_row']
  253. records = ExecuteQuery("""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
  254. , (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
  255. -- SELECT *
  256. FROM homepage_set
  257. left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  258. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  259. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  260. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  261. WHERE homepage_set.onoff='Y'
  262. AND outer_set = (SELECT oss_id from outer_site_set WHERE title='粉絲推薦')
  263. 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))
  264. ORDER BY outer_set, inner_sort
  265. LIMIT """ + str(maxrow))
  266. x["data"] = []
  267. for c in records:
  268. if c['iframe'] is None:
  269. if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  270. c['J'] = c['J'].decode('utf8')
  271. if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  272. c['caption'] = c['caption'].decode('utf8')
  273. if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  274. c['short_desc'] = c['short_desc'].decode('utf8')
  275. a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  276. 'description': c['short_desc'], 'video': 'false'}
  277. else:
  278. tid = extract.video_id(str(c['iframe']))
  279. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  280. ccaption = ""
  281. cdescription = ""
  282. if isinstance(c['caption'], bytearray):
  283. ccaption = str(c['caption'].decode('utf8'))
  284. else:
  285. ccaption = str(c['caption'])
  286. if c['short_desc'] is not None:
  287. if isinstance(c['short_desc'], bytes):
  288. cdescription = str(c['short_desc'].decode('utf8'))
  289. else:
  290. cdescription = str(c['short_desc'])
  291. a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  292. 'description': cdescription, 'video': tid}
  293. x["data"].append(a)
  294. # print(x["data"])
  295. if x['id'] == 9:
  296. records = ExecuteQuery(
  297. "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")
  298. for c in records:
  299. x['title'] = ""
  300. if isinstance(c['T'], bytearray):
  301. x['title'] = str(c['T'].decode('utf8'))
  302. else:
  303. x['title'] = str(c['T'])
  304. x['yt'] = extract.video_id(str(c['Y']))
  305. # print(id)
  306. if x['id'] == 10:
  307. records = ExecuteQuery(
  308. "SELECT all_search_tag ast FROM site_setup")
  309. x["data"] = []
  310. for c in records:
  311. x["data"] = c['ast'].split(',')
  312. # print(id)
  313. # print(jData)
  314. """ if not os.path.exists(hhhHomePath):
  315. os.mkdir(hhhHomePath)
  316. with open(hhhHomePath+'/json/' + filename, 'w', encoding='utf-8') as f:
  317. json.dump(jData, f, ensure_ascii=False, indent=4) """
  318. print(jData)
  319. """ if __name__ == "__main__":
  320. uvicorn.run(app, host="0.0.0.0", port=8000) """