faq_static.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/python3
  2. import os
  3. import codecs
  4. import dataset
  5. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  6. #lst=['5926','5878','5912','5939','5885','4300','1042','3559']
  7. #lst=['3559']
  8. #lst=['5926','5878','5912','5939','5885','4300','1042']
  9. #lst=['5926','5878','5912','5939','5885','4300']
  10. #lst=['5926']
  11. ins_cmd=''
  12. dir_prefix='/var/www/jared-faq/'
  13. for l in lst:
  14. curdir=dir_prefix+l+'/index.html'
  15. outdir=dir_prefix+'modify/'+l+"/"
  16. # outfile='/tmp/out.html'
  17. if not os.path.exists(outdir):
  18. os.mkdir(outdir)
  19. outfile=outdir+'index.html'
  20. json_content=None
  21. cursor=db.query('select json_ld from xoops._hcolumn where hcolumn_id="'+l+'"')
  22. for c in cursor:
  23. json_content=c['json_ld']
  24. break
  25. jsonld='<script type="application/ld+json">[___TEMPLATE___]</script>'
  26. # curdir=dir_prefix+l+'/new.html'
  27. fr=codecs.open(curdir,'r','utf-8')
  28. lines=fr.readlines()
  29. fr.close()
  30. idx=0
  31. alldata=''
  32. skipmode=False
  33. for line in lines:
  34. if '<script type="application/ld+json">' in line:
  35. skipmode=True
  36. continue
  37. if skipmode and '</script>' in line:
  38. skipmode=False
  39. continue
  40. if skipmode:
  41. print('skip')
  42. continue
  43. if '<meta name="twitter:image"' in line:
  44. alldata+=line
  45. newjson=jsonld.replace('[___TEMPLATE___]',json_content)
  46. print(newjson)
  47. alldata+=newjson+"\n"
  48. print(l)
  49. else:
  50. alldata+=line
  51. # print(line)
  52. fw=codecs.open(outfile,'w','utf-8')
  53. fw.write(alldata)
  54. fw.close()