一个交作业的小工具 v2.0

本版将参数减少到一个:题目的 URL。之后自动获取文件名、标题、描述、难度等等,只需填写题目解释、解题思路与代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: UTF-8 -*-
import sys
import time
from pyquery import PyQuery as pq
tmp = '''---
title: {title}
tag: algorithm
date: {date}
---
## Description
**Difficulty: {difficulty}**
{contents}
## Solution
{% codeblock lang:text %}
{% endcodeblock %}
'''
url = sys.argv[1]
d = pq(url)
get_title = d('.question-title h3').text()
get_difficulty = d('#DescriptionContent .question-info li strong').eq(2).text()
get_filename = url.rstrip('?tab=Description').rstrip('/').split('/')[-1] + '.md'
get_contents = (d('.question-content')
.html(method='html')
.split('<div><p><a href="/subscribe/">Subscribe</a>')[0]
.split('<p><b>Credits:</b>')[0]
.strip())
tmp = tmp.format(
title=get_title,
difficulty=get_difficulty,
contents=get_contents,
date=time.strftime("%F %H:%M:%S", time.localtime()))
file = open(get_filename, 'w')
file.write(tmp)
file.close()
print('Show time:', get_filename)

下一版打算实现输入题号代替网址。

人生苦短,我用 Python。