Skip to content

Instantly share code, notes, and snippets.

@ci7lus

ci7lus/pdc-comic-meteor.ts

Last active Aug 13, 2020
Embed
What would you like to do?
import { defineComponent } from "ironpipe"
import axios from "axios"
import moment from "moment"
/**
* comic-meteor.ts
* MIT License (c) 2020 ci7lus
*/
module.exports = defineComponent({
name: "comic-meteor",
version: "0.0.1",
props: {
titles: {
type: "string",
label: "Target title list",
description: "List of target titles (separate with ,)",
},
timer: {
type: "$.interface.timer",
default: {
cron: "30 3 * * *", // 12:30 JST
},
},
db: "$.service.db",
},
methods: {},
dedupe: "unique",
async run() {
const titles = this.titles.split(",")
const now = moment().startOf("minutes")
const yest = now.clone().subtract(1, "days")
// すいません、実行30秒制限考慮に温玉付きをお願いします。
const histories = this.db.get<{ [key: string]: string }>("histories") || {}
const sorted = Object.fromEntries(
titles
.map(
(title) =>
[title, title in histories ? moment(histories[title]) : yest] as [
string,
moment.Moment
]
)
.sort((a, b) => a[1].diff(b[1]))
)
const target = Object.entries(sorted)
.filter(([_, m]) => 1 < now.diff(m, "hours"))
.map(([s, _]) => s)
.splice(0, 5)
console.log(`target: ${target.join(",")}`)
for (let title of target) {
try {
const r = await axios.get<Feed>(
`https://7c34d95b956bda5dc5db046915498d63.m.pipedream.net/${title}.json1`,
{
validateStatus: () => true,
timeout: 5000,
}
)
if (r.status !== 200) {
console.warn(`${title}の取得に失敗しました: ${r.status}`)
continue
}
const feed = r.data
const comic = Object.assign({}, feed)
delete comic["items"]
feed.items.reverse().map((item) => {
this.$emit(
{
...item,
comic,
},
{
id: item.url,
summary: `${feed.title} ${item.title}`,
}
)
})
sorted[title] = now
} catch (error) {
console.error(error)
}
}
this.db.set(
"histories",
Object.fromEntries(
Object.entries(sorted).map(([s, m]) => [s, m.format()])
)
)
},
})
type Feed = {
title: string
home_page_url: string
description: string
icon: string
author: {
name: string
url: string
}
items: { id: string; url: string; title: string; date_modified: string }[]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.