众所周知,sharelatex的免费版本是没有自动git同步的,但是有时候我们又需要相应的同步怎么办呢?当然是自己写一个脚本啦!
事先声明:本脚本仅在南方科技大学计算机协会托管的校园sharelatex实例下测试过,其他使用场景请自行测试。
事前准备,安装 python, pip 安装 gitpython bs4 zipfile。
准备一个已经和GitHub做好连接的仓库。
使用下列的python脚本
import requests
import bs4
import zipfile
from datetime import datetime
from git.repo import Repo
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
session = requests.session()
csrf_token = ''
def sync_project(email:str, password:str, ids: list,target_forder:list):
csrf_token_url = 'https://sharelatex.cra.ac.cn/login'
res = session.get(csrf_token_url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
csrf_token = soup.find('input', {'name': '_csrf'})['value']
print(csrf_token)
login_url = 'https://sharelatex.cra.ac.cn/login'
login_data = {
'_csrf': csrf_token,
'email': email,
'password': password
}
res = session.post(login_url, json=login_data)
res.raise_for_status()
res = session.get('https://sharelatex.cra.ac.cn/project')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
csrf_token = soup.find('input', {'name': '_csrf'})['value']
print(csrf_token)
print(requests.get('https://sharelatex.cra.ac.cn/system/messages').text)
for id, target_forder in zip(ids, target_forder):
# https://sharelatex.cra.ac.cn/project/65e19b18d3608735f8368768/download/zip
# or https://sharelatex.cra.ac.cn/project/download/zip?project_ids=65a22dffe0ed699a6bb57ee9,65e19b18d3608735f8368768
url = 'https://sharelatex.cra.ac.cn/project/{}/download/zip'.format(id)
res = session.get(url)
with open(id+'.zip', 'wb') as f:
f.write(res.content)
repo = Repo(target_forder)
repo.git.pull()
f = zipfile.ZipFile(id+'.zip','r')
f.extractall(target_forder)
repo.git.add('.')
repo.index.commit(str(datetime.now().strftime("%Y-%m-%d %H:%M")))
repo.remote("origin").push()
sync_project(YOUR_ACCOUNT_NAME,YOUR_PASSWORD,[PROJECT_ID],[TARGET_FORDER])
请注意,需要保证的是 PROJECT_ID 与 TARGET_FORDER要一一对应的关系,如果你想更进一步,比如启用GitHub Actions来自动编译一份的话,可以将下述文件添加到 .github/workflows/build.yaml
name: Build and Release LaTeX document
on:
push:
branches: [ master ]
tags:
- 'v*'
pull_request:
branches: [ master ]
workflow_dispatch:
jobs:
build_release_latex:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Compile LaTeX document
uses: xu-cheng/latex-action@v3
with:
root_file: main.tex
extra_fonts: |
./fonts/*
./fonts/times/*
latexmk_use_xelatex: true
- name: Stash PDF
run: |
mv main.pdf $HOME # cache the file
- name: Create Branch
uses: peterjgrainger/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: gh_actions_builds
- name: Checkout gh_actions_builds Branch
uses: actions/checkout@v4
with:
ref: gh_actions_builds
- name: Commit PDF
run: |
git config --local user.email YOUR_USER_EMAIL
git config --local user.name YOU_NAME
mv $HOME/main.pdf $(pwd) # bring it back
git add -f main.pdf
git commit -m "Updated by GitHub Action Automatically"
- name: Push PDF
uses: ad-m/[email protected]
with:
branch: gh_actions_builds
force: false
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Set Tag
if: github.event.inputs.tag == ''
run: |
echo "tag is empty"
echo "set tag to day"
echo "tag=$(date +%Y%m%d)" >> $GITHUB_ENV
- name: Release
uses: softprops/action-gh-release@v2
with:
files: main.pdf
tag_name: ${{ github.event.inputs.tag || env.tag }}
token: ${{ secrets.GITHUB_TOKEN }}
如果你不需要release的话,可以将最后一行删除,编译完成的 main.pdf 在 gh_actions_builds 分支下。