mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 13:43:06 -04:00
add news
This commit is contained in:
parent
5b7e9841a4
commit
3a016dde3d
5 changed files with 154 additions and 1 deletions
127
commands/message/search/news.js
Normal file
127
commands/message/search/news.js
Normal file
|
@ -0,0 +1,127 @@
|
|||
const { googleNews, googleNewsSupplemental} = require('#api');
|
||||
const { PERMISSION_GROUPS } = require('#constants');
|
||||
|
||||
const { createDynamicCardStack } = require("#cardstack/index");
|
||||
const { ResolveCallbackTypes } = require("#cardstack/constants");
|
||||
|
||||
const { createEmbed, page } = require('#utils/embed');
|
||||
const { acknowledge } = require('#utils/interactions');
|
||||
const { link } = require('#utils/markdown');
|
||||
const { editOrReply } = require('#utils/message');
|
||||
const { STATIC_ASSETS, STATIC_ICONS, STATICS} = require("#utils/statics");
|
||||
|
||||
function renderNewsCard(context, res, supplementalKey, includeSupplementalData = true){
|
||||
|
||||
// search_service/endpoints/google-news#NEWS_CARD_TYPES.COLLECTION
|
||||
if(res.type && res.type === 2) res = res.cards[0];
|
||||
|
||||
let result = createEmbed("default", context, {
|
||||
author: {
|
||||
name: res.publisher.name,
|
||||
iconUrl: res.publisher.icon
|
||||
},
|
||||
title: res.title,
|
||||
description: `${res.description}`,
|
||||
fields: [],
|
||||
footer: {
|
||||
iconUrl: STATICS.googlenews,
|
||||
text: `Google News • ${context.application.name}`
|
||||
}
|
||||
})
|
||||
|
||||
if(res.url) result.url = res.url;
|
||||
|
||||
if(res.image) result.image = { url: res.image };
|
||||
|
||||
return page(result, {}, includeSupplementalData ? {
|
||||
full_coverage_key: supplementalKey
|
||||
} : {});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
name: 'news',
|
||||
label: 'query',
|
||||
aliases: ['n'],
|
||||
metadata: {
|
||||
description: 'Show the latest News from around the world or search for topics.',
|
||||
description_short: 'Search News',
|
||||
examples: [
|
||||
'news germany',
|
||||
'news latest movies'
|
||||
],
|
||||
category: 'search',
|
||||
usage: 'news [<query>]',
|
||||
//slashCommand: "anime"
|
||||
},
|
||||
permissionsClient: [...PERMISSION_GROUPS.baseline],
|
||||
run: async (context, args) => {
|
||||
await acknowledge(context);
|
||||
|
||||
try{
|
||||
let search = await googleNews(context, args.query)
|
||||
search = search.response
|
||||
|
||||
if(search.body.status === 2) return editOrReply(context, createEmbed("error", context, search.body.message))
|
||||
|
||||
let pages = []
|
||||
for(const res of search.body.cards){
|
||||
let sup;
|
||||
if(res.supplemental_key || res.supplemental_key === 0) sup = search.body.supplemental_keys[res.supplemental_key]
|
||||
pages.push(renderNewsCard(context, res, sup, true))
|
||||
}
|
||||
|
||||
if(!pages.length) return editOrReply(context, createEmbed("warning", context, `No results found.`))
|
||||
|
||||
createDynamicCardStack(context, {
|
||||
cards: pages,
|
||||
interactive: {
|
||||
full_coverage_button: {
|
||||
label: "Full Coverage",
|
||||
icon: "button_full_coverage",
|
||||
inline: true,
|
||||
visible: (pg) => {
|
||||
return (pg.getState("full_coverage_key") !== null)
|
||||
},
|
||||
condition: true,
|
||||
renderLoadingState: (pg) => {
|
||||
return createEmbed("default", context, {
|
||||
author: {
|
||||
name: "Full Coverage",
|
||||
iconUrl: STATIC_ICONS.full_coverage,
|
||||
},
|
||||
image: {
|
||||
url: STATIC_ASSETS.card_skeleton
|
||||
}
|
||||
})
|
||||
},
|
||||
resolvePage: async (pg) => {
|
||||
let fullCoverage = await googleNewsSupplemental(context, pg.getState("full_coverage_key"));
|
||||
|
||||
let cards = fullCoverage.response.body.cards.map((c)=>renderNewsCard(context, c, undefined, false))
|
||||
|
||||
return {
|
||||
type: ResolveCallbackTypes.SUBSTACK,
|
||||
cards: cards.length >= 1 ? cards : [
|
||||
// This happens if the episode metadata resolver fails.
|
||||
page(createEmbed("defaultNoFooter", context, {
|
||||
author: {
|
||||
name: "Full Coverage",
|
||||
iconUrl: STATIC_ICONS.full_coverage,
|
||||
},
|
||||
description: `\n## Full Coverage Unavailable\n\nWe're unable to display Full Coverage for this story.`
|
||||
}))
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}catch(e){
|
||||
if(e.response?.body?.status === 1) return editOrReply(context, createEmbed("warning", context, e.response?.body?.message))
|
||||
if(e.response?.body?.status === 2) return editOrReply(context, createEmbed("warning", context, e.response?.body?.message))
|
||||
|
||||
console.log(e)
|
||||
return editOrReply(context, createEmbed("error", context, `Unable to perform news search.`))
|
||||
}
|
||||
}
|
||||
};
|
|
@ -40,6 +40,8 @@ const Api = Object.freeze({
|
|||
SEARCH_BING_IMAGES: '/search/bing-images',
|
||||
SEARCH_GOOGLE: '/search/google',
|
||||
SEARCH_GOOGLE_IMAGES: '/search/google-images',
|
||||
SEARCH_GOOGLE_NEWS: '/search/google-news',
|
||||
SEARCH_GOOGLE_NEWS_SUPPLEMENTAL: '/search/google-news-supplemental',
|
||||
SEARCH_LYRICS: '/search/lyrics',
|
||||
SEARCH_MAPS: '/search/maps',
|
||||
SEARCH_MAPS_SUPPLEMENTAL: '/search/maps-supplemental',
|
||||
|
|
|
@ -126,6 +126,18 @@ module.exports.googleImages = async function(context, query, nsfw){
|
|||
})
|
||||
}
|
||||
|
||||
module.exports.googleNews = async function(context, query, nsfw){
|
||||
return await request(Api.SEARCH_GOOGLE_NEWS, "GET", {}, {
|
||||
q: query
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.googleNewsSupplemental = async function(context, supplementalKey){
|
||||
return await request(Api.SEARCH_GOOGLE_NEWS_SUPPLEMENTAL, "GET", {}, {
|
||||
supplemental_key: supplementalKey
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.lyrics = async function(context, query){
|
||||
return await request(Api.SEARCH_LYRICS, "GET", {}, {
|
||||
q: query
|
||||
|
|
|
@ -100,6 +100,8 @@ module.exports.ICONS_NEXTGEN = Object.freeze({
|
|||
"open_in_new": "<:nextgen_ico_open_in_new:1336075848528429086>",
|
||||
"open_in_new_alt": "<:nextgen_ico_open_in_new_alt:1336075859181965322>",
|
||||
|
||||
"button_full_coverage": "<:ico_full_coverage:1341535912017793045>",
|
||||
|
||||
/* Brands */
|
||||
"brand": "<:nextgen_ico_brand:1336064940670193780>",
|
||||
"brand_discord": "<:nextgen_ico_brand_discord:1336064953727320256>",
|
||||
|
|
|
@ -96,6 +96,10 @@ const Statics = Object.freeze({
|
|||
file: "brands/maps.png",
|
||||
revision: 0
|
||||
},
|
||||
googlenews: {
|
||||
file: "brands/google_news.png",
|
||||
revision: 0
|
||||
},
|
||||
googletranslate: {
|
||||
file: "brands/googletranslate.png",
|
||||
revision: 0
|
||||
|
@ -233,6 +237,10 @@ const Statics = Object.freeze({
|
|||
search_calculator: {
|
||||
file: "search/calculator.78rasnkwtvo0.png",
|
||||
revision: 1
|
||||
},
|
||||
full_coverage: {
|
||||
file: "search/full_coverage.7aewvft4qrc0.png",
|
||||
revision: 0
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -254,6 +262,7 @@ module.exports.STATICS = Object.freeze({
|
|||
googledictionary: staticAsset(Statics.brands.googledictionary),
|
||||
googlefinance: staticAsset(Statics.brands.googlefinance),
|
||||
googlemaps: staticAsset(Statics.brands.googlemaps),
|
||||
googlenews: staticAsset(Statics.brands.googlenews),
|
||||
googletranslate: staticAsset(Statics.brands.googletranslate),
|
||||
emojipedia: staticAsset(Statics.brands.emojipedia),
|
||||
inferkit: staticAsset(Statics.brands.inferkit),
|
||||
|
@ -291,7 +300,8 @@ module.exports.STATIC_ICONS = Object.freeze({
|
|||
ai_image: staticAsset(Statics.icons.ai_image),
|
||||
ai_image_processing: staticAsset(Statics.icons.ai_image_processing),
|
||||
warning: staticAsset(Statics.icons.warning),
|
||||
search_calculator: staticAsset(Statics.icons.search_calculator)
|
||||
search_calculator: staticAsset(Statics.icons.search_calculator),
|
||||
full_coverage: staticAsset(Statics.icons.full_coverage)
|
||||
})
|
||||
|
||||
module.exports.STATIC_ASSETS = Object.freeze({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue