gen_realtime.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. import requests
  12. access_token = "57e1d6642904ecc2348b2768b028b5dd74720f791c7a3fd7ec551db3fd12a303"
  13. hhhMBPath = '../hhh-home-mb'
  14. hhhPCPath = '../hhh-home-pc'
  15. #connstr = 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4'
  16. #2022/12/13 change
  17. connstr = 'mysql://hhh7796hhh:lYmWsu^ujcA1@ec2-3-35-26-49.ap-northeast-2.compute.amazonaws.com:3306/xoops?charset=utf8mb4'
  18. """ db = dataset.connect(
  19. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4') """
  20. # charset='utf8',
  21. # collation='utf8_unicode_ci'
  22. #2022/12/13 change
  23. connobj = {
  24. 'host': 'ec2-3-35-26-49.ap-northeast-2.compute.amazonaws.com',
  25. 'database': 'xoops',
  26. 'user': 'hhh7796hhh',
  27. 'password': 'lYmWsu^ujcA1',
  28. 'use_unicode': True
  29. #'charset': 'utf8',
  30. #'collation': 'utf8_unicode_ci'
  31. }
  32. #connobj = {
  33. # 'host': 'hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com',
  34. # 'database': 'xoops',
  35. # 'user': 'hhh7796hhh',
  36. # 'password': 'lYmWsu^ujcA1',
  37. # 'use_unicode': True
  38. # #'charset': 'utf8',
  39. # #'collation': 'utf8_unicode_ci'
  40. #}
  41. def ExecuteQuery(isql):
  42. with mysql.connector.connect(**connobj) as connection :
  43. with connection.cursor(dictionary=True) as cursor:
  44. # connection.set_charset_collation('utf8','utf8_general_ci')
  45. #cursor = connection.cursor(dictionary=True)
  46. cursor.execute(isql)
  47. return cursor.fetchall()
  48. def genjson_new(filename: str = "realtime.json"):
  49. jData = json.load(open('../json/'+filename, encoding='utf8'))
  50. 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 '首八大%' ")
  51. for x in jData:
  52. # 頂部輪播區-新刊頭
  53. if x['id'] == 0:
  54. records = ExecuteQuery("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp, adlogo_webp dwebp FROM _had
  55. WHERE adtype LIKE '新刊頭%'
  56. AND onoff='1'
  57. 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))
  58. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  59. x["data"] = []
  60. for c in records:
  61. a = {'imgUrl': c['mlo'], 'link': str(
  62. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp']), 'Dwebp': str(c['dwebp'])}
  63. x["data"].append(a)
  64. # print(x["data"])
  65. # 主要輪播區-首八大
  66. if x['id'] == 1:
  67. records = ExecuteQuery("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp, adlogo_webp dwebp,logo_icon,index_char_1,index_char_2_1,index_char_2_2,index_char_2_3 FROM _had
  68. WHERE adtype LIKE '首八大%'
  69. AND onoff='1'
  70. 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))
  71. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)""")
  72. x["data"] = []
  73. for c in records:
  74. if c['index_char_1']==None:
  75. c['index_char_1']=''
  76. if c['index_char_2_1']==None:
  77. c['index_char_2_1']=''
  78. if c['index_char_2_2']==None:
  79. c['index_char_2_2']=''
  80. if c['index_char_2_3']==None:
  81. c['index_char_2_3']=''
  82. a = {'imgUrl': c['mlo'], 'link': str(
  83. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp']), 'Dwebp': str(c['dwebp']),'logo_icon':str(c['logo_icon']),'index_char_1':str(c['index_char_1']),'index_char_2_1':str(c['index_char_2_1']),'index_char_2_2':str(c['index_char_2_2']),'index_char_2_3':str(c['index_char_2_3'])}
  84. x["data"].append(a)
  85. # print(x["data"])
  86. #tab區塊-最夯設計, 影音實錄, 專欄文章
  87. if x['id'] == 6:
  88. x["data"] = []
  89. #過年後修改
  90. a = {'tab': '編輯精選', 'data': []}
  91. records_for_custom = ExecuteQuery("""SELECT hcolumn_id, ctitle,clogo,cdesc,_hcolumn.ctag
  92. FROM homepage_set
  93. LEFT JOIN _hcolumn ON mapping_id = hcolumn_id
  94. WHERE outer_set=8
  95. AND homepage_set.onoff='Y'
  96. 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))
  97. ORDER BY inner_sort limit 4""")
  98. #加入不重複id
  99. id_use = []
  100. for c in records_for_custom:
  101. id_use.append(c['hcolumn_id'])
  102. id_len = 8 - len(id_use)
  103. # print(id_use)
  104. str_id_use=''
  105. for k in id_use:
  106. str_id_use+=' and hcolumn_id!=%s '%k
  107. # print(str_id_use)
  108. #加入不重複id sql
  109. # records = ExecuteQuery("""SELECT ctag,ctitle TT,clogo IMG, CONCAT('/columns/detail/',hcolumn_id,'/') LK, cdesc
  110. # from _hcolumn
  111. # WHERE onoff='1' """+str_id_use+"""
  112. # AND NOW() > sdate
  113. # ORDER BY hcolumn_id DESC
  114. # LIMIT 8""")
  115. records = ExecuteQuery("""SELECT ctag,ctitle TT,clogo IMG, CONCAT('/columns/detail/',hcolumn_id,'/') LK, cdesc
  116. from _hcolumn
  117. WHERE onoff='1' """ +str_id_use+"""
  118. AND NOW() > sdate
  119. ORDER BY sdate desc,hcolumn_id DESC
  120. LIMIT """+ str(id_len) +""" """)
  121. count_k=1
  122. for c in records:
  123. ctag_list=c['ctag'].split(',')
  124. if len(ctag_list)==1 and ctag_list[0]=='':
  125. ctag_list=[]
  126. ad = {'imgUrl': c['IMG'], 'link': c['LK'],
  127. 'title': c['TT'],'ctag':ctag_list}
  128. a['data'].append(ad)
  129. if count_k==4:
  130. for d in records_for_custom:
  131. ad = {'imgUrl': d['clogo'], 'link': "/columns/detail/" + str(
  132. d['hcolumn_id']) + "/", 'title': d['ctitle'], 'ctag':d['ctag'].split(',')}
  133. a["data"].append(ad)
  134. count_k+=1
  135. x["data"].append(a)
  136. a = {'tab': '最夯設計', 'data': []}
  137. records_for_custom = ExecuteQuery(""" SELECT hcase_id,caption TT,tag ,cover IMG, CONCAT('/cases/detail/',hcase_id,'/') LK,_hdesigner.hdesigner_id hid,img_path,title
  138. FROM homepage_set
  139. LEFT JOIN _hcase ON mapping_id = hcase_id
  140. left join _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  141. WHERE outer_set=2 and _hcase.onoff='1' AND _hdesigner.onoff='1'
  142. AND homepage_set.onoff='Y'
  143. 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))
  144. ORDER BY inner_sort limit 4""")
  145. #加入不重複id
  146. id_use = []
  147. for c in records_for_custom:
  148. id_use.append(c['hcase_id'])
  149. id_len = 8 - len(id_use)
  150. # print(id_use)
  151. str_id_use=''
  152. for k in id_use:
  153. str_id_use+=' and hcase_id!=%s '%k
  154. records = ExecuteQuery("""SELECT hcase_id,caption TT,tag ,cover IMG, CONCAT('/cases/detail/',hcase_id,'/') LK,_hdesigner.hdesigner_id hid,img_path,title
  155. from _hcase
  156. left join _hdesigner ON _hcase.hdesigner_id=_hdesigner.hdesigner_id
  157. WHERE
  158. _hcase.onoff='1' AND _hdesigner.onoff='1'
  159. AND (NOW() > sdate) """+str_id_use+"""
  160. ORDER BY sdate desc,hcase_id DESC
  161. LIMIT """+ str(id_len) +""" """)
  162. count_k=1
  163. for c in records:
  164. # print(c['hcase_id'])
  165. ctag_list="、".join(c['tag'].split(',')).split("、")
  166. if len(ctag_list)==1 and ctag_list[0]=='':
  167. ctag_list=[]
  168. ad = {'imgUrl': c['IMG'], 'link': c['LK'],'img_designer':c['img_path'],
  169. 'title': c['TT'],'ctag':ctag_list,'title_designer':c['title'],'link_designer': "/HHH_NEW/designers/index_designerList.php?cid=" + str(c['hid'])}
  170. a['data'].append(ad)
  171. if count_k==4:
  172. for d in records_for_custom:
  173. ctag_list="、".join(d['tag'].split(',')).split("、")
  174. if len(ctag_list)==1 and ctag_list[0]=='':
  175. ctag_list=[]
  176. ad = {'imgUrl': d['IMG'], 'link': d['LK'],'img_designer':d['img_path'],
  177. 'title': d['TT'],'ctag':ctag_list,'title_designer':d['title'],'link_designer': "/HHH_NEW/designers/index_designerList.php?cid=" + str(c['hid'])}
  178. a["data"].append(ad)
  179. count_k+=1
  180. x["data"].append(a)
  181. a = {'tab': '最新影音實錄', 'data': []}
  182. records_for_custom = ExecuteQuery(""" SELECT hvideo_id,title TT,iframe IMG , CONCAT('https://hhh.com.tw/video-post.php?id=',hvideo_id) LK , name
  183. FROM homepage_set
  184. LEFT JOIN _hvideo ON mapping_id = hvideo_id
  185. WHERE outer_set=3
  186. AND homepage_set.onoff='Y'
  187. 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))
  188. ORDER BY inner_sort limit 4""")
  189. #加入不重複id
  190. id_use = []
  191. for c in records_for_custom:
  192. id_use.append(c['hvideo_id'])
  193. id_len = 8 - len(id_use)
  194. # print(id_use)
  195. str_id_use=''
  196. for k in id_use:
  197. str_id_use+=' and hvideo_id!=%s '%k
  198. records = ExecuteQuery("""SELECT title TT,iframe IMG , CONCAT('https://hhh.com.tw/video-post.php?id=',hvideo_id) LK , name
  199. from _hvideo where display_datetime < NOW() """+str_id_use+"""
  200. ORDER BY hvideo_id DESC
  201. LIMIT """+ str(id_len) +""" """)
  202. count_k=1
  203. for c in records:
  204. tid = extract.video_id(c['IMG'])
  205. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  206. ad = {'imgUrl': timg, 'link': "https://www.youtube.com/watch?v="+str(tid),
  207. 'description': c['TT']}
  208. a['data'].append(ad)
  209. if count_k==4:
  210. for d in records_for_custom:
  211. tid = extract.video_id(d['IMG'])
  212. timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  213. ad = {'imgUrl': timg, 'link': "https://www.youtube.com/watch?v="+str(tid),
  214. 'description': d['TT']}
  215. a["data"].append(ad)
  216. count_k+=1
  217. x["data"].append(a)
  218. # print(x["data"])
  219. # 主題企劃區
  220. if x['id'] == 4:
  221. records = ExecuteQuery("""SELECT logo lo, CONCAT('/topic/detail/',htopic_id,'/') lk, `desc`, title FROM _htopic
  222. WHERE onoff = '1'
  223. ORDER BY htopic_id DESC limit 3""")
  224. x["data"] = []
  225. #手機板
  226. a = {'tab': 'phone', 'data': []}
  227. for c in records:
  228. phone = {'imgUrl': c['lo'], 'link': str(
  229. c['lk']), 'title': c['title']} #'video': 'false', 'description': c['desc'],
  230. a["data"].append(phone)
  231. x["data"].append(a)
  232. #電腦版
  233. records = ExecuteQuery("""SELECT theme_type, mapping_id, IFNULL(ifnull(ifnull(ifnull(_hcase.caption,_hcolumn.ctitle),_hproduct.name),_hvideo.title),_hbrand.title) caption ,
  234. IFNULL(ifnull(ifnull(_hcase.cover,_hcolumn.clogo),_hproduct.cover),_hbrand.logo) J,
  235. (case when theme_type='case' then CONCAT('/cases/detail/d/',mapping_id) when theme_type='column' then CONCAT('/columns/detail/',mapping_id) when theme_type='product' then CONCAT('/product-post.php?id=',mapping_id) when theme_type='video' then CONCAT('https://hhh.com.tw/video-post.php?id=',mapping_id) when theme_type='brand' then CONCAT('/brand-index.php?brand_id=',mapping_id) ELSE '' END) url
  236. FROM homepage_set left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  237. LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'
  238. LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'
  239. LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  240. LEFT JOIN _hbrand ON mapping_id = _hbrand.hbrand_id AND theme_type='brand'
  241. WHERE homepage_set.onoff='Y' and outer_set=1 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)) order by inner_sort asc limit 4""")
  242. a = {'tab': 'web', 'data': []}
  243. for c in records:
  244. phone = {'imgUrl': c['J'], 'link': str(
  245. c['url']), 'title': c['caption']} #'video': 'false', 'description': c['desc'],
  246. a["data"].append(phone)
  247. x["data"].append(a)
  248. # print(x["data"])
  249. # 編輯精選
  250. # if x['id'] == 4:
  251. # records = ExecuteQuery("""SELECT hcolumn_id, ctitle, clogo,cdesc
  252. # FROM homepage_set
  253. # LEFT JOIN _hcolumn ON mapping_id = hcolumn_id
  254. # WHERE outer_set=8
  255. # AND homepage_set.onoff='Y'
  256. # 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))
  257. # ORDER BY inner_sort""")
  258. # x["data"] = []
  259. # for c in records:
  260. # a = {'imgUrl': c['clogo'], 'link': "https://hhh.com.tw/columns/detail/" + str(
  261. # c['hcolumn_id']) + "/", 'title': c['ctitle'], 'video': 'false', 'description': c['cdesc']}
  262. # x["data"].append(a)
  263. # print(x["data"])
  264. # 首列表廣告
  265. if x['id'] == 3:
  266. records = ExecuteQuery("""SELECT adlogo lo,adlogo_mobile mlo, adhref lk, adlogo_mobile_webp lomwebp, adlogo_webp dwebp FROM _had
  267. WHERE adtype LIKE '首列表廣告%'
  268. AND onoff='1'
  269. 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))
  270. ORDER BY adtype""")
  271. x["data"] = []
  272. for c in records:
  273. a = {'imgUrl': c['mlo'], 'link': str(
  274. c['lk']), 'DimgUrl': c['lo'], 'webp': str(c['lomwebp']), 'Dwebp': str(c['dwebp'])}
  275. x["data"].append(a)
  276. # print(x["data"])
  277. # 來選好物區
  278. # if x['id'] == 6:
  279. # records = ExecuteQuery(
  280. # "SELECT max_row from outer_site_set WHERE title='來選好貨'")
  281. # maxrow = 1
  282. # for c in records:
  283. # maxrow = c['max_row']
  284. # records = ExecuteQuery("""(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
  285. # , (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
  286. # -- SELECT *
  287. # FROM homepage_set
  288. # left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  289. # LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  290. # LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  291. # LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  292. # WHERE homepage_set.onoff='Y'
  293. # AND outer_set = (SELECT oss_id from outer_site_set WHERE title='來選好貨')
  294. # 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))
  295. # ORDER BY outer_set, inner_sort)
  296. # UNION
  297. # (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) + """)
  298. # LIMIT """ + str(maxrow))
  299. # x["data"] = []
  300. # for c in records:
  301. # #print(c)
  302. # if c['iframe'] is None:
  303. # if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  304. # c['J'] = c['J'].decode('utf8')
  305. # if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  306. # c['caption'] = c['caption'].decode('utf8')
  307. # if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  308. # c['short_desc'] = c['short_desc'].decode('utf8')
  309. # a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  310. # 'description': c['short_desc'], 'video': 'false'}
  311. # else:
  312. # tid = extract.video_id(str(c['iframe']))
  313. # timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  314. # ccaption = ""
  315. # cdescription = ""
  316. # if isinstance(c['caption'], bytearray):
  317. # ccaption = str(c['caption'].decode('utf8'))
  318. # else:
  319. # ccaption = str(c['caption'])
  320. # if c['short_desc'] is not None:
  321. # if isinstance(c['short_desc'], bytes):
  322. # cdescription = str(c['short_desc'].decode('utf8'))
  323. # else:
  324. # cdescription = str(c['short_desc'])
  325. # a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  326. # 'description': cdescription, 'video': tid}
  327. # x["data"].append(a)
  328. # # print(x["data"])
  329. # # 本週推薦
  330. # if x['id'] == 7:
  331. # # records = ExecuteQuery(
  332. # # "SELECT max_row from outer_site_set WHERE title='本週推薦'")
  333. # maxrow = 8
  334. # # for c in records:
  335. # # maxrow = c['max_row']
  336. # records = ExecuteQuery("""SELECT theme_type, mapping_id, IFNULL(ifnull(ifnull(ifnull(_hcase.caption,_hcolumn.ctitle),_hproduct.name),_hvideo.title),_hbrand.title) caption ,
  337. # IFNULL(ifnull(ifnull(_hcase.cover,_hcolumn.clogo),_hproduct.cover),_hbrand.logo) J, iframe , IFNULL(ifnull(ifnull(ifnull(_hcase.short_desc,_hcolumn.cdesc),_hproduct.descr),_hvideo.`desc`),_hbrand.intro) short_desc
  338. # , (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) when theme_type='brand' then CONCAT('https://hhh.com.tw/brand-index.php?brand_id=',mapping_id) ELSE '' END) url
  339. # -- SELECT *
  340. # FROM homepage_set
  341. # left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  342. # LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  343. # LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  344. # LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  345. # LEFT JOIN _hbrand ON mapping_id = _hbrand.hbrand_id AND theme_type='brand'
  346. # WHERE homepage_set.onoff='Y'
  347. # AND outer_set = (SELECT oss_id from outer_site_set WHERE title='本週推薦')
  348. # 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))
  349. # ORDER BY outer_set, inner_sort
  350. # LIMIT """ + str(maxrow))
  351. # x["data"] = []
  352. # for c in records:
  353. # if c['iframe'] is None:
  354. # if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  355. # c['J'] = c['J'].decode('utf8')
  356. # if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  357. # c['caption'] = c['caption'].decode('utf8')
  358. # if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  359. # c['short_desc'] = c['short_desc'].decode('utf8')
  360. # a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  361. # 'description': c['short_desc'], 'video': 'false'}
  362. # else:
  363. # tid = extract.video_id(str(c['iframe']))
  364. # timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  365. # ccaption = ""
  366. # cdescription = ""
  367. # if isinstance(c['caption'], bytearray):
  368. # ccaption = str(c['caption'].decode('utf8'))
  369. # else:
  370. # ccaption = str(c['caption'])
  371. # if c['short_desc'] is not None:
  372. # if isinstance(c['short_desc'], bytes):
  373. # cdescription = str(c['short_desc'].decode('utf8'))
  374. # else:
  375. # cdescription = str(c['short_desc'])
  376. # a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  377. # 'description': cdescription, 'video': tid}
  378. # x["data"].append(a)
  379. # # print(x["data"])
  380. # # 粉絲推薦
  381. # if x['id'] == 8:
  382. # records = ExecuteQuery(
  383. # "SELECT max_row from outer_site_set WHERE title='粉絲推薦'")
  384. # maxrow = 1
  385. # for c in records:
  386. # maxrow = c['max_row']
  387. # records = ExecuteQuery("""SELECT theme_type, mapping_id, IFNULL(ifnull(ifnull(ifnull(_hcase.caption,_hcolumn.ctitle),_hproduct.name),_hvideo.title),_hbrand.title) caption ,
  388. # IFNULL(ifnull(ifnull(_hcase.cover,_hcolumn.clogo),_hproduct.cover),_hbrand.logo) J, iframe , IFNULL(ifnull(ifnull(ifnull(_hcase.short_desc,_hcolumn.cdesc),_hproduct.descr),_hvideo.`desc`),_hbrand.intro) short_desc
  389. # , (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) when theme_type='brand' then CONCAT('https://hhh.com.tw/brand-index.php?brand_id=',mapping_id) ELSE '' END) url
  390. # -- SELECT *
  391. # FROM homepage_set
  392. # left join _hcase ON _hcase.hcase_id=homepage_set.mapping_id AND theme_type='case'-- AND _hcase.onoff = '1'
  393. # LEFT JOIN _hproduct ON mapping_id = _hproduct.id AND theme_type='product'-- AND _hproduct.onoff = '1'
  394. # LEFT JOIN _hcolumn ON mapping_id = _hcolumn.hcolumn_id AND theme_type='column'-- AND _hcolumn.onoff = '1'
  395. # LEFT JOIN _hvideo ON mapping_id = _hvideo.hvideo_id AND theme_type='video'
  396. # LEFT JOIN _hbrand ON mapping_id = _hbrand.hbrand_id AND theme_type='brand'
  397. # WHERE homepage_set.onoff='Y'
  398. # AND outer_set = (SELECT oss_id from outer_site_set WHERE title='粉絲推薦')
  399. # 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))
  400. # ORDER BY outer_set, inner_sort
  401. # LIMIT """ + str(maxrow))
  402. # x["data"] = []
  403. # for c in records:
  404. # if c['iframe'] is None:
  405. # if isinstance(c['J'], bytearray) or isinstance(c['J'], bytes):
  406. # c['J'] = c['J'].decode('utf8')
  407. # if isinstance(c['caption'], bytearray) or isinstance(c['caption'], bytes):
  408. # c['caption'] = c['caption'].decode('utf8')
  409. # if isinstance(c['short_desc'], bytearray) or isinstance(c['short_desc'], bytes):
  410. # c['short_desc'] = c['short_desc'].decode('utf8')
  411. # a = {'imgUrl': c['J'], 'link': c['url'], 'title': c['caption'],
  412. # 'description': c['short_desc'], 'video': 'false'}
  413. # else:
  414. # tid = extract.video_id(str(c['iframe']))
  415. # timg = "https://img.youtube.com/vi/" + tid+"/hqdefault.jpg"
  416. # ccaption = ""
  417. # cdescription = ""
  418. # if isinstance(c['caption'], bytearray):
  419. # ccaption = str(c['caption'].decode('utf8'))
  420. # else:
  421. # ccaption = str(c['caption'])
  422. # if c['short_desc'] is not None:
  423. # if isinstance(c['short_desc'], bytes):
  424. # cdescription = str(c['short_desc'].decode('utf8'))
  425. # else:
  426. # cdescription = str(c['short_desc'])
  427. # a = {'imgUrl': timg, 'link': c['url'], 'title': ccaption,
  428. # 'description': cdescription, 'video': tid}
  429. # x["data"].append(a)
  430. # # 推薦設計師
  431. # if x['id'] == 5:
  432. # records = ExecuteQuery("""SELECT hdesigner_id,title,img_path,name FROM _hdesigner where onoff=1 order by dorder limit 12;
  433. # """)
  434. # x["data"] = []
  435. # for c in records:
  436. # a = {'imgUrl': c['img_path'],'name': c['title'] ,'designers_name':c['name'],'link': "/HHH_NEW/designers/index_designerList.php?cid=" + str(c['hdesigner_id'])}
  437. # x["data"].append(a)
  438. # 推薦設計師
  439. if x['id'] == 5:
  440. records_for_custom = ExecuteQuery(""" SELECT hd.hdesigner_id,hd.title,hd.img_path,hd.name
  441. FROM homepage_set
  442. LEFT JOIN _hdesigner hd ON mapping_id = hd.hdesigner_id
  443. WHERE outer_set=9 AND hd.onoff='1'
  444. AND homepage_set.onoff='Y'
  445. 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))
  446. ORDER BY inner_sort limit 6""")
  447. #加入不重複id
  448. id_use = []
  449. for g in records_for_custom:
  450. id_use.append(g['hdesigner_id'])
  451. id_len = 12 - len(id_use)
  452. print(id_use)
  453. str_id_use=''
  454. for k in id_use:
  455. str_id_use+=' and hdesigner_id!=%s '%k
  456. records = ExecuteQuery("""SELECT hdesigner_id,title,img_path,name FROM _hdesigner where onoff=1 """+str_id_use +""" order by dorder limit """+ str(id_len) +""" """)
  457. x["data"] = []
  458. count_k=1
  459. for c in records:
  460. a = {'imgUrl': c['img_path'],'name': c['title'] ,'designers_name':c['name'],'link': "/HHH_NEW/designers/index_designerList.php?cid=" + str(c['hdesigner_id'])}
  461. if count_k==7:
  462. for d in records_for_custom:
  463. q = {'imgUrl': d['img_path'],'name': d['title'] ,'designers_name':d['name'],'link': "/HHH_NEW/designers/index_designerList.php?cid=" + str(d['hdesigner_id'])}
  464. x["data"].append(q)
  465. count_k+=1
  466. x["data"].append(a)
  467. #=======================================================================================================================
  468. # 加好物API
  469. if x['id'] == 7:
  470. url = 'https://open.shopline.io/v1/products/search'
  471. params = {'per_page': 200, 'page': 1,"category_id":"6108deabe15ff9003e5cef08"}
  472. headers = {
  473. 'accept': 'application/json',
  474. 'authorization': 'Bearer '+access_token,
  475. 'User-Agent': 'Shopline test'
  476. }
  477. response = requests.get(url, params=params, headers=headers)
  478. use=response.json()
  479. x["data"] = []
  480. # for i in range(len(use['items'])):
  481. for i in range(len(use['items'])):
  482. if use['items'][i]['status']=='active':
  483. title = use['items'][i]['title_translations']['zh-hant']
  484. link = use['items'][i]['link']
  485. img = use['items'][i]['medias'][0]['images']['original']['url']
  486. a = {'imgUrl': img,'title': title ,'link':'https://shop.hhh.com.tw/products'+link}
  487. x["data"].append(a)
  488. #=======================================================================================================================
  489. # print(x["data"])
  490. # if x['id'] == 9:
  491. # records = ExecuteQuery(
  492. # "SELECT id, (case when youtube_title = '' OR youtube_title IS NULL then (SELECT title FROM _hvideo where display_datetime < NOW() 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 where display_datetime < NOW() ORDER BY hvideo_id DESC LIMIT 1) ELSE youtube_id end) Y FROM site_setup")
  493. # for c in records:
  494. # x['title'] = ""
  495. # if isinstance(c['T'], bytearray):
  496. # x['title'] = str(c['T'].decode('utf8'))
  497. # else:
  498. # x['title'] = str(c['T'])
  499. # x['yt'] = extract.video_id(str(c['Y']))
  500. # print(id)
  501. # if x['id'] == 10:
  502. # records = ExecuteQuery(
  503. # "SELECT all_search_tag ast FROM site_setup")
  504. # x["data"] = []
  505. # for c in records:
  506. # x["data"] = c['ast'].split(',')
  507. # print(id)
  508. # print(jData)
  509. """ if not os.path.exists(hhhMBPath):
  510. os.mkdir(hhhMBPath)
  511. with open(hhhMBPath+'/json/' + filename, 'w', encoding='utf-8') as f:
  512. json.dump(jData, f, ensure_ascii=False, indent=4)
  513. if not os.path.exists(hhhPCPath):
  514. os.mkdir(hhhPCPath)
  515. with open(hhhPCPath+'/json/' + filename, 'w', encoding='utf-8') as f:
  516. json.dump(jData, f, ensure_ascii=False, indent=4) """
  517. with open('../json/' + filename, 'w', encoding='utf-8') as f:
  518. json.dump(jData, f, ensure_ascii=False, indent=4)
  519. return jData
  520. genjson_new("data_index.json")
  521. 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://hhh.com.tw/hhh_index/json/data_index.json"]}'"""
  522. os.system(cmd)