|
20 | 20 | from . import __version__
|
21 | 21 | from .config import md5_checker
|
22 | 22 | from .crawler import crawl_once, find_next_check_time
|
23 |
| -from .models import Task, query_tasks, tasks |
| 23 | +from .models import Task, query_feeds, query_tasks, tasks |
24 | 24 | from .settings import (Config, get_host_freq_list, refresh_token, release_app,
|
25 | 25 | set_host_freq, setup_app)
|
26 | 26 | from .utils import format_size, gen_rss
|
27 | 27 |
|
28 |
| -description = f"Watchdogs to keep an eye on the world's change.\nRead more: [https://github.com/ClericPy/watchdogs](https://github.com/ClericPy/watchdogs)" |
| 28 | +description = "Watchdogs to keep an eye on the world's change.\nRead more: [https://github.com/ClericPy/watchdogs](https://github.com/ClericPy/watchdogs)" |
29 | 29 | app = FastAPI(title="Watchdogs", description=description, version=__version__)
|
30 | 30 | sub_app.openapi_prefix = '/uniparser'
|
31 | 31 | app.mount("/uniparser", sub_app)
|
@@ -113,8 +113,13 @@ async def index(request: Request, tag: str = ''):
|
113 | 113 | quoted_tag = quote_plus(tag)
|
114 | 114 | rss_sign = Config.get_sign('/rss', f'tag={quoted_tag}')[1]
|
115 | 115 | lite_sign = Config.get_sign('/lite', f'tag={quoted_tag}')[1]
|
| 116 | + feeds_sign = Config.get_sign('/feeds', f'tag={quoted_tag}')[1] |
| 117 | + rss_feeds_sign = Config.get_sign('/rss_feeds', f'tag={quoted_tag}')[1] |
116 | 118 | kwargs['rss_url'] = f'/rss?tag={quoted_tag}&sign={rss_sign}'
|
117 | 119 | kwargs['lite_url'] = f'/lite?tag={quoted_tag}&sign={lite_sign}'
|
| 120 | + kwargs['feeds_url'] = f'/feeds?tag={quoted_tag}&sign={feeds_sign}' |
| 121 | + kwargs[ |
| 122 | + 'rss_feeds_url'] = f'/rss_feeds?tag={quoted_tag}&sign={rss_feeds_sign}' |
118 | 123 | init_vars_json = dumps({
|
119 | 124 | 'custom_links': Config.custom_links,
|
120 | 125 | 'callback_workers': Config.callback_handler.workers,
|
@@ -303,7 +308,7 @@ async def crawler_rule(method: str,
|
303 | 308 | elif method == 'pop':
|
304 | 309 | _result = await Config.rule_db.pop_crawler_rule(rule)
|
305 | 310 | else:
|
306 |
| - raise ValueError(f'method only support add and pop') |
| 311 | + raise ValueError('method only support add and pop') |
307 | 312 | result = {'msg': 'ok', 'result': _result}
|
308 | 313 | except Exception as e:
|
309 | 314 | result = {'msg': repr(e)}
|
@@ -419,7 +424,7 @@ async def rss(request: Request,
|
419 | 424 | source_link = f'https://{host}'
|
420 | 425 | xml_data: dict = {
|
421 | 426 | 'channel': {
|
422 |
| - 'title': f'Watchdogs', |
| 427 | + 'title': 'Watchdogs', |
423 | 428 | 'description': f'Watchdog on web change, v{__version__}.',
|
424 | 429 | 'link': source_link,
|
425 | 430 | },
|
@@ -505,7 +510,93 @@ async def lite(request: Request,
|
505 | 510 | else:
|
506 | 511 | last_page_url = ''
|
507 | 512 | context['last_page_url'] = last_page_url
|
508 |
| - quoted_tag = quote_plus(tag) |
509 | 513 | rss_sign = Config.get_sign('/rss', f'tag={quoted_tag}')[1]
|
510 | 514 | context['rss_url'] = f'/rss?tag={quoted_tag}&sign={rss_sign}'
|
511 | 515 | return templates.TemplateResponse("lite.html", context=context)
|
| 516 | + |
| 517 | + |
| 518 | +@app.get("/feeds") |
| 519 | +async def feeds( |
| 520 | + request: Request, |
| 521 | + tag: str = '', |
| 522 | + # user: str = '', |
| 523 | + sign: str = '', |
| 524 | + page: int = 1, |
| 525 | + page_size: int = Config.default_page_size, |
| 526 | +): |
| 527 | + feeds, has_more = await query_feeds(tag=tag, page=page, page_size=page_size) |
| 528 | + now = datetime.now() |
| 529 | + _feeds = [] |
| 530 | + current_date = None |
| 531 | + today = datetime.today().strftime('%Y-%m-%d') |
| 532 | + for feed in feeds: |
| 533 | + date = feed['ts_create'].strftime('%Y-%m-%d') |
| 534 | + if date != current_date: |
| 535 | + current_date = date |
| 536 | + if date == today: |
| 537 | + date += ' [Today]' |
| 538 | + _feeds.append({'current_date': date}) |
| 539 | + feed['timeago'] = timeago((now - feed['ts_create']).total_seconds(), |
| 540 | + 1, |
| 541 | + 1, |
| 542 | + short_name=True) |
| 543 | + _feeds.append(feed) |
| 544 | + context = {'feeds': _feeds, 'request': request} |
| 545 | + context['version'] = __version__ |
| 546 | + quoted_tag = quote_plus(tag) |
| 547 | + if has_more: |
| 548 | + next_page = page + 1 |
| 549 | + sign = Config.get_sign('/feeds', |
| 550 | + f'tag={quoted_tag}&page={next_page}')[1] |
| 551 | + next_page_url = f'/feeds?tag={quoted_tag}&page={next_page}&sign={sign}' |
| 552 | + else: |
| 553 | + next_page_url = '' |
| 554 | + context['next_page_url'] = next_page_url |
| 555 | + if page > 1: |
| 556 | + last_page = page - 1 |
| 557 | + sign = Config.get_sign('/feeds', |
| 558 | + f'tag={quoted_tag}&page={last_page}')[1] |
| 559 | + last_page_url = f'/feeds?tag={quoted_tag}&page={last_page}&sign={sign}' |
| 560 | + else: |
| 561 | + last_page_url = '' |
| 562 | + context['last_page_url'] = last_page_url |
| 563 | + rss_sign = Config.get_sign('/rss_feeds', f'tag={quoted_tag}')[1] |
| 564 | + context['rss_url'] = f'/rss_feeds?tag={quoted_tag}&sign={rss_sign}' |
| 565 | + return templates.TemplateResponse("feeds.html", context=context) |
| 566 | + |
| 567 | + |
| 568 | +@app.get("/rss_feeds") |
| 569 | +async def rss_feeds(request: Request, |
| 570 | + tag: str = '', |
| 571 | + sign: str = '', |
| 572 | + host: str = Header('', alias='Host')): |
| 573 | + feeds, _ = await query_feeds(tag=tag) |
| 574 | + source_link = f'https://{host}' |
| 575 | + xml_data: dict = { |
| 576 | + 'channel': { |
| 577 | + 'title': 'Watchdogs Timeline', |
| 578 | + 'description': f'Watchdog on web change, v{__version__}.', |
| 579 | + 'link': source_link, |
| 580 | + }, |
| 581 | + 'items': [] |
| 582 | + } |
| 583 | + for feed in feeds: |
| 584 | + pubDate: str = feed['ts_create'].strftime( |
| 585 | + format='%a, %d %b %Y %H:%M:%S') |
| 586 | + link: str = feed['url'] |
| 587 | + description: str = feed['text'] |
| 588 | + title: str = f'{feed["name"]}#{description[:80]}' |
| 589 | + item: dict = { |
| 590 | + 'title': title, |
| 591 | + 'link': link, |
| 592 | + 'guid': str(feed['id']), |
| 593 | + 'description': description, |
| 594 | + 'pubDate': pubDate |
| 595 | + } |
| 596 | + xml_data['items'].append(item) |
| 597 | + xml: str = gen_rss(xml_data) |
| 598 | + response = Response( |
| 599 | + content=xml, |
| 600 | + media_type="application/xml", |
| 601 | + headers={'Content-Type': 'application/xml; charset="utf-8"'}) |
| 602 | + return response |
0 commit comments