utxo-prague/utils/twitter.js

99 řádky
2.5 KiB
JavaScript
Surový Normální zobrazení Historie

2022-01-14 17:56:42 +01:00
import { config } from "https://deno.land/x/dotenv/mod.ts";
2023-01-25 22:50:39 +01:00
import SimpleTwitter from "https://raw.githubusercontent.com/burningtree/twit-deno/master/simple_twitter_deno.ts";
2022-01-14 17:56:42 +01:00
import { Table } from "https://deno.land/x/cliffy@v0.20.1/table/mod.ts";
import { UTXOEngine } from "./engine.js";
import { exists } from "https://deno.land/std/fs/mod.ts";
import { fromStreamReader } from "https://deno.land/std@0.60.0/io/streams.ts";
2022-01-04 09:32:08 +01:00
2022-01-14 17:56:42 +01:00
const utxo = new UTXOEngine({ silent: true });
await utxo.init();
2022-01-04 09:32:08 +01:00
2022-01-14 17:56:42 +01:00
config({ path: ".env", export: true });
2022-01-04 09:32:08 +01:00
2023-01-25 22:50:39 +01:00
const twitterImagesPath = "./spec/23/photos/";
2022-01-06 05:18:28 +01:00
2022-01-04 09:32:08 +01:00
const simple_twitter = new SimpleTwitter({
consumer_key: Deno.env.get("CONSUMER_KEY"),
consumer_secret: Deno.env.get("CONSUMER_SECRET"),
access_token: Deno.env.get("ACCESS_TOKEN"),
access_token_secret: Deno.env.get("ACCESS_TOKEN_SECRET"),
2022-01-14 17:56:42 +01:00
bearer_token: Deno.env.get("BEARER_TOKEN"),
});
2022-01-04 09:32:08 +01:00
2023-01-25 22:50:39 +01:00
const entryId = "23";
2022-01-14 17:56:42 +01:00
const entry = utxo.entries[entryId];
2022-01-04 09:32:08 +01:00
2022-04-22 10:50:05 +02:00
const collections = ["speakers", "partners"];
2022-01-19 01:15:57 +01:00
2022-01-14 17:56:42 +01:00
const arr = [];
let total = 0;
let items = [];
2022-01-04 09:32:08 +01:00
2022-01-19 01:15:57 +01:00
async function fetchImageAndSave(tw, imageFn) {
2022-01-09 12:36:25 +01:00
if (!await exists(imageFn)) {
2022-01-14 17:56:42 +01:00
const url = tw.profile_image_url_https.replace("_normal", "");
const res = await fetch(url);
const file = await Deno.open(imageFn, { create: true, write: true });
const reader = fromStreamReader(res.body.getReader());
await Deno.copy(reader, file);
file.close();
2022-01-19 01:15:57 +01:00
console.log(`Saved file: ${imageFn} (url=${url})`);
2022-01-09 12:36:25 +01:00
}
}
2022-01-19 01:15:57 +01:00
for (const col of collections) {
for (const sp of entry.specs[col]) {
if (!sp.twitter) {
continue;
}
2022-01-19 04:57:43 +01:00
if (
Deno.args[0] === "photos" && sp.photos.find((x) => x.match(/^twitter:/))
) {
continue;
}
2022-02-23 13:06:10 +01:00
let tw;
try {
tw = await twitterUser(sp.twitter);
if (!tw) {
continue;
}
} catch (e) {
2022-03-14 13:55:02 +01:00
console.log(sp.twitter, e);
Deno.exit(1);
2022-01-19 01:15:57 +01:00
}
await fetchImageAndSave(
tw,
twitterImagesPath + col + "/" + sp.id + "-twitter.jpg",
);
items.push([
`${col}${sp.type ? ":" + sp.type : ""}`,
tw.screen_name,
tw.followers_count,
]);
total += tw.followers_count;
2022-01-04 09:32:08 +01:00
}
}
2022-01-14 17:56:42 +01:00
arr.push(...items.sort((x, y) => x[2] < y[2] ? 1 : -1));
2022-01-04 09:32:08 +01:00
2022-01-14 17:56:42 +01:00
arr.push([]);
arr.push(["total", "", total]);
const table = Table.from(arr);
2022-01-19 04:57:43 +01:00
if (!Deno.args[0]) {
console.log("\nTwitter followers count:\n\n" + table.toString() + "\n");
}
2022-01-04 09:32:08 +01:00
async function twitterUser(screen_name) {
2022-01-14 17:56:42 +01:00
const resp = await simple_twitter.get("users/lookup", { screen_name });
2022-01-04 09:32:08 +01:00
if (resp.length === 1) {
2022-01-14 17:56:42 +01:00
return resp[0];
2022-01-04 09:32:08 +01:00
}
2022-01-14 17:56:42 +01:00
return null;
2022-01-04 09:32:08 +01:00
}