#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Oct 5 15:00:31 2022 @author: wangbingxun """ #docker exec python2.7 python /data/www/workshop2025.aiacademy.tw/__crontab__/cache_workshop2025.py delete_cache # import socket # print("\n######## ip is :",socket.gethostbyname(socket.gethostname())) # ip = socket.gethostbyname(socket.gethostname()) import sys reload(sys) sys.setdefaultencoding('utf-8') import requests import os import time import json # if os.path.isdir("/data/www/workshop202503.aiacademy.tw"): # base_path="/data/www/workshop202503.aiacademy.tw" # else: # base_path="/Users/wangbingxun/Desktop/cache" base_path="/data/www/workshop2025.aiacademy.tw" URL="https://workshop202503.aiacademy.tw" target_URI=["/venue/","/rules/","/signup/","/agenda/","/speakers/","/keynote-speakers/","/panel-discussion/","/moderator/"] # "/agenda/","/speakers/","/vip/" #Speaker URI get from server result = requests.get(URL + "/wp-content/themes/aiaconf20/_get_all_speaker_wjiocxvj2138fdcj.php") result.encoding="utf-8" speakers_list = result.content.strip().split(",") target_URI += speakers_list # The speaker list is not yet finalized. target_URI = ['/speakers/'] # Clear cache if len(sys.argv)>1: if sys.argv[1]=="delete_cache": import datetime today_YYYYMMDD="/"+datetime.datetime.now().strftime("%Y%m%d-%H%M%S") os.mkdir(base_path + today_YYYYMMDD) for URI in target_URI: folder_dir=base_path + URI if os.path.isdir(folder_dir): os.rename(folder_dir, base_path + today_YYYYMMDD + URI) sys.exit() elif sys.argv[1]=='agenda': target_URI=['/agenda'] # Only update the agenda else: print("argv invalid") sys.exit() if 1: for URI in target_URI: try: print("try: " + URI) result = requests.get( URL + URI) result.encoding="utf-8" print(URL + URI) folder_dir=base_path + URI # 檢查檔案是否存在 if os.path.isdir(folder_dir): pass else: os.mkdir(folder_dir) with open(folder_dir + "/.htaccess","w") as f: f.write( """RewriteEngine On\nRewriteCond %{HTTPS} !=on\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] Header set Content-Security-Policy "frame-src 'self' https://*.facebook.com https://*.google.com https://*.gstatic.com;" """) html_file="/index.html" with open(folder_dir + html_file,"w",) as f: f.write(result.text) print'{} sucess!\r'.format(URI) time.sleep(1) except Exception as e: print("can't get the content: {}".format(URI)) print(e) ### cahce for video page % homepage target_URI ={ '/?nc':'/home_cache.html', #'/conf2024-playback-video/?nc':'conf2024-playback-video.html', #'/playback-conf2024/?nc':'playback-conf2024.html', # '/playback-spring/?nc':'playback-spring.html', # '/aigc-summer-playback-video/?nc':'aigc-summer-playback-video.html', } for URI,html_file in target_URI.items(): try: result = requests.get( URL + URI) result.encoding="utf-8" #print(result.content) #folder_dir=base_path + URI folder_dir=base_path + "/v_cache/" # 檢查檔案是否存在 if os.path.isdir(folder_dir): pass else: os.mkdir(folder_dir) with open(folder_dir + "/.htaccess","w") as f: f.write( """Deny from All""") #html_file="/home_cache.html" with open(folder_dir + html_file,"w",) as f: f.write(result.text) print('{} sucess!\r'.format(URI)) time.sleep(1) except Exception as e: print("can't get the content: {}".format(URI)) print(e)