Last active
March 31, 2018 06:24
-
-
Save ImSingee/1e149ca6ca20fef62aba2459eb124068 to your computer and use it in GitHub Desktop.
万门大学课程目录生成(可读性 txt 与可导入至 Omni Focus 的 Task Paper 文件)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import json | |
import subprocess | |
import os | |
from urllib.parse import urlparse | |
import requests | |
def request(url): | |
try: | |
r = requests.get(url) | |
j = json.loads(r.text) | |
return j | |
except: | |
return False | |
class Course: | |
def __init__(self, name, children=None): | |
self.name = name | |
self.children = children | |
@property | |
def children(self): | |
return self._children | |
@children.setter | |
def children(self, value): | |
if value is None: | |
self._children = None | |
return | |
self._children = [Course(i['name'], i.get('children')) for i in value] | |
def print(self, n=0, sep='\t', p=0, *, begin='', end=''): | |
if self.children is None: | |
return '{}{}'.format(self.name, end) | |
else: | |
if n == 0: | |
des = '' | |
else: | |
des = '{}.'.format(p) | |
return '{}{}\n{}'.format( | |
begin if n == 0 else '', # begin | |
'{}{}'.format(self.name, end), | |
'\n'.join(['{}{}{} {}'.format( | |
sep * (n+1), # Tab | |
begin, # begin | |
'{}{}'.format(des, i), # 序号 = des.i | |
x.print(n + 1, sep, '{}{}'.format(des, i), begin=begin, end=end) # 下一层内容 | |
) for i,x in enumerate(self.children, 1)]) | |
) | |
def taskpaper(self, parallel=False): | |
begin = '- ' | |
end = ' @parallel({parallel}) @autodone(false)'.format( | |
parallel = 'true' if parallel else 'false' | |
) | |
return self.print(begin=begin, end=end) | |
def __repr__(self): | |
return self.print() | |
def setClipboardData(data): | |
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE) | |
p.stdin.write(data.encode()) | |
p.stdin.close() | |
p.communicate() | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print('请确保提供了参数') | |
exit() | |
url = sys.argv[1] | |
o = urlparse(url) | |
if o.scheme == '': | |
url = 'https://api.wanmen.org/4.0/content/courses/{}'.format(url) | |
elif o.netloc == 'www.wanmen.org': | |
url = 'https://api.wanmen.org/4.0/content{}'.format(o.path) | |
elif o.netloc != 'api.wanmen.org': | |
print('网址错误(支持 CourseID、api 网址与课程网址)') | |
exit() | |
print(url) | |
j = request(url) | |
if not j: | |
print('解析错误,请检查网络连接状态、网址等') | |
exit() | |
lectures = j['lectures'] | |
lectures = Course(j['name'], lectures) | |
name = lectures.name | |
print(lectures.name) | |
setClipboardData(lectures.taskpaper()) | |
print('TaskPaper 内容已经保存至剪贴板') | |
BASE_DIR = os.environ['HOME'] | |
path_txt = os.path.join(BASE_DIR, '{}.txt'.format(name)) | |
path_taskpaper = os.path.join(BASE_DIR, '{}.taskpaper'.format(name)) | |
with open(path_txt, 'w', encoding='utf-8') as f: | |
f.write(lectures.print()) | |
with open(path_taskpaper, 'w', encoding='utf-8') as f: | |
f.write(lectures.taskpaper()) | |
print('内容已经保存至 {}'.format(path_txt)) | |
print('TaskPaper 文件已经保存至 {}'.format(path_taskpaper)) | |
exit() | |
使用方式:
chmod +x wanmen.py
./wanmen.py 课程页面 url
有效的课程页面 URL 例如:https://www.wanmen.org/courses/59293a3ab45f51716ab6f2f7
也可直接选择后面的「59293a3ab45f51716ab6f2f7」作为参数传递
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
剪贴板功能仅在 Mac 下测试通过