过往更新:
1. 创建仪表盘(灵感来自 0303)
2. 增加了博客数量排名
本次更新:
1. 所有空白标题都会改为 click [here]()
下一次更新:
1. 系统分区(1,2班 和 3班)
2. 记录已经被删掉的 blog
3. 记录 blog 的标题修改
4. 评论

开源协议:apache license 2.0.

import requests as rq
from datetime import datetime

HFOJ = "http://www.hfoj.net"

StudentUid = [[], [], []]

class Blog:
	def __init__(self, title = "", url = ""):
		self.title = title
		self.url = url

class UserBlogData:
	def __init__(self, uid = 0, page = 0, tot = 0):
		self.uid = uid
		self.page = page
		self.tot = tot
		self.blog = []
	def __lt__(self, other):
		if self.tot != other.tot:
			return (self.tot > other.tot)
		else:
			return (self.uid < other.uid)

def str_gbk(st):
	return st.encode("utf-8").decode("utf-8").encode("gbk", "ignore").decode("gbk")

def Strstr(txt, BeginPat, EndPat):
	res = []
	n, m, p = len(txt), len(BeginPat), len(EndPat)
	pos = {'': -1}
	for i in range(0, m):
		pos[BeginPat[i]] = i
	i = 0
	while i <= n - m:
		j = 0
		while j < m and txt[i + j] == BeginPat[j]:
			j += 1
		if j == m:
			k = i + j
			while k < n:
				if txt[k: k + p] == EndPat:
					res.append(txt[i + j: k])
					break
				k += 1
		i += m - pos.get(txt[i + m], -1)
	return res

def GetHtml(url):
	resp = rq.get(url)
	html = ""
	k = 0
	while resp.status_code != 200 and k < 32:
		resp = rq.get(url)
		k += 1
	if k < 32:
		html = resp.text
	else:
		print (f"failed to connected to {url}")
	return html

def InitUid():
	for Class in range(0, 3):
		for i in range(3 + Class * 40, 43 + Class * 40):
			StudentUid[Class].append(i)
		if Class == 2:
			StudentUid[Class].append(1246)
			
ubds = []

if __name__ == "__main__":
	InitUid()
	filename = "25级奥班仪表盘23.txt"
	with open(filename, "w") as fio:
		CurrentTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		fio.write(f"last update: {CurrentTime}\n\n")
		for Class in range(1, 4):
			fio.write(f"## Class {Class}\n\n<details>\n<summary>展开</summary>\n\n")
			gdtot = 0
			count0 = 0
			for stid in range(1, len(StudentUid[Class - 1]) + 1):
				print ("now: {:02}{:02}".format(Class, stid))
				uid = StudentUid[Class - 1][stid - 1]
				fio.write(f"### [id: {stid}, uid: {uid}](/blog/{uid}), [](/user/{uid})\n\n")
				ubd = UserBlogData(uid);
				while ubd.page <= 100:
					html = str_gbk(GetHtml(f"{HFOJ}/blog/{uid}/?page={ubd.page + 1}"))
					res1 = Strstr(html, "data-emoji-enabled>", "</a>")
					res2 = Strstr(html, f'<a href="/blog/{uid}/', "#")
					n = len(res1)
					if n == 0:
						break
					for i in range(0, n):
						ubd.blog.append(Blog(res1[i], f'/blog/{uid}/' + res2[i]))
					ubd.page += 1
					ubd.tot += n
				gdtot += ubd.tot
				if ubd.tot == 0:
					count0 += 1
				ubds.append(ubd)
				fio.write(f"**Ze has sent {ubd.tot} blog(s), {ubd.page} page(s)**\n\n")
				fio.write("<details>\n<summary>展开</summary>\n\n")
				for i in range(0, ubd.tot):
					if all(ch.isspace() for ch in ubd.blog[i].title):
						fio.write(f"{i + 1: 3d}: click [here]({ubd.blog[i].url})\n\n")
					else:
						fio.write(f"{i + 1: 3d}: [{ubd.blog[i].title}]({ubd.blog[i].url})\n\n")
				fio.write("</details>\n\n---\n\n")
			fio.write(f"**Students in Class {Class} have sent {gdtot} blog(s).**\n\n")
			fio.write(f"**There is(are) {count0} student(s) without sending blogs in HFOJ.**\n\n")
			fio.write(f"**On average, each person has sent {gdtot / len(StudentUid[Class - 1]):.2f} blog(s).**\n\n")
			fio.write("</details>\n\n---\n\n")
		ubds.sort()
		fio.write("## Rank\n\n")
		fio.write("<details>\n<summary>展开</summary>\n\n")
		for i in range(0, len(ubds)):
			if i == 0:
				rk = 1
			elif ubds[i].tot != ubds[i-1].tot:
				rk = i + 1
			if rk <= 3:
				fio.write(f"=={rk}==: ({ubds[i].tot} blog(s)) [](/user/{ubds[i].uid})\n\n")
			else:
				fio.write(f"{rk}: ({ubds[i].tot} blog(s)) [](/user/{ubds[i].uid})\n\n")
		fio.write("</details>")