Initial album support

This commit is contained in:
video-prize-ranch
2022-01-17 15:23:04 -05:00
parent 0c5f9bc6b5
commit 579cbc84c6
26 changed files with 1104 additions and 9098 deletions

View File

@@ -1,118 +0,0 @@
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 = {
http: CONFIG.http_proxy
? new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: CONFIG.http_proxy,
})
: httpGlobalAgent,
https: CONFIG.https_proxy
? new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: CONFIG.https_proxy,
})
: httpsGlobalAgent
};
const get = async (url: string): Promise<Response<string>> => {
try {
return got(url, { agent });
}
catch (err) {
console.error(`Error getting ${url}`);
throw err;
}
};
export const fetchAlbumURL = async (albumID: string): Promise<string> => {
// https://imgur.com/a/DfEsrAB
const response = await get(`https://imgur.com/a/${albumID}`);
const $ = cheerio.load(response.body);
const url = $('head meta[property="og:image"]').attr('content')?.replace(/\/\?.*$/, '');
if (!url) {
throw new Error(`Could not read image url for album ${albumID}`);
}
return url;
};
export const fetchAlbum = async (albumID: string): Promise<Media[]> => {
// https://api.imgur.com/post/v1/albums/zk7mdKH?client_id=${CLIENT_ID}&include=media%2Caccount
const response = await get(
`https://api.imgur.com/post/v1/albums/${albumID}?client_id=${CONFIG.imgur_client_id}&include=media%2Caccount`,
);
return JSON.parse(response.body);
}
export const fetchComments = async (galleryID: string): Promise<Comment[]> => {
/* 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
const response = await get(
`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;
/* eslint-enable max-len */
}
export const fetchUserInfo = async (userID: string): Promise<UserResult> => {
// https://api.imgur.com/account/v1/accounts/hughjaniss?client_id=${CLIENT_ID}
const response = await get(
`https://api.imgur.com/account/v1/accounts/${userID.toLowerCase()}?client_id=${CONFIG.imgur_client_id}&include=`,
);
return JSON.parse(response.body);
}
export const fetchUserPosts = async (userID: string, sort: Sorting = 'newest'): Promise<Post[]> => {
/* eslint-disable max-len */
// https://api.imgur.com/3/account/mombotnumber5/submissions/0/newest?album_previews=1&client_id=${CLIENT_ID}
const response = await get(
`https://api.imgur.com/3/account/${userID.toLowerCase()}/submissions/0/${sort}?album_previews=1&client_id=${CONFIG.imgur_client_id}`,
);
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}
const response = await get(
`https://api.imgur.com/3/gallery/t/${tagID.toLowerCase()}/${sort}/week/0?client_id=${CONFIG.imgur_client_id}`,
);
return JSON.parse(response.body).data;
/* eslint-enable max-len */
}
export const fetchGallery = async (galleryID: string): Promise<Gallery> => {
// https://imgur.com/gallery/g1bk7CB
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');
}
const body = postDataMatches[1].replace(/\\'/g, "'");
return JSON.parse(JSON.parse(body));
};
export const fetchMedia = async (filename: string): Promise<Response<string>> =>
await get(`https://i.imgur.com/${filename}`);

View File

@@ -1,92 +0,0 @@
import Hapi = require('@hapi/hapi');
import '@hapi/vision';
import
{
fetchAlbum, fetchAlbumURL, fetchComments, fetchGallery, fetchMedia, fetchTagPosts, fetchUserInfo, fetchUserPosts
} from './fetchers';
import * as util from './util';
import CONFIG from './config';
export const handleMedia = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const {
baseName,
extension,
} = request.params;
const result = await fetchMedia(`${baseName}.${extension}`);
const response = h.response(result.rawBody)
.header('Content-Type', result.headers["content-type"] || `image/${extension}`);
return response;
};
export const handleAlbum = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/a/DfEsrAB
const albumID = request.params.albumID;
if (CONFIG.use_api) {
const album = await fetchAlbum(albumID);
return h.view('gallery', {
...album,
pageTitle: CONFIG.page_title,
util,
});
}
const url = await fetchAlbumURL(albumID);
return h.view('bare-album', {
url,
pageTitle: CONFIG.page_title,
util,
});
};
export const handleUser = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/user/MomBotNumber5
if (!CONFIG.use_api) {
return 'User page disabled. Rimgu administrator needs to enable API for this to work.';
}
const userID = request.params.userID;
const user = await fetchUserInfo(userID);
const posts = await fetchUserPosts(userID);
return h.view('posts', {
posts,
user,
pageTitle: CONFIG.page_title,
util,
});
};
export const handleUserCover = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const userID = request.params.userID;
const result = await fetchMedia(`/user/${userID}/cover?maxwidth=2560`);
const response = h.response(result.rawBody)
.header('Content-Type', result.headers["content-type"] || `image/jpeg`);
return response;
};
export const handleTag = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/t/funny
if (!CONFIG.use_api) {
return 'Tag page disabled. Rimgu administrator needs to enable API for this to work.';
}
const tagID = request.params.tagID;
const result = await fetchTagPosts(tagID);
return h.view('posts', {
posts: result.items,
pageTitle: CONFIG.page_title,
tag: result,
util,
});
};
export const handleGallery = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const galleryID = request.params.galleryID;
const gallery = await fetchGallery(galleryID);
const comments = CONFIG.use_api
? await fetchComments(galleryID)
: null;
return h.view('gallery', {
...gallery,
comments,
pageTitle: CONFIG.page_title,
util,
});
};

View File

@@ -1,96 +0,0 @@
import Hapi = require('@hapi/hapi');
/* eslint-disable @typescript-eslint/no-var-requires */
const Exiting = require('exiting');
import Path = require('path');
import { handleAlbum, handleGallery, handleMedia, handleTag, handleUser, handleUserCover } from './handlers';
import CONFIG from './config';
const server = Hapi.server({
port: CONFIG.port,
host: CONFIG.host,
address: CONFIG.address,
routes: {
files: {
relativeTo: Path.join(__dirname, 'static')
}
},
debug: CONFIG.debug ? {
request: ['error']
} : {},
});
server.events.on('stop', () => {
console.log('Server stopped.');
});
const manager = Exiting.createManager(server);
const init = async () => {
await server.register(require('@hapi/vision'));
await server.register(require('@hapi/inert'));
server.route({
method: 'GET',
path: '/css/{param*}',
handler: ({
directory: {
path: Path.join(__dirname, 'static/css')
}
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
});
server.views({
engines: {
pug: require('pug')
},
relativeTo: __dirname,
path: 'templates',
});
server.route({
method: 'GET',
path: '/{baseName}.{extension}',
handler: handleMedia,
});
server.route({
method: 'GET',
path: '/a/{albumID?}',
handler: handleAlbum,
});
server.route({
method: 'GET',
path: '/t/{tagID?}',
handler: handleTag,
});
server.route({
method: 'GET',
path: '/user/{userID?}',
handler: handleUser,
});
server.route({
method: 'GET',
path: '/user/{userID}/cover',
handler: handleUserCover,
});
server.route({
method: 'GET',
path: '/gallery/{galleryID}',
handler: handleGallery,
});
await manager.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.error(err);
process.exit(1);
});
if (!CONFIG.use_api) {
console.log('Running without imgur client ID; certain views and functionality missing.');
}
else if (!CONFIG.imgur_client_id) {
console.error('imgur_client_id missing. Configure it via RIMGU_IMGUR_CLIENT_ID or set RIMGU_USE_API=false');
process.exit(1);
}
init();