2022-07-31 08:27:58 +00:00
|
|
|
import random
|
2022-01-20 12:13:03 +00:00
|
|
|
import traceback
|
2021-11-02 11:14:21 +00:00
|
|
|
from datetime import datetime, timedelta
|
2021-11-12 14:25:53 +00:00
|
|
|
from urllib.parse import quote
|
|
|
|
|
|
|
|
import ujson as json
|
2021-10-24 13:19:37 +00:00
|
|
|
|
2021-08-24 15:58:57 +00:00
|
|
|
from config import Config
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.builtins import Url, Bot
|
2023-03-04 08:51:56 +00:00
|
|
|
from core.component import module
|
2021-08-24 15:58:57 +00:00
|
|
|
from core.logger import Logger
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.scheduler import IntervalTrigger
|
|
|
|
from core.utils.http import get_url
|
|
|
|
from core.utils.storedata import get_stored_list, update_stored_list
|
2021-11-01 17:06:19 +00:00
|
|
|
|
|
|
|
|
2021-11-10 15:54:45 +00:00
|
|
|
class Article:
|
|
|
|
count = 10
|
2021-12-08 14:58:37 +00:00
|
|
|
tags = ['minecraft:article/news', 'minecraft:article/insider', 'minecraft:article/culture',
|
|
|
|
'minecraft:article/merch', 'minecraft:stockholm/news', 'minecraft:stockholm/guides',
|
|
|
|
'minecraft:stockholm/deep-dives', 'minecraft:stockholm/merch', 'minecraft:stockholm/events',
|
|
|
|
'minecraft:stockholm/minecraft-builds', 'minecraft:stockholm/marketplace']
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def random_tags():
|
|
|
|
tags = Article.tags
|
|
|
|
long = len(tags)
|
|
|
|
m = long // 2
|
|
|
|
random_tags = []
|
|
|
|
|
|
|
|
def random_choice():
|
|
|
|
c = random.choice(tags)
|
|
|
|
if c not in random_tags:
|
|
|
|
random_tags.append(c)
|
|
|
|
else:
|
|
|
|
random_choice()
|
|
|
|
|
|
|
|
for _ in range(m):
|
|
|
|
random_choice()
|
|
|
|
return random_tags
|
2021-11-10 15:54:45 +00:00
|
|
|
|
|
|
|
|
2023-03-01 13:41:42 +00:00
|
|
|
bot = Bot.FetchTarget
|
|
|
|
|
2023-03-04 08:51:56 +00:00
|
|
|
minecraft_news = module('minecraft_news', developers=['_LittleC_', 'OasisAkari', 'Dianliang233'],
|
|
|
|
recommend_modules=['feedback_news'],
|
2023-03-19 06:36:34 +00:00
|
|
|
desc='{minecraft_news.help.minecraft_news}', alias='minecraftnews')
|
2023-03-01 13:41:42 +00:00
|
|
|
|
|
|
|
|
2023-03-04 08:51:56 +00:00
|
|
|
@minecraft_news.handle(IntervalTrigger(seconds=60 if not Config('slower_schedule') else 180))
|
2023-03-01 13:41:42 +00:00
|
|
|
async def start_check_news():
|
2021-08-24 15:58:57 +00:00
|
|
|
baseurl = 'https://www.minecraft.net'
|
2021-11-12 14:25:53 +00:00
|
|
|
url = quote(
|
2021-12-08 14:58:37 +00:00
|
|
|
f'https://www.minecraft.net/content/minecraft-net/_jcr_content.articles.grid?tileselection=auto&tagsPath={",".join(Article.random_tags())}&offset=0&pageSize={Article.count}')
|
2021-11-08 16:09:06 +00:00
|
|
|
webrender = Config('web_render')
|
2021-08-24 15:58:57 +00:00
|
|
|
if not webrender:
|
|
|
|
return
|
2022-12-18 14:28:04 +00:00
|
|
|
try:
|
|
|
|
get = webrender + 'source?url=' + url
|
2023-01-19 06:30:46 +00:00
|
|
|
getpage = await get_url(get, 200, attempt=1, logging_err_resp=False)
|
2022-12-18 14:28:04 +00:00
|
|
|
if getpage:
|
|
|
|
alist = get_stored_list(bot, 'mcnews')
|
|
|
|
o_json = json.loads(getpage)
|
|
|
|
o_nws = o_json['article_grid']
|
|
|
|
Article.count = o_json['article_count']
|
|
|
|
for o_article in o_nws:
|
|
|
|
default_tile = o_article['default_tile']
|
|
|
|
title = default_tile['title']
|
|
|
|
desc = default_tile['sub_header']
|
|
|
|
link = baseurl + o_article['article_url']
|
|
|
|
if title not in alist:
|
|
|
|
publish_date = datetime.strptime(o_article['publish_date'], '%d %B %Y %H:%M:%S %Z')
|
|
|
|
now = datetime.now()
|
|
|
|
if now - publish_date < timedelta(days=2):
|
2023-03-29 14:42:53 +00:00
|
|
|
await bot.post_message('minecraft_news', 'minecraft_news.message.minecraft_news', i18n=True,
|
|
|
|
title=title, desc=desc, link=str(Url(link)))
|
2022-12-18 14:28:04 +00:00
|
|
|
alist.append(title)
|
|
|
|
update_stored_list(bot, 'mcnews', alist)
|
|
|
|
except Exception:
|
|
|
|
if Config('debug'):
|
|
|
|
Logger.error(traceback.format_exc())
|
2021-10-08 11:54:27 +00:00
|
|
|
|
2023-01-04 06:04:16 +00:00
|
|
|
|
2023-03-04 08:51:56 +00:00
|
|
|
feedback_news = module('feedback_news', developers=['Dianliang233'], recommend_modules=['minecraft_news'],
|
2023-03-19 06:36:34 +00:00
|
|
|
desc='{minecraft_news.help.feedback_news}',
|
2023-03-04 08:51:56 +00:00
|
|
|
alias='feedbacknews')
|
2023-03-01 13:41:42 +00:00
|
|
|
|
|
|
|
|
2023-03-04 08:51:56 +00:00
|
|
|
@feedback_news.handle(IntervalTrigger(seconds=300))
|
2023-03-01 13:41:42 +00:00
|
|
|
async def feedback_news():
|
2021-11-12 14:25:53 +00:00
|
|
|
sections = [{'name': 'beta',
|
|
|
|
'url': 'https://minecraftfeedback.zendesk.com/api/v2/help_center/en-us/sections/360001185332/articles?per_page=5'},
|
|
|
|
{'name': 'article',
|
|
|
|
'url': 'https://minecraftfeedback.zendesk.com/api/v2/help_center/en-us/sections/360001186971/articles?per_page=5'}]
|
2021-10-01 02:28:33 +00:00
|
|
|
for section in sections:
|
|
|
|
try:
|
2022-06-12 14:30:02 +00:00
|
|
|
alist = get_stored_list(bot, 'mcfeedbacknews')
|
2023-01-19 06:30:46 +00:00
|
|
|
get = await get_url(section['url'], 200, attempt=1, logging_err_resp=False)
|
2021-10-01 14:00:35 +00:00
|
|
|
res = json.loads(get)
|
2021-10-01 02:28:33 +00:00
|
|
|
articles = []
|
|
|
|
for i in res['articles']:
|
|
|
|
articles.append(i)
|
|
|
|
for article in articles:
|
|
|
|
if article['name'] not in alist:
|
|
|
|
name = article['name']
|
|
|
|
link = article['html_url']
|
|
|
|
Logger.info(f'huh, we find {name}.')
|
2023-03-29 14:42:53 +00:00
|
|
|
await bot.post_message('feedback_news', 'minecraft_news.message.feedback_news', i18n=True,
|
|
|
|
name=name, link=str(Url(link)))
|
2022-06-12 14:30:02 +00:00
|
|
|
alist.append(name)
|
|
|
|
update_stored_list(bot, 'mcfeedbacknews', alist)
|
2021-10-01 02:28:33 +00:00
|
|
|
except Exception:
|
2022-12-08 01:28:54 +00:00
|
|
|
if Config('debug'):
|
|
|
|
Logger.error(traceback.format_exc())
|