Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Guest-Liang committed Oct 29, 2024
0 parents commit 9b59981
Show file tree
Hide file tree
Showing 46 changed files with 12,770 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Deploy to GitHub Pages

on:
push:
branches:
- main # 使用你的主分支名
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4 # 使用最新版本的 checkout

- name: Set up Node.js
uses: actions/setup-node@v4 # 使用最新版本的 setup-node
with:
node-version: 'lts/*' # 使用 Node.js 的最新 LTS 版本

- name: List directory for debugging
run: ls -a

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies with pnpm
run: cd pjallcate_frontend && pnpm install

- name: Build project with pnpm
run: cd pjallcate_frontend && pnpm run build-only

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4 # 使用最新版本的 actions-gh-pages
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./pjallcated_frontend/dist # 确保指向正确的构建输出目录
publish_branch: gh-pages
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# all node_modules
**/node_modules

# mysql infomation
config

# log
**/*.log

# raw html and excel file
html存档

# backend

# frontend
pjallcate_frontend/.vscode/settings.json
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## BBC6521 Project Allocation 2024/25: Project Allocation - Student View
1. 网址 http://18.218.128.52/live24.html ,预计开放时间为10.23 15:30到(10.31日)?
2. 前端和后端分别pnpm install, 前端pnpm dev,后端pnpm start
3. 哎呀我知道allocated没打对,但是改又得重新配一遍,拉倒吧

## MySQL相关
会把mysql做成json,到时候部署GitHub Pages
53 changes: 53 additions & 0 deletions excel2mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pandas as pd
import mysql.connector
from datetime import datetime
import yaml

with open('./config/mysqlinfo.yaml', 'r') as file:
config = yaml.safe_load(file)

try:
print('try connecting')
conn = mysql.connector.connection.MySQLConnection(
host=config['database']['host'],
user=config['database']['user'],
password=config['database']['password'],
database=config['database']['databasename'],
)
print('connected')
except mysql.connector.Error as err:
print(f"Errors: {err}")

cursor = conn.cursor()

date_part = '20241028'

df = pd.read_excel(f'{date_part}.xlsx', sheet_name=None)

# project_titles = {}
# for sheet_name, data in df.items():
# for _, row in data.iterrows():
# project_id = row['Project ID']
# project_title = row['Project Title']
# if project_id not in project_titles:
# # 避免重复插入相同项目
# project_titles[project_id] = project_title
# sql = config['database']['py_query']['insert_projects']
# print(f"[{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}] Inserting: {project_id}, {project_title}")
# cursor.execute(sql, (project_id, project_title))
# else:
# print(f"[{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}] Project {project_id} already exists")

for sheet_name, data in df.items():
time_part = sheet_name
datetime_value = datetime.strptime(date_part + time_part, '%Y%m%d%H%M%S')

for _, row in data.iterrows():
sql = config['database']['py_query']['insert_project_data']
allocated_to= row['Allocated to (BUPT no.)']
allocated_to = str(allocated_to).replace(".0",'') if not pd.isna(row['Allocated to (BUPT no.)']) else None
print(f"[{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}] Inserting: {datetime_value}, {row['Project ID']}, {allocated_to}")
cursor.execute(sql, (datetime_value, row['Project ID'], allocated_to))
conn.commit()
cursor.close()
conn.close()
64 changes: 64 additions & 0 deletions gethtml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import requests
import time
from datetime import datetime, timedelta

def save_webpage(url):
current_time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
filename = f"{current_time}.html"
max_retries = 5
retry_count = 0

while retry_count < max_retries:
try:
response = requests.get(url, timeout=10)
response.raise_for_status()

content = response.text

with open(filename, 'w', encoding='utf-8') as file:
file.write(content)

message = f"Saved {url} to {filename}"
print(message)
with open("log.log", "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}\n")
return

except Exception as e:
retry_count += 1
message = f"Retry {retry_count}/{max_retries} for {url} due to error: {e}"
print(message)
with open("log.log", "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}\n")

if retry_count == max_retries:
error_message = f"Failed to save {url} after {max_retries} retries: {e}"
print(error_message)
with open("log.log", "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {error_message}\n")
else:
time.sleep(10)

url = "http://18.218.128.52/live24.html"

# 每5分钟的10秒更新
while True:
now = datetime.now()
# 计算下一次5分钟周期的时间点(向上取整到最近的5分钟)
next_update = now + timedelta(minutes=(5 - now.minute % 5))
next_update = next_update.replace(second=10, microsecond=0)

# 如果当前时间已经超过了下一个更新时间点,则加5分钟
if next_update <= now:
next_update += timedelta(minutes=5)

next_update_message = f"Next update at: {next_update.strftime('%Y-%m-%d %H:%M:%S')}"
print(next_update_message)
with open("log.log", "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {next_update_message}\n")

# 需要等待的秒数
wait_time = (next_update - now).total_seconds()
time.sleep(wait_time)

save_webpage(url)
20 changes: 20 additions & 0 deletions html2excel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pandas as pd
import glob
import os

html_files = glob.glob('./html存档/linux/1028/*.html')

if html_files:
workbook_name = html_files[0][20:20+10].replace('-', '') + '.xlsx'
else:
raise FileNotFoundError("没有找到符合命名规则的HTML文件")

with pd.ExcelWriter(workbook_name) as writer:
for html_file in html_files:
tables = pd.read_html(html_file)
df = tables[0]
sheet_name = os.path.splitext(html_file)[0][11+20:].replace('-', '')
df.to_excel(writer, sheet_name=sheet_name, index=False)
print(f'sheet_name: {sheet_name} successfully written')

print(f"所有表格已成功写入 {workbook_name}")
109 changes: 109 additions & 0 deletions html2mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import requests
import time
from datetime import datetime, timedelta
import mysql.connector
import pandas as pd
import atexit
import yaml

with open('./config/mysqlinfo.yaml', 'r') as file:
config = yaml.safe_load(file)

LOGfile = f'./html存档/0000/log.log'
current_time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
filename = f'./html存档/0000/{current_time}.html'

def save_webpage(url):
global current_time, filename
current_time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
filename = f'./html存档/0000/{current_time}.html'
max_retries = 5
retry_count = 0

while retry_count < max_retries:
try:
response = requests.get(url, timeout=10)
response.raise_for_status()

content = response.text

with open(filename, 'w', encoding='utf-8') as file:
file.write(content)

message = f"Saved {url} to {filename}"
print(message)
with open(LOGfile, "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}\n")
return

except Exception as e:
retry_count += 1
message = f"Retry {retry_count}/{max_retries} for {url} due to error: {e}"
print(message)
with open(LOGfile, "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}\n")
if retry_count == max_retries:
error_message = f"Failed to save {url} after {max_retries} retries: {e}"
print(error_message)
with open(LOGfile, "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {error_message}\n")
else:
time.sleep(10)

url = "http://18.218.128.52/live24.html"

try:
print('try connecting')
conn = mysql.connector.connection.MySQLConnection(
host=config['database']['host'],
user=config['database']['user'],
password=config['database']['password'],
database=config['database']['databasename'],
)
print('connected')
except mysql.connector.Error as err:
print(f"Errors: {err}")
exit(1)

cursor = conn.cursor()

def close_db_connection():
cursor.close()
conn.close()
print("Database connection closed.")

atexit.register(close_db_connection)

while True:
now = datetime.now()
next_update = now + timedelta(minutes=(5 - now.minute % 5))
next_update = next_update.replace(second=10, microsecond=0)
if next_update <= now:
next_update += timedelta(minutes=5)

next_update_message = f"Next update at: {next_update.strftime('%Y-%m-%d %H:%M:%S')}"
print(next_update_message)
with open(LOGfile, "a", encoding="utf-8") as log_file:
log_file.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {next_update_message}\n")

wait_time = (next_update - now).total_seconds()
time.sleep(wait_time)

save_webpage(url)


tables = pd.read_html(filename)
df = tables[0]
data = df

datetime_value = filename[14:33].replace("-", "")

for _, row in data.iterrows():
allocated_to = row['Allocated to (BUPT no.)'] if not pd.isna(row['Allocated to (BUPT no.)']) else None
project_id = row['Project ID']

sql = config['database']['py_query']['insert_project_data']
print(f"[{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}] ({_}) INSERT INTO project_data {datetime_value, project_id, allocated_to}")
cursor.execute(sql, (datetime_value, project_id, allocated_to))

conn.commit()
19 changes: 19 additions & 0 deletions pjallcate_backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "pjallcate_backend",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.1",
"js-yaml": "^4.1.0",
"mysql2": "^3.11.3"
}
}
Loading

0 comments on commit 9b59981

Please sign in to comment.