(self, comment, weibo)
| 2599 | con.close() |
| 2600 | |
| 2601 | def parse_sqlite_comment(self, comment, weibo): |
| 2602 | if not comment: |
| 2603 | return |
| 2604 | sqlite_comment = OrderedDict() |
| 2605 | sqlite_comment["id"] = comment["id"] |
| 2606 | |
| 2607 | self._try_get_value("bid", "bid", sqlite_comment, comment) |
| 2608 | self._try_get_value("root_id", "rootid", sqlite_comment, comment) |
| 2609 | self._try_get_value("created_at", "created_at", sqlite_comment, comment) |
| 2610 | sqlite_comment["weibo_id"] = weibo["id"] |
| 2611 | |
| 2612 | sqlite_comment["user_id"] = comment["user"]["id"] |
| 2613 | sqlite_comment["user_screen_name"] = comment["user"]["screen_name"] |
| 2614 | self._try_get_value( |
| 2615 | "user_avatar_url", "avatar_hd", sqlite_comment, comment["user"] |
| 2616 | ) |
| 2617 | if self.remove_html_tag: |
| 2618 | sqlite_comment["text"] = re.sub('<[^<]+?>', '', comment["text"]).replace('\n', '').strip() |
| 2619 | else: |
| 2620 | sqlite_comment["text"] = comment["text"] |
| 2621 | |
| 2622 | sqlite_comment["pic_url"] = "" |
| 2623 | if comment.get("pic"): |
| 2624 | sqlite_comment["pic_url"] = comment["pic"]["large"]["url"] |
| 2625 | if sqlite_comment["pic_url"] and self.comment_pic_download: |
| 2626 | pic_url = sqlite_comment["pic_url"] |
| 2627 | |
| 2628 | # 评论图片目录:weibo/<用户目录>/<用户昵称>_comments_img |
| 2629 | csv_path = self.get_filepath("csv") |
| 2630 | user_dir = os.path.dirname(csv_path) |
| 2631 | if not os.path.isdir(user_dir): |
| 2632 | os.makedirs(user_dir) |
| 2633 | screen_name = self.user.get("screen_name") or str( |
| 2634 | self.user_config.get("user_id", "") |
| 2635 | ) |
| 2636 | safe_screen_name = re.sub(r'[\\/:*?"<>|]', "_", str(screen_name)) |
| 2637 | pic_path = os.path.join(user_dir, f"{safe_screen_name}_comments_img") |
| 2638 | if not os.path.exists(pic_path): |
| 2639 | os.makedirs(pic_path) |
| 2640 | |
| 2641 | # 文件名包含 微博用户昵称 + weibo_id + 评论用户昵称 + comments |
| 2642 | # 为避免重名,如果已存在则在末尾追加 _1/_2/... 序号 |
| 2643 | weibo_id = sqlite_comment["weibo_id"] |
| 2644 | comment_user = sqlite_comment.get("user_screen_name", "") |
| 2645 | safe_comment_user = re.sub(r'[\\/:*?"<>|]', "_", str(comment_user)) |
| 2646 | base_name = "{screen_name}_{weibo_id}_{comment_user}_comments".format( |
| 2647 | screen_name=safe_screen_name, |
| 2648 | weibo_id=weibo_id, |
| 2649 | comment_user=safe_comment_user, |
| 2650 | ) |
| 2651 | pic_name = base_name + ".jpg" |
| 2652 | idx = 1 |
| 2653 | while os.path.exists(os.path.join(pic_path, pic_name)): |
| 2654 | pic_name = f"{base_name}_{idx}.jpg" |
| 2655 | idx += 1 |
| 2656 | pic_full_path = os.path.join(pic_path, pic_name) |
| 2657 | if not os.path.exists(pic_full_path): |
| 2658 | try: |
no test coverage detected