| Conditions | 12 |
| Total Lines | 48 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like api_rssbot.RssRobot.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- encoding: utf-8 -*- |
||
| 17 | def main(self): |
||
| 18 | self.remove_old_history() |
||
| 19 | |||
| 20 | rss_list = Rss.select() |
||
| 21 | msg = "" |
||
| 22 | post_url_list = [ |
||
| 23 | rss_history.url |
||
| 24 | for rss_history in History.select().where( |
||
| 25 | History.publish_at == datetime.today().strftime("%Y-%m-%d") |
||
| 26 | ) |
||
| 27 | ] |
||
| 28 | |||
| 29 | no = 0 |
||
| 30 | for rss in rss_list: |
||
| 31 | rss_history_list = [] |
||
| 32 | feed = feedparser.parse(rss.feed) |
||
| 33 | title = True |
||
| 34 | c_no = 1 |
||
| 35 | for entry in feed.entries: |
||
| 36 | pub_t = datetime.fromtimestamp(mktime(entry["published_parsed"])) |
||
| 37 | |||
| 38 | # 此网站单独处理 |
||
| 39 | if rss.url == "https://www.foreverblog.cn": |
||
| 40 | pub_t = pub_t.replace(year=datetime.utcnow().year) + timedelta(hours=8) |
||
| 41 | elif rss.url == "https://www.zhihu.com": |
||
| 42 | entry.link = entry.link.split("/answer")[0] |
||
| 43 | |||
| 44 | if ( |
||
| 45 | entry.link not in post_url_list |
||
| 46 | and (datetime.timestamp(datetime.utcnow()) - datetime.timestamp(pub_t)) |
||
| 47 | < rss.before * 86400 |
||
| 48 | ): |
||
| 49 | if title: |
||
| 50 | msg += f"\n--{rss.title}--\n" |
||
| 51 | title = False |
||
| 52 | msg = msg + f"{str(c_no).zfill(2)}.{entry.title}\n{entry.link}\n" |
||
| 53 | no += 1 |
||
| 54 | c_no += 1 |
||
| 55 | if no % 20 == 0: |
||
| 56 | send(f"RSS 订阅", msg) |
||
| 57 | msg = "" |
||
| 58 | title = False |
||
| 59 | rss_history_list.append(History(url=entry.link)) |
||
| 60 | with db.atomic(): |
||
| 61 | History.bulk_create(rss_history_list, batch_size=10) |
||
| 62 | |||
| 63 | if no % 20 != 0 and msg: |
||
| 64 | send(f"RSS 订阅", msg) |
||
| 65 | |||
| 76 |