rimgo/src/fetchers.ts

119 lines
4.3 KiB
TypeScript
Raw Normal View History

import cheerio from 'cheerio';
import got, { Response } from 'got';
import { HttpsProxyAgent, HttpProxyAgent } from 'hpagent';
import { globalAgent as httpGlobalAgent } from 'http';
import { globalAgent as httpsGlobalAgent } from 'https';
import CONFIG from './config';
const GALLERY_JSON_REGEX = /window\.postDataJSON=(".*")$/;
const agent = {
2021-10-07 06:15:35 +00:00
http: CONFIG.http_proxy
? new HttpProxyAgent({
2021-10-07 06:15:35 +00:00
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: CONFIG.http_proxy,
})
: httpGlobalAgent,
2021-10-07 06:15:35 +00:00
https: CONFIG.https_proxy
? new HttpsProxyAgent({
2021-10-07 06:15:35 +00:00
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: CONFIG.https_proxy,
})
: httpsGlobalAgent
};
2021-11-25 12:50:55 +00:00
const get = async (url: string): Promise<Response<string>> => {
try {
return got(url, { agent });
}
catch (err) {
console.error(`Error getting ${url}`);
throw err;
}
};
2021-10-06 17:47:10 +00:00
export const fetchAlbumURL = async (albumID: string): Promise<string> => {
// https://imgur.com/a/DfEsrAB
2021-11-25 12:50:55 +00:00
const response = await get(`https://imgur.com/a/${albumID}`);
2021-10-06 17:47:10 +00:00
const $ = cheerio.load(response.body);
const url = $('head meta[property="og:image"]').attr('content')?.replace(/\/\?.*$/, '');
if (!url) {
2021-11-25 12:50:55 +00:00
throw new Error(`Could not read image url for album ${albumID}`);
2021-10-06 17:47:10 +00:00
}
return url;
};
2021-11-25 12:50:55 +00:00
export const fetchAlbum = async (albumID: string): Promise<Media[]> => {
2021-10-06 17:47:10 +00:00
// https://api.imgur.com/post/v1/albums/zk7mdKH?client_id=${CLIENT_ID}&include=media%2Caccount
2021-11-25 12:50:55 +00:00
const response = await get(
2021-10-07 06:15:35 +00:00
`https://api.imgur.com/post/v1/albums/${albumID}?client_id=${CONFIG.imgur_client_id}&include=media%2Caccount`,
);
2021-10-06 17:47:10 +00:00
return JSON.parse(response.body);
}
export const fetchComments = async (galleryID: string): Promise<Comment[]> => {
2021-10-07 06:15:35 +00:00
/* eslint-disable max-len */
// https://api.imgur.com/comment/v1/comments?client_id=${CLIENT_ID}%5Bpost%5D=eq%3Ag1bk7CB&include=account%2Cadconfig&per_page=30&sort=best
2021-11-25 12:50:55 +00:00
const response = await get(
2021-10-07 06:15:35 +00:00
`https://api.imgur.com/comment/v1/comments?client_id=${CONFIG.imgur_client_id}&filter%5Bpost%5D=eq%3A${galleryID}&include=account%2Cadconfig&per_page=30&sort=best`,
);
return JSON.parse(response.body).data;
2021-10-07 06:15:35 +00:00
/* eslint-enable max-len */
}
2021-10-08 07:38:43 +00:00
export const fetchUserInfo = async (userID: string): Promise<UserResult> => {
// https://api.imgur.com/account/v1/accounts/hughjaniss?client_id=${CLIENT_ID}
2021-11-25 12:50:55 +00:00
const response = await get(
2021-10-08 07:38:43 +00:00
`https://api.imgur.com/account/v1/accounts/${userID.toLowerCase()}?client_id=${CONFIG.imgur_client_id}&include=`,
);
return JSON.parse(response.body);
}
2021-10-08 06:45:56 +00:00
export const fetchUserPosts = async (userID: string, sort: Sorting = 'newest'): Promise<Post[]> => {
2021-10-07 17:29:02 +00:00
/* eslint-disable max-len */
// https://api.imgur.com/3/account/mombotnumber5/submissions/0/newest?album_previews=1&client_id=${CLIENT_ID}
2021-11-25 12:50:55 +00:00
const response = await get(
2021-10-08 07:38:43 +00:00
`https://api.imgur.com/3/account/${userID.toLowerCase()}/submissions/0/${sort}?album_previews=1&client_id=${CONFIG.imgur_client_id}`,
2021-10-08 06:45:56 +00:00
);
return JSON.parse(response.body).data;
/* eslint-enable max-len */
}
export const fetchTagPosts = async (tagID: string, sort: Sorting = 'viral'): Promise<TagResult> => {
/* eslint-disable max-len */
// https://api.imgur.com/3/account/mombotnumber5/submissions/0/newest?album_previews=1&client_id=${CLIENT_ID}
2021-11-25 12:50:55 +00:00
const response = await get(
2021-10-08 06:45:56 +00:00
`https://api.imgur.com/3/gallery/t/${tagID.toLowerCase()}/${sort}/week/0?client_id=${CONFIG.imgur_client_id}`,
2021-10-07 17:29:02 +00:00
);
return JSON.parse(response.body).data;
/* eslint-enable max-len */
}
export const fetchGallery = async (galleryID: string): Promise<Gallery> => {
// https://imgur.com/gallery/g1bk7CB
2021-11-25 12:50:55 +00:00
const response = await get(`https://imgur.com/gallery/${galleryID}`);
const $ = cheerio.load(response.body);
const postDataScript = $('head script:first-of-type').html();
if (!postDataScript) {
throw new Error('Could not find gallery data');
}
const postDataMatches = postDataScript.match(GALLERY_JSON_REGEX);
if (!postDataMatches || postDataMatches.length < 2) {
throw new Error('Could not parse gallery data');
}
2021-10-07 17:21:04 +00:00
const body = postDataMatches[1].replace(/\\'/g, "'");
return JSON.parse(JSON.parse(body));
};
export const fetchMedia = async (filename: string): Promise<Response<string>> =>
2021-11-25 12:50:55 +00:00
await get(`https://i.imgur.com/${filename}`);