forked from 11ty/11ty-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-avatars-twitter.js
108 lines (93 loc) · 2.75 KB
/
node-avatars-twitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const slugify = require("slugify");
const fs = require("fs-extra");
const fastglob = require("fast-glob");
const eleventyImg = require("@11ty/eleventy-img");
const cleanName = require("./config/cleanAuthorName");
const getTwitterAvatarUrl = require("twitter-avatar-url");
eleventyImg.concurrency = 5;
async function fetch(entry) {
let name = entry.username;
let url = entry.url.large;
if(!name || !url) {
return;
}
let slug = slugify(name).toLowerCase();
let dir = `./avatars/twitter/`;
await fs.ensureDir(dir);
let path = `${dir}${slug}.json`;
try {
console.log( "Fetching", name, url );
// let url = `https://unavatar.now.sh/twitter/${name}?fallback=false`;
let stats = await eleventyImg(url, {
formats: ["avif", "webp", "jpeg"],
widths: [90],
urlPath: "/img/avatars/twitter/",
outputDir: "./src/img/avatars/twitter/",
cacheOptions: {
duration: "30d",
}
});
return fs.writeFile(path, JSON.stringify(stats, null, 2));
} catch(e) {
console.log( "Error: ", e );
// return fs.unlink(path);
}
}
(async function() {
let twitterUsernames = new Set();
// Twitter
let testimonials = require("./src/_data/testimonials.json").map(entry => entry.twitter);
for(let twitter of testimonials) {
twitterUsernames.add(cleanName(twitter).toLowerCase());
}
// Starters
let starters = await fastglob("./src/_data/starters/*.json", {
caseSensitiveMatch: false
});
for(let site of starters) {
let siteData = require(site);
if(siteData.author) {
twitterUsernames.add(cleanName(siteData.author).toLowerCase());
}
}
// Plugins
let plugins = await fastglob("./src/_data/plugins/*.json", {
caseSensitiveMatch: false
});
for(let plugin of plugins) {
let pluginData = require(plugin);
if(pluginData.author) {
twitterUsernames.add(cleanName(pluginData.author).toLowerCase());
}
}
// Extras
let extras = require("./src/_data/extraAvatars.json").map(entry => entry.twitter);
for(let twitter of extras) {
twitterUsernames.add(cleanName(twitter).toLowerCase());
}
let sites = await fastglob("./src/_data/builtwith/*.json", {
caseSensitiveMatch: false
});
for(let site of sites) {
let siteData = require(site);
if(siteData.opened_by) {
let name = cleanName(siteData.opened_by).toLowerCase();
if(name.startsWith("twitter:")) {
twitterUsernames.add(name.substr("twitter:".length));
}
}
for(let author of siteData.authors) {
let name = cleanName(author).toLowerCase();
if(name.startsWith("twitter:")) {
twitterUsernames.add(name.substr("twitter:".length));
}
}
}
console.log( "Found", twitterUsernames.size, "usernames" );
let allTwitterUrls = await getTwitterAvatarUrl(Array.from(twitterUsernames), {
twitterApiVersion: 1
});
for(let entry of allTwitterUrls) {
await fetch(entry);
}
})();