Add hashnode support for posts plugin and additional options (#165)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//Setup
|
||||
export default async function({login, data, imports, q, account}, {enabled = false} = {}) {
|
||||
export default async function({login, data, imports, q, queries, account}, {enabled = false} = {}) {
|
||||
//Plugin execution
|
||||
try {
|
||||
//Check if plugin is enabled and requirements are met
|
||||
@@ -7,7 +7,7 @@
|
||||
return null
|
||||
|
||||
//Load inputs
|
||||
let {source, limit, user} = imports.metadata.plugins.posts.inputs({data, account, q})
|
||||
let {source, descriptions, covers, limit, user} = imports.metadata.plugins.posts.inputs({data, account, q})
|
||||
|
||||
//Retrieve posts
|
||||
console.debug(`metrics/compute/${login}/plugins > posts > processing with source ${source}`)
|
||||
@@ -16,7 +16,12 @@
|
||||
//Dev.to
|
||||
case "dev.to":{
|
||||
console.debug(`metrics/compute/${login}/plugins > posts > querying api`)
|
||||
posts = (await imports.axios.get(`https://dev.to/api/articles?username=${user}&state=fresh`)).data.map(({title, readable_publish_date:date}) => ({title, date}))
|
||||
posts = (await imports.axios.get(`https://dev.to/api/articles?username=${user}&state=fresh`)).data.map(({title, description, published_at:date, cover_image:image}) => ({title, description, date, image}))
|
||||
break
|
||||
}
|
||||
//Hashnode
|
||||
case "hashnode":{
|
||||
posts = (await imports.axios.post("https://api.hashnode.com", {query:queries.posts.hashnode({user})}, {headers:{"Content-type":"application/json"}})).data.data.user.publication.posts.map(({title, brief:description, dateAdded:date, coverImage:image}) => ({title, description, date, image}))
|
||||
break
|
||||
}
|
||||
//Unsupported
|
||||
@@ -26,13 +31,18 @@
|
||||
|
||||
//Format posts
|
||||
if (Array.isArray(posts)) {
|
||||
//Limit tracklist
|
||||
//Limit posts
|
||||
if (limit > 0) {
|
||||
console.debug(`metrics/compute/${login}/plugins > posts > keeping only ${limit} posts`)
|
||||
posts.splice(limit)
|
||||
}
|
||||
//Cover images
|
||||
if (covers) {
|
||||
console.debug(`metrics/compute/${login}/plugins > posts > formatting cover images`)
|
||||
posts = await Promise.all(posts.map(async({image, ...post}) => ({image:await imports.imgb64(image, {width:144, height:-1}), ...post})))
|
||||
}
|
||||
//Results
|
||||
return {source, list:posts}
|
||||
return {source, descriptions, covers, list:posts}
|
||||
}
|
||||
|
||||
//Unhandled error
|
||||
|
||||
@@ -19,6 +19,19 @@ inputs:
|
||||
default: ""
|
||||
values:
|
||||
- dev.to # Dev.to
|
||||
- hashnode
|
||||
|
||||
# Display a few lines about each posts
|
||||
plugin_posts_descriptions:
|
||||
description: Display posts descriptions
|
||||
type: boolean
|
||||
default: no
|
||||
|
||||
# Display posts cover images
|
||||
plugin_posts_covers:
|
||||
description: Display posts cover images
|
||||
type: boolean
|
||||
default: no
|
||||
|
||||
# Number of posts to display
|
||||
plugin_posts_limit:
|
||||
|
||||
12
source/plugins/posts/queries/hashnode.graphql
Normal file
12
source/plugins/posts/queries/hashnode.graphql
Normal file
@@ -0,0 +1,12 @@
|
||||
query PostsHashnode {
|
||||
user(username: "$user"){
|
||||
publication{
|
||||
posts(page: 1) {
|
||||
title
|
||||
brief
|
||||
coverImage
|
||||
dateAdded
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,3 +5,11 @@
|
||||
plugin_posts: yes
|
||||
plugin_posts_source: dev.to
|
||||
plugin_posts_user: lowlighter
|
||||
|
||||
- name: Posts plugin (hashnode)
|
||||
uses: lowlighter/metrics@latest
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_posts: yes
|
||||
plugin_posts_source: hashnode
|
||||
plugin_posts_user: lowlighter
|
||||
|
||||
Reference in New Issue
Block a user