Skip to content

Commit 2afb4f7

Browse files
author
RSSHub-Py
committed
添加 randomword 随机内容支持\n- 新增 randomword 路由,支持 sentence/question/paragraph 三种类型\n- 添加随机内容爬虫和模板\n- 更新 Python 版本至 3.12.3
1 parent 899e31e commit 2afb4f7

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ undetected-chromedriver = "==3.5.5"
3030
[dev-packages]
3131

3232
[requires]
33-
python_version = "3.10"
33+
python_version = "3.12.3"

Pipfile.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rsshub/blueprints/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,11 @@ def zhihu_question(qid):
392392
def xueqiu_user(user_id):
393393
from rsshub.spiders.xueqiu.user import ctx
394394
return render_template('main/atom.xml', **filter_content(ctx(user_id)))
395+
396+
397+
@bp.route('/randomword/<string:category>')
398+
@bp.route('/randomword')
399+
@cache.cached(timeout=3600) # 1小时缓存
400+
def randomword(category='sentence'):
401+
from rsshub.spiders.randomword.randomword import ctx
402+
return render_template('main/atom.xml', **filter_content(ctx(category)))
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
import datetime
4+
from rsshub.utils import DEFAULT_HEADERS
5+
6+
def get_random_content(url, content_type):
7+
"""从randomword.com获取随机内容"""
8+
try:
9+
response = requests.get(url, headers=DEFAULT_HEADERS)
10+
response.raise_for_status()
11+
12+
soup = BeautifulSoup(response.text, 'html.parser')
13+
14+
# 根据content_type查找不同的元素
15+
if content_type == 'paragraph':
16+
# 查找包含段落的div
17+
content_div = soup.find('div', {'id': 'random_word_definition'})
18+
if not content_div:
19+
# 备用方案:查找其他可能的段落元素
20+
content_div = soup.find('div', {'id': 'random_word'})
21+
else:
22+
# 其他类型使用默认的div查找
23+
content_div = soup.find('div', {'id': 'random_word'})
24+
25+
if content_div:
26+
content = content_div.get_text(strip=True)
27+
return content
28+
else:
29+
# 备用方案:查找其他可能的元素
30+
content_elements = soup.find_all('div', string=True)
31+
for element in content_elements:
32+
text = element.get_text(strip=True)
33+
if len(text) > 20: # 假设内容长度大于20个字符
34+
return text
35+
36+
return f"Failed to extract {content_type} from the page"
37+
38+
except Exception as e:
39+
return f"Error fetching {content_type}: {str(e)}"
40+
41+
def ctx(category=''):
42+
"""通用RSS内容生成函数"""
43+
# 根据类型确定URL和标题
44+
type_map = {
45+
'sentence': {
46+
'url': 'https://randomword.com/sentence',
47+
'title_prefix': 'Random Sentence',
48+
'feed_title': 'Random Sentence'
49+
},
50+
'question': {
51+
'url': 'https://randomword.com/question',
52+
'title_prefix': 'Random Question',
53+
'feed_title': 'Random Question'
54+
},
55+
'paragraph': {
56+
'url': 'https://randomword.com/paragraph',
57+
'title_prefix': 'Random Paragraph',
58+
'feed_title': 'Random Paragraph'
59+
}
60+
}
61+
62+
if category not in type_map:
63+
# 默认使用sentence类型
64+
category = 'sentence'
65+
66+
config = type_map[category]
67+
content = get_random_content(config['url'], category)
68+
69+
# 创建RSS项目
70+
item = {
71+
'title': content,
72+
'description': content,
73+
'link': config['url'],
74+
'pubDate': datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT'),
75+
'author': 'RandomWord.com'
76+
}
77+
78+
return {
79+
'title': config['feed_title'],
80+
'link': config['url'],
81+
'description': f'Random {category}s from RandomWord.com',
82+
'items': [item]
83+
}

rsshub/templates/main/feeds.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,4 +728,23 @@ <h6 class="text-muted">用户动态 <a href="https://github.com/hillerliao" targ
728728
<br>
729729
<!--item xueqiu end-->
730730

731+
<!--item randomword start-->
732+
<div class="card text-left">
733+
<div class="card-body">
734+
<h4 class="card-title">RandomWord</h4>
735+
<h6 class="text-muted">RandomWord<a href="https://github.com/hillerliao" target="_blank" class="badge badge-secondary">by hillerliao</a></h6>
736+
<p class="card-text">举例:<a href="https://pyrsshub.vercel.app/randomword/sentence" target="_blank">https://pyrsshub.vercel.app/randomword/sentence</a></p>
737+
<p class="card-text">路由:<code>/randomword/:category</code></p>
738+
<p class="card-text">参数:category [可选,类型,sentence | question | paragraph,默认为 sentence] </p>
739+
<p class="card-text">功能:获取随机句子、问题或段落内容,每小时更新一次</p>
740+
<ul>
741+
<li><code>sentence</code> - 随机句子</li>
742+
<li><code>question</code> - 随机问题</li>
743+
<li><code>paragraph</code> - 随机段落</li>
744+
</ul>
745+
</div>
746+
</div>
747+
<br>
748+
<!--item randomword end-->
749+
731750
{% endblock content %}

0 commit comments

Comments
 (0)