Multi-album support

This commit is contained in:
3nprob
2021-10-07 02:47:10 +09:00
parent 5fdf892e1f
commit aba74e1abc
5 changed files with 77 additions and 34 deletions

View File

@@ -31,9 +31,26 @@ const agent = {
: httpsGlobalAgent
};
export const fetchAlbumURL = async (albumID: string): Promise<string> => {
// https://imgur.com/a/DfEsrAB
const response = await got(`https://imgur.com/a/${albumID}`, { agent });
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');
}
return url;
};
export const fetchAlbum = async (albumID: string): Promise<Comment[]> => {
// https://api.imgur.com/post/v1/albums/zk7mdKH?client_id=${CLIENT_ID}&include=media%2Caccount
const response = await got(`https://api.imgur.com/post/v1/albums/${albumID}?client_id=${CONFIG.imgur_client_id}&include=media%2Caccount`, { agent });
return JSON.parse(response.body);
}
export const fetchComments = async (galleryID: string): Promise<Comment[]> => {
// 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 got(`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`);
const response = await got(`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`, { agent });
return JSON.parse(response.body).data;
}
@@ -53,16 +70,5 @@ export const fetchGallery = async (galleryID: string): Promise<Gallery> => {
return postData;
};
export const fetchAlbumURL = async (albumID: string): Promise<string> => {
// https://imgur.com/a/DfEsrAB
const response = await got(`https://imgur.com/a/${albumID}`, { agent });
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');
}
return url;
};
export const fetchMedia = async (filename: string): Promise<Response<string>> =>
await got(`https://i.imgur.com/${filename}`, { agent });

View File

@@ -1,6 +1,6 @@
import Hapi = require('@hapi/hapi');
import '@hapi/vision';
import { fetchAlbumURL, fetchComments, fetchGallery, fetchMedia } from './fetchers';
import { fetchAlbum, fetchAlbumURL, fetchComments, fetchGallery, fetchMedia } from './fetchers';
import * as util from './util';
import CONFIG from './config';
@@ -18,12 +18,23 @@ export const handleMedia = async (request: Hapi.Request, h: Hapi.ResponseToolkit
export const handleAlbum = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/a/DfEsrAB
const url = await fetchAlbumURL(request.params.albumID);
return h.view('album', {
url,
title: CONFIG.page_title,
util,
});
const albumID = request.params.albumID;
if (CONFIG.disable_comments) {
const url = await fetchAlbumURL(albumID);
return h.view('bare-album', {
url,
pageTitle: CONFIG.page_title,
util,
});
} else {
const album = await fetchAlbum(albumID);
return h.view('gallery', {
...album,
pageTitle: CONFIG.page_title,
util,
});
}
};
export const handleUser = (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
@@ -45,7 +56,7 @@ export const handleGallery = async (request: Hapi.Request, h: Hapi.ResponseToolk
return h.view('gallery', {
...gallery,
comments,
title: CONFIG.page_title,
pageTitle: CONFIG.page_title,
util,
});
};