gothulearn

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 14 Imported by: 0

README

gothulearn

This is a Golang API wrapper aimed to provide a program-friendly interface of Learn website of Tsinghua University. It supports learn2018.

API documentation in progress

I made this to learn about the Go Programming Language

Key Words: Tsinghua University, API, Learn, 清华大学,网络学堂

Installation

To use this package, run go get -u github.com/AkaCoder404/gothulearn

Build From Source

Usage

Here is an example usage case for logging in and getting the courses of this semester.

package main

import (
	"fmt"
	"os"

	"github.com/AkaCoder404/gothulearn"
)

func main() {
	// 创造新的 API Client
	c := gothulearn.NewLearnClient()

	// 登录账号
	err := c.Auth.Login("username", "password")
	if err != nil {
		fmt.Println("Login failed")
	}
	fmt.Println("登入成功")

	// 登录,获取用户基本信息
	name, studentTypes, department, nil := c.User.GetUserInformation()
	if err != nil {
		fmt.Println("User information retrieval failed")
	}
	fmt.Printf("名字: %s\n用户类型: %s\n用户号: %s\n", name, studentTypes, department)

	// 获取本学期
	currentSemesterID, err := c.Class.GetCurrentAndNextSemester()
	currentSemesterID = "2022-2023-1"
	if err != nil {
		fmt.Println("Failed to get current semester")
	}
	fmt.Printf("本学期 %s", currentSemesterID)

	// 获取本学期课程
	list, err := c.Class.GetCourseList(currentSemesterID, "student")
	if err != nil {
		fmt.Println("Failed to get current semester class list")
	}
	for index := 0; index < len(list.ResultList); index++ {
		class := list.ResultList[index]
		fmt.Printf("%s %s\n", class.Kcm, class.Ywkcm)
	}

Change Log

Completed
  • V1.0.0

    • Initial Commit
    • Login
  • V1.0.1

    • Get current semester
    • Get list of classes based on semester
    • Get list of all classes
    • Get class information
    • Get unsubmitted/submitted/graded list of a class
    • Get notification list of a class
    • Get file list of a class
    • Get graded homework information *incomplete
  • V1.0.2

    • Get unsubmitted/submmited/graded homework information
    • Download file
  • V1.0.3 Licensing

Projects using this library

Documentation

Index

Constants

View Source
const (
	LearnPrefix     = "https://learn.tsinghua.edu.cn"
	RegistrarPrefix = "https://zhjw.cic.tsinghua.edu.cn"
)

Global Consts

Variables

View Source
var (
	CurrentSemesterID string
	NextSemesterID    string
	CourseType        string
)

Global variables

View Source
var (
	MAX_SIZE int = 20
)

Functions

func DecodeRequestBodyToString

func DecodeRequestBodyToString(response *http.Response) string

DecodeRequestBodyToString: Decode a byte response object into a string

func LearnAuthRom

func LearnAuthRom(ticket string) string

Types

type AuthService

type AuthService service

AuthService: Provides Auth services to Learn Account

func (*AuthService) Login

func (s *AuthService) Login(username string, password string) error

Login: Login to Learn

type Class

type Class struct {
	Xnxq      string `json:"xnxq"`      // 学年学期
	Wlkcid    string `json:"wlkcid"`    // 课程全ID
	Tpid      string `json:"tpid"`      //
	Kch       string `json:"kch"`       // 课程号
	Kxhnumber string `json:"kxhnumber"` // 课程号
	Kxh       string `json:"kxh"`       // 课序号
	Kcm       string `json:"kcm"`       // 课中文名
	Jsh       string `json:"jsh"`       // 教师号
	Kssj      string `json:"kssj"`      // 开始时间
	Jssj      string `json:"jssj"`      // 结束时间
	Kcnr      string `json:"kcnr"`      // 课程内容
	Xf        string `json:"xf"`        // 学分
	Xs        string `json:"xs"`        // 学时
	Cks       string `json:"cks"`       //
	Jxfs      string `json:"jxfs"`      // 教学方式
	Kclx      string `json:"kclx"`      // 课程类型
	Ksfs      string `json:"ksfs"`      // 考试方式
	Lls       int    `json:"lls"`       // 浏览数
	Dys       int    `json:"dys"`       // 答疑数
	Zys       int    `json:"zys"`       // 作业数
	Tjzys     int    `json:"tjzys"`     // 提交作业数
	Zls       int    `json:"zls"`       // 资料数
	Jslls     int    `json:"jslls"`     // 教师浏览数
	Xslls     int    `json:"xslls"`     // 学生浏览数
	Jsdys     int    `json:"jsdys"`     // 教师答疑数
	Xsdys     int    `json:"xsdys"`     // 学生答疑数
	Kfyhlx    string `json:"kfyhlx"`    // 开放用户类型
	Ddys      int    `json:"ddys"`      // 答疑数
	Tlts      int    `json:"ttls"`      // 讨论帖数
	Ggs       int    `json:"ggs"`       // 公告数
	Xtls      int    `json:"xtls"`      // 学堂历史数?
	Jxbjs     int    `json:"jxbjs"`     // 教学班级数?
	Jxkjs     int    `json:"jxkjs"`     // 教学课件数
	Kcfws     int    `json:"kcfws"`     // 课程范围数
	Wpgs      int    `json:"wpgs"`      // 问卷数?
	Xss       int    `json:"xss"`       // 学生数
	Xsbds     int    `json:"xsbds"`     // 学生报到数
	Xzxss     int    `json:"xzxss"`     // 学生在线数
	Tkxss     int    `json:"tkxss"`     // 退课学生数
	Bbh       string `json:"bbh"`       // 版本号
	Czr       string `json:"czr"`       // 创造人
	Czsj      string `json:"czsj"`      // 创造时间
	Bz        string `json:"bz"`        // 备注
	Studentid string `json:"studentid"` // 学生号
	Jsm       string `json:"jsm"`       // 教师名
	Zyzs      int    `json:"zyzs"`      // 作业总数
	Wjzys     int    `json:"wjzys"`     // 未交作业数
	Xggs      int    `json:"xggs"`      // 未读公告数
	Ggzs      int    `json:"ggzs"`      // 公告总数
	Xkjs      int    `json:"xkjs"`      // 未读课件数
	Xskjs     int    `json:"xskjs"`     // 课件总数
	Fqtls     int    `json:"fqtls"`     // 未读讨论帖数
	Cytls     int    `json:"cytls"`     // 已读讨论帖数
	Xsdyzs    int    `json:"xsdyzs"`    // 学生答疑总数
	Yhddys    int    `json:"yhddys"`    // 已回答答疑数
	Ywkcm     string `json:"ywkcm"`     // 英文课程名
}

Class: struct containing information on Class

type ClassAll

type ClassAll struct {
	Xnxq      string `json:"xnxq"`      // 学年学期
	Wlkcid    string `json:"wlkcid"`    // 课程ID
	Kch       string `json:"kch"`       // 课程号
	Kxh       string `json:"kxh"`       // 课序号
	Kcm       string `json:"kcm"`       // 课程名
	Jsh       string `json:"jsh"`       // 教授号
	StudentId string `json:"studentid"` // 学生号
	Jsm       string `json:"jsm"`       // 教授名
	Kcflm     string `json:"kcflm"`     // 课程分类名
	Ggs       int    `json:"ggs"`       // 公告数
	Jxbjs     int    `json:"jxbjs"`     //
	Jxkjs     int    `json:"jxkjs"`     //
	Zys       int    `json:"zys"`       // 作业数
	Kcdys     int    `json:"kcdys"`     // 课程答疑数
	Tlts      int    `json:"tlts"`      // 讨论帖数
	Lls       int    `json:"lls"`       // 浏览数
	Zyzs      int    `json:"zyzs"`      // 作业总数
	Fqtls     int    `json:"fqtls"`     // 发起讨论数
	Cytls     int    `json:"cytls"`     // 参与讨论数
	Xsdys     int    `json:"xsdys"`     // 学生答疑数
	Yxcs      int    `json:"yxcs"`      //
	Bjgcs     int    `json:"bjgcs"`     //
	Kjlls     int    `json:"Kjlls"`     //
	Gglls     int    `json:"gglls"`     // 公告浏览数
	Tls       int    `json:"tls"`       // 讨论数
	Jslx      string `json:"jslx"`      // 教授类型
	Ywkcm     string `json:"ywkcm"`     // 英文课程名
	Id        string `json:"id"`        // id
}

type ClassInfo

type ClassInfo struct {
	// Main Information
	Title                string // 课程 Title
	ClassName            string // 课程名称
	MainProfessor        string // 主讲教师
	HekaiProfessor       string // 合开教师
	ClassCredits         string // 课程学分
	ClassHours           string // 课程学时
	ClassScope           string // 课程开放范围
	ClassMaterialsCount  string // 课程文件数
	ClassHomeworkCount   string // 布置作业书
	ClassDiscussionCount string // 讨论贴数

	// Other Information
	ClassDescription               string // 课程简介
	ClassEnglishDescription        string // 英文课程简介
	ClassSchedule                  string // 进度安排
	ClassAssesmentMethod           string // 考核方式
	ClassReferenceMaterials        string // 教材及参考书
	ClassMainReference             string // 主教材
	ClassReferenceBooks            string // 参考书
	ClassProfessor                 string // 授课教师
	ClassSelectionGuidance         string // 选课指导
	ClassPrerequisites             string // 先修要求
	ClassOpenOfficeHour            string // Open Office Hour
	ClassGradingStandard           string // 成绩评定标准
	TeacherTeachingCharacteristics string // 教师教学特色
}

type ClassList

type ClassList struct {
	CurrentUser string  `json:"currentUser"`
	Message     string  `json:"message"`
	ResultList  []Class `json:"resultList"`
}

ClassList : A response for getting a list of classes

type ClassListAll

type ClassListAll struct {
	Result string      `json:"success"`
	Msg    string      `json:"msg"`
	Object ClassObject `json:"object"`
}

ClassListAll : struct just for getting all classes (different from ClassList)

type ClassObject

type ClassObject struct {
	ITotalRecords        string     `json:"iTotalRecords"`
	ITotalDisplayRecords string     `json:"iTotalDisplayRecords"`
	IDisplayStart        string     `json:"iDisplayStart"`
	IDisplayLength       string     `json:"iDisplayLength"`
	SEcho                string     `json:"sEcho"`
	SSearch              string     `json:"sSearch"`
	AaData               []ClassAll `json:"aaData"`
}

type ClassService

type ClassService service

func (*ClassService) GetAllClasses

func (c *ClassService) GetAllClasses() (*ClassListAll, error)

GetAllClasses : Get a list of all classes

func (*ClassService) GetClassInformation

func (s *ClassService) GetClassInformation(classID string, courseType string) (*ClassInfo, error)

GetClassInformation : Parse class information page (which is written in HTML) and return parts

func (*ClassService) GetCourseList

func (s *ClassService) GetCourseList(semesterID string, courseType string) (*ClassList, error)

func (*ClassService) GetCurrentAndNextSemester

func (s *ClassService) GetCurrentAndNextSemester() (string, error)

type File

type File struct {
	Id            string
	Filename      string // 文件名字
	Two           int
	Three         string
	Wlkcid        string // 文件ID
	Summary       string // 简要说明
	Six           string
	FileID        string // 文件ID
	Eight         int    //
	FileSize      int    // 文件大小Bytes
	PublishedDate string // 发布时间
}

TODO: Better names for two, three, six, eight

func (*File) UnmarshalJSON

func (f *File) UnmarshalJSON(data []byte) error

fileUnmarshaller :

type FileList

type FileList struct {
	Result string `json:"result"`
	Msg    string `json:"msg"`
	Object []File `json:"object"`
}

TODO: 文件 per Tabs

type FilePage

type FilePage struct {
	Wlkcid string `json:"wlkcid"`
	Kjflid string `json:"kjflid"` //
	Lb     string `json:"lb"`
	Bt     string `json:"bt"` // 标题
	Wz     int    `json:"wz"` // 序号
	Xzst   int    `json:"xzst"`
	Czr    string `json:"czr"`
	Czsj   int    `json:"czsj"`
	Bz     string `json:"bz"`
	Id     string `json:"id"`
}

type FilePageList

type FilePageList struct {
	Result string         `json:"result"`
	Msg    string         `json:"msg"`
	Object FilePageObject `json:"object"`
}

TODO: 课程文件 Tabs (FilePageList) FilePageList :

type FilePageObject

type FilePageObject struct {
	Page    int        `json:"page"`
	Total   int        `json:"total"`
	Records int        `json:"records"`
	Rows    []FilePage `json:"rows"`
}

type FileService

type FileService service

func (*FileService) DownloadFileFromFilePage

func (f *FileService) DownloadFileFromFilePage(fileName string, FileID string) (int64, error)

downloadFileFromFilePage :

func (*FileService) DownloadFileFromHomework

func (f *FileService) DownloadFileFromHomework(fileName string, courseID string, fileID string) (int64, error)

downloadFile :

func (*FileService) GetFileList

func (f *FileService) GetFileList(courseID string, filePageID string, classType string) (*FileList, error)

getFileList :

func (*FileService) GetFilePageList

func (f *FileService) GetFilePageList(courseID string, classType string) (*FilePageList, error)

getFilePageList :

type GradedHomeworkInfo

type GradedHomeworkInfo struct {
	// 作业内容及要求
	Title                string // 作业标题
	Description          string // 作业说明
	AttachmentName       string // 作业附件
	AttachmentLink       string // 作业附件链接
	AttachmentSize       string // 作业附件大小
	AnswerDesc           string // 答案说明
	AnswerAttachment     string // 大答附件
	AnswerAttachmentLink string // TODO: 答案附件链接
	AnswerAttachmentSize string // TODO: 答案附件大小
	PublishObject        string // 发布对象
	FinishType           string // 完成方式
	DueDate              string // 截止日期

	// 本人提交的作业
	Studentid                string // 提交日期
	SubmissionDate           string // 提交日期
	SubmissionContent        string // 上交作业内容
	SubmissionAttachmentName string // 上交作业附件名字
	SubmissionAttachmentLink string // 上交作业附件连接
	SubmissionAttachmentSize string // TODO

	// 老师批阅结果
	ReviewTeacher          string // 批阅老师
	ReviewTime             string // 批阅时间
	Grade                  string // 成绩
	Comments               string // 评语
	CommentsAttachmentName string // 评语附件
	CommentsAttachmentLink string // TODO
	CommentsAttachmentSize string // TODO
}

GradedHomeworkInfo : A struct for a graded homework instance

type Homework

type Homework struct {
	Kssj    int    `json:"kssj"`    // 开始时间
	KssjStr string `json:"kssjStr"` // 开始时间 String
	Jzsj    int    `json:"jzsj"`    // 截止时间
	JzsjStr string `json:"jzsjStr"` // 截止时间 String
	Bt      string `json:"bt"`      // 标题
	Wz      int    `json:"wz"`      // 序号
	Zywcfs  int    `json:"zywcfs"`
	Zytjfs  int    `json:"zytjfs"`
	Jffs    int    `json:"jffs"`
	Mxdxmc  string `json:"mxdxmc"`
	Mxdx    string `json:"mxdx"`
	Wlkcid  string `json:"wlkcid"`
	Xszyid  string `json:"xszyid"`
	Xh      string `json:"xh"`      // 学号
	Zyid    string `json:"zyid"`    // 作业ID
	Zynr    string `json:"zynr"`    // 作业内容
	ZynrStr string `json:"zynrStr"` // 作业内容 Str
	Zyfjid  string `json:"zyfjid"`  // 作业附件ID
	Scr     string `json:"scr"`     // 上传人
	Scsj    string `json:"scsj"`    // 上传时间
	ScsjStr string `json:"scsjStr"` // 上传时间 Str
	Ggzh    string `json:"gzzh"`
	Pysj    string `json:"pysj"`    // 批阅时间
	PysjStr string `json:"pysjStr"` // 批阅时间 Str
	Pynr    string `json:"pynr"`    // 批阅内容
	Pylj    string `json:"pylj"`    //
	Cj      string `json:"cj"`      // 成绩
	Zt      string `json:"zt"`      // 状态
	Pyzt    string `json:"pyzt"`    // 批阅状态
	Qzid    string `json:"qzid"`
	Bz      string `json:"bz"`
	Qzmc    string `json:"qzmc"`
	Xm      string `json:"xm"` // 姓名
	Dwmc    string `json:"dwmc"`
	Bm      string `json:"bm"` // 班名
	Cjgbsj  string `json:"cjgbsj"`
	Fjdxxz  string `json:"fjdxxz"`
	Djzcj   string `json:"djzcj"`
	Ywdjzcj string `json:"ywdjzcj"`
	Jsm     string `json:"jsm"`  // 教师名
	Wjmc    string `json:"wjmc"` // 文件名称
	Wjdx    string `json:"wjdx"` // 文件大小
	Id      string `json:"id"`   // ID
}

type HomeworkList

type HomeworkList struct {
	Result string         `json:"result"`
	Msg    string         `json:"msg"`
	Object HomeworkObject `json:"object"`
}

type HomeworkObject

type HomeworkObject struct {
	ITotalRecords        string     `json:"iTotalRecords"`
	ITotalDisplayRecords string     `json:"iTotalDisplayRecords"`
	IDisplayStart        string     `json:"iDisplayStart"`
	IDisplayLength       string     `json:"iDisplayLength"`
	SEcho                string     `json:"sEcho"`
	SSearch              string     `json:"sSearch"`
	AaData               []Homework `json:"aaData"`
}

type HomeworkService

type HomeworkService service

func (*HomeworkService) GetGradedHomeworkInfo

func (h *HomeworkService) GetGradedHomeworkInfo(courseID string, homeworkID string, xsHomeworkID string) (*GradedHomeworkInfo, error)

func (*HomeworkService) GetGradedHomeworks

func (h *HomeworkService) GetGradedHomeworks(courseID string) (*HomeworkList, error)

func (*HomeworkService) GetSubmittedHomeworkInfo

func (h *HomeworkService) GetSubmittedHomeworkInfo(courseID string, homeworkID string, xsHomeworkID string) (*SubmittedHomeworkInfo, error)

func (*HomeworkService) GetSubmmitedNotGradedHomeworks

func (h *HomeworkService) GetSubmmitedNotGradedHomeworks(courseID string) (*HomeworkList, error)

func (*HomeworkService) GetUnsubmittedHomeworkInfo

func (h *HomeworkService) GetUnsubmittedHomeworkInfo(courseID string, homeworkID string, xsHomeworkID string) (*UnsubmittedHomeworkInfo, error)

func (*HomeworkService) GetUnsubmittedHomeworks

func (h *HomeworkService) GetUnsubmittedHomeworks(courseID string) (*HomeworkList, error)

type LearnClient

type LearnClient struct {

	// Services for LearnClient API
	Auth     *AuthService
	User     *UserService
	Class    *ClassService
	File     *FileService
	Homework *HomeworkService
	Notice   *NoticeService
	// contains filtered or unexported fields
}

LearnClient: The GoLearn client

func NewLearnClient

func NewLearnClient() *LearnClient

NewLearnClient: New anonymous client. To login as authenticated user, use LearnClient.Login

func (*LearnClient) Request

func (c *LearnClient) Request(ctx context.Context, method, url string, body io.Reader) (*http.Response, error)

Request: Sends a request to Learn

type Notice

type Notice struct {
	Id       string `json:"id"`       // ID=公告ID+学号
	Ggid     string `json:"ggid"`     // 公告ID
	Bt       string `json:"bt"`       // 标题
	Xh       string `json:"xh"`       // 学号
	Wlkcid   string `json:"wlkcid"`   //
	Fbr      string `json:"fbr"`      // 发布人
	Fbrxm    string `json:"fbrxm"`    // 发布人姓名
	Fbsj     string `json:"fbsj"`     // 发布时间
	FbsjStr  string `json:"fbsjStr"`  // 发布时间 Str
	Ydsj     string `json:"ydsj"`     // 阅读时间
	Sfqd     string `json:"sfqd"`     // 是否确定?// FLAG
	Ggnr     string `json:"ggnr"`     // 公告内容
	GgnrStr  string `json:"ggnrStr"`  // 公告内容 Str
	GgnrMini string `json:"ggnrMini"` // 公告内容 Mini
	Fjmc     string `json:"fjmc"`     // 附件名称
	Sfyd     string `json:"sfyd"`     // 是否阅读
}

type NoticeList

type NoticeList struct {
	Result string       `json:"result"`
	Msg    string       `json:"msg"`
	Object NoticeObject `json:"object"`
}

type NoticeObject

type NoticeObject struct {
	ITotalRecords        string   `json:"iTotalRecords"`
	ITotalDisplayRecords string   `json:"iTotalDisplayRecords"`
	IDisplayStart        string   `json:"iDisplayStart"`
	IDisplayLength       string   `json:"iDisplayLength"`
	SEcho                string   `json:"sEcho"`
	SSearch              string   `json:"sSearch"`
	AaData               []Notice `json:"aaData"`
}

type NoticeService

type NoticeService service

func (*NoticeService) GetNoticeList

func (n *NoticeService) GetNoticeList(courseID string) (*NoticeList, error)

type Semester

type Semester struct {
	Xnxq   string `json:"xnxq"`   // 学年学期
	Xnxqmc string `json:"xnxqmc"` // 学年学期中文名
	Kssj   string `json:"kssj"`   // 开始时间
	Jssj   string `json:"jssj"`   // 结束时间
	Id     string `json:"id"`     // 学期号
}

type SemesterList

type SemesterList struct {
	Result     map[string]string // Current Semester
	Message    string
	ResultList []Semester `json:"resultList"` // Remaining Semesters
}

SemesterList : A response for getting the current semester

type SubmittedHomeworkInfo

type SubmittedHomeworkInfo struct {
	// 作业内容及要求
	Title          string // 作业标题
	Description    string // 作业说明
	AttachmentName string // 作业附件
	AttachmentLink string // 作业附件链接
	AttachmentSize string // TODO 作业附件大小

	AnswerDesc           string // 答案说明
	AnswerAttachmentName string // TODO 大答附件
	AnswerAttachmentLink string // TODO 答案附件链接
	AnswerAttachmentSize string // TODO 答案附件大小
	PublishObject        string // 发布对象
	FinishType           string // 完成方式

	// 本人提交的作业
	Studentid                string // 学号
	SubmissionDate           string // 提交日期
	SubmissionContent        string // 上交作业内容
	SubmissionAttachmentName string // 上交作业附件
	SubmissionAttachmentLink string // 上交作业附件连接
	SubmissionAttachmentSize string // 上交作业附件大小
}

SubmittedHomeworkInfo : A struct for a submitted homework, but not graded hw instance

type UnsubmittedHomeworkInfo

type UnsubmittedHomeworkInfo struct {
	// 作业内容及要求
	Title                string // 作业标题
	Description          string // 作业说明
	AttachmentName       string // 作业附件
	AttachmentLink       string // 作业附件链接
	AttachmentSize       string // 作业附件大小
	AnswerDesc           string // 答案说明
	AnswerAttachmentName string // TODO 大答附件
	AnswerAttachmentLink string // TODO 答案附件链接
	AnswerAttachmentSize string // TODO 答案附件大小
	PublishObject        string // 发布对象
	FinishType           string // 完成方式
	DueDate              string // 截止日期
}

UnsubmittedHomeworkInfo : A struct for a unsubmmited homework instance

type UserService

type UserService service

UserService: Provides User information service of Learn Account

func (*UserService) GetUserInformation

func (s *UserService) GetUserInformation() (string, string, string, error)

GetUserInformation: Get User Information after logging in, such as name, department, type (student/teacher)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL