diff --git a/rsshub/blueprints/main.py b/rsshub/blueprints/main.py index 2a93ec7..cdcc2f8 100644 --- a/rsshub/blueprints/main.py +++ b/rsshub/blueprints/main.py @@ -3,10 +3,12 @@ from rsshub.extensions import cache bp = Blueprint('main', __name__) +@bp.route('/word/') @bp.route('/') -def word(): +@cache.cached(timeout=3600) +def word(category=''): from rsshub.spiders.word.word import ctx - return render_template('main/word.html', **ctx()) + return render_template('main/word.html', **ctx(category)) @bp.route('/index') def index(): diff --git a/rsshub/spiders/word/Pipfile b/rsshub/spiders/word/Pipfile new file mode 100644 index 0000000..3026dd1 --- /dev/null +++ b/rsshub/spiders/word/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "2.7" diff --git a/rsshub/spiders/word/word.py b/rsshub/spiders/word/word.py index 24a1006..2d473b8 100644 --- a/rsshub/spiders/word/word.py +++ b/rsshub/spiders/word/word.py @@ -1,15 +1,34 @@ -import random +import csv +import random +import requests import linecache -import sys from os import path +import requests +from rsshub.utils import DEFAULT_HEADERS + file_path = path.dirname(path.realpath(__file__)) -def ctx(): - file = path.join(file_path,'toeflwords.txt') - with open(file, encoding='utf-8') as inf: - f = inf.readlines() - count = len(f) - wordnum = random.randrange(0, count, 1) - word = linecache.getline(file, wordnum) - return {"word": word} +def get_csv_line(url): + response = requests.get(url) + lines = response.text.splitlines() + reader = csv.reader(lines) + data = list(reader) + data = data[1:] + random_line = random.choice(data) + return random_line + +def ctx(category=''): + word = '' + if category == 'ja': + url = 'https://raw.githubusercontent.com/hillerliao/img/main/words.csv' + res = get_csv_line(url) + word = f"{res[1]} 〔{res[2]}〕 {res[3]} " + else: + file = path.join(file_path,'toeflwords.txt') + with open(file, encoding='utf-8') as inf: + f = inf.readlines() + count = len(f) + wordnum = random.randrange(0, count, 1) + word = linecache.getline(file, wordnum) + return {"word": word} \ No newline at end of file