get random ja word

This commit is contained in:
Hiller Liao
2023-07-16 23:10:53 +08:00
parent 5aaba55dbd
commit 4370bc36f0
3 changed files with 44 additions and 12 deletions

View File

@@ -3,10 +3,12 @@ from rsshub.extensions import cache
bp = Blueprint('main', __name__)
@bp.route('/word/<string:category>')
@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():

View File

@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
[dev-packages]
[requires]
python_version = "2.7"

View File

@@ -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}