def add_qnmlgb_feed(feed_url): """ 解析 qnmlgb.tech RSS 源 :param feed_url: :return: """ feed_obj = feedparser.parse(feed_url) title = feed_obj.feed.get('title') name = get_hash_name(settings.QNMLGB_HOST + title) site = Site.objects.filter(name=name) if site: logger.info(f"源已经存在:`{feed_url}") return {"site": site[0].pk} cname = title[:20] link = feed_obj.feed.link[:1024] brief = feed_obj.feed.subtitle[:200] favicon = feed_obj.feed.image.link.replace('/64.ico', '/96') try: site = Site(name=name, cname=cname, link=link, brief=brief, star=19, copyright=20, creator='wemp', rss=feed_url, favicon=favicon) site.save() return {"site": site.pk} except: logger.error(f'处理源出现异常:`{feed_url}') return None
def save_avatar(avatar, userid, size=100, referer=None): """ 保存网络头像 :param avatar: :param userid: :param size: :param referer: :return: 保存后的头像地址 """ try: headers = {'Referer': referer} if referer else {} rsp = requests.get(avatar, timeout=15, headers=headers) if rsp.ok: img_obj = Image.open(BytesIO(rsp.content)) img_obj.thumbnail((size, size)) jpg = get_hash_name(userid) + '.jpg' if img_obj.mode != 'RGB': img_obj = img_obj.convert('RGB') img_obj.save( os.path.join(settings.BASE_DIR, 'assets', 'avatar', jpg)) return f'/assets/avatar/{jpg}' else: logger.error(f"同步用户头像出现网络异常!`{userid}`{avatar}") except (HTTPError, Timeout, ConnectionError): logger.error(f"同步用户头像网络异常!`{userid}`{avatar}") except: logger.error(f"同步用户头像未知异常`{userid}`{avatar}") return '/assets/img/logo.svg'
def add_postcast_feed(feed_obj): """ 播客类型的 RSS 源 :param feed_obj: :return: 解析结果,成功字典;失败 None """ url = feed_obj.url if feed_obj.feed.get('title'): name = get_hash_name(url) site = Site.objects.filter(name=name, status='active') if site: logger.info(f"源已经存在:`{url}") return {"site": site[0].pk} cname = feed_obj.feed.title[:50] try: link = feed_obj.feed.content_detail.base except AttributeError: if feed_obj.feed.get('link'): link = feed_obj.feed.link[:1024] else: link = url if feed_obj.feed.get('subtitle'): brief = trim_brief(get_html_text(feed_obj.feed.subtitle))[:200] else: brief = feed_obj.feed.title try: author = feed_obj.feed.author_detail.name except AttributeError: author = feed_obj.feed.get('author') or get_short_host_name(link) # 使用默认头像 if feed_obj.feed.get('image'): favicon = save_avatar(feed_obj.feed.image.href, name) else: favicon = generate_rss_avatar(link) try: site = Site(name=name, cname=cname, link=link, brief=brief, star=10, copyright=30, creator='podcast', rss=url, favicon=favicon, author=author) site.save() return {"site": site.pk} except: logger.error(f'新增播客异常:`{url}') else: logger.warning(f"播客解析失败:`{url}") return None
def add_atom_feed(feed_obj): """ 解析普通的 RSS 源 :param feed_obj: :return: 解析结果,成功字典;失败 None """ url = feed_obj.url if feed_obj.feed.get('title'): name = get_hash_name(url) site = Site.objects.filter(name=name, status='active') if site: logger.info(f"源已经存在:`{url}") return {"site": site[0].pk} cname = feed_obj.feed.title[:50] if feed_obj.feed.get('link'): link = feed_obj.feed.link[:1024] else: link = url if feed_obj.feed.get('subtitle'): brief = trim_brief(get_html_text(feed_obj.feed.subtitle))[:200] else: brief = feed_obj.feed.title author = feed_obj.feed.get('author') or get_short_host_name(link) # 使用默认头像 favicon = generate_rss_avatar(link) try: site = Site(name=name, cname=cname, link=link, brief=brief, star=9, copyright=30, creator='user', rss=url, favicon=favicon, author=author) site.save() return {"site": site.pk, "creator": "user"} except: logger.error(f'处理源出现异常:`{url}') else: logger.warning(f"RSS 解析失败:`{url}") return None
def valid_dvc_req(dvc_id, dvc_type, sign): src = dvc_id + dvc_type + current_day() return get_hash_name(src) == sign