mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2025-12-14 04:05:14 +00:00
First commit: /a/, /gallery/, images, gifv
This commit is contained in:
8
src/config.ts
Normal file
8
src/config.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export default {
|
||||
port: process.env.RIMGU_PORT || 8080,
|
||||
host: process.env.RIMGU_HOST || 'localhost',
|
||||
address: process.env.RIMGU_ADDRESS || '127.0.0.1',
|
||||
http_proxy: process.env.RIMGU_HTTP_PROXY || null,
|
||||
https_proxy: process.env.RIMGU_HTTPS_PROXY || null,
|
||||
imgur_client_id: process.env.RIMGU_IMGUR_CLIENT_ID || null,
|
||||
};
|
||||
68
src/fetchers.ts
Normal file
68
src/fetchers.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
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
|
||||
};
|
||||
|
||||
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`);
|
||||
return JSON.parse(response.body).data;
|
||||
}
|
||||
|
||||
export const fetchGallery = async (galleryID: string): Promise<Gallery> => {
|
||||
// https://imgur.com/gallery/g1bk7CB
|
||||
const response = await got(`https://imgur.com/gallery/${galleryID}`, { agent });
|
||||
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 postData = JSON.parse(JSON.parse(postDataMatches[1]));
|
||||
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 });
|
||||
45
src/handlers.ts
Normal file
45
src/handlers.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import Hapi = require('@hapi/hapi');
|
||||
import '@hapi/vision';
|
||||
import { fetchAlbumURL, fetchComments, fetchGallery, fetchMedia } from './fetchers';
|
||||
import * as util from './util';
|
||||
|
||||
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 url = await fetchAlbumURL(request.params.albumID);
|
||||
return h.view('album', {
|
||||
url,
|
||||
util,
|
||||
});
|
||||
};
|
||||
|
||||
export const handleUser = (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
|
||||
// https://imgur.com/user/MomBotNumber5
|
||||
throw new Error('not implemented');
|
||||
};
|
||||
|
||||
export const handleTag = (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
|
||||
// https://imgur.com/t/funny
|
||||
throw new Error('not implemented');
|
||||
};
|
||||
|
||||
export const handleGallery = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
|
||||
const galleryID = request.params.galleryID;
|
||||
const gallery = await fetchGallery(galleryID);
|
||||
const comments = await fetchComments(galleryID);
|
||||
return h.view('gallery', {
|
||||
...gallery,
|
||||
comments,
|
||||
util,
|
||||
});
|
||||
};
|
||||
74
src/index.ts
Normal file
74
src/index.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
'use strict';
|
||||
|
||||
import Hapi = require('@hapi/hapi');
|
||||
import Path = require('path');
|
||||
import { handleAlbum, handleGallery, handleMedia, handleTag, handleUser } from './handlers';
|
||||
|
||||
import CONFIG from './config';
|
||||
|
||||
const init = async () => {
|
||||
const server = Hapi.server({
|
||||
port: CONFIG.port,
|
||||
host: CONFIG.host,
|
||||
address: CONFIG.address,
|
||||
routes: {
|
||||
files: {
|
||||
relativeTo: Path.join(__dirname, 'static')
|
||||
}
|
||||
}
|
||||
});
|
||||
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)
|
||||
});
|
||||
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: '/gallery/{galleryID}',
|
||||
handler: handleGallery,
|
||||
});
|
||||
|
||||
await server.start();
|
||||
console.log('Server running on %s', server.info.uri);
|
||||
};
|
||||
|
||||
process.on('unhandledRejection', (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
init();
|
||||
77
src/types/index.d.ts
vendored
Normal file
77
src/types/index.d.ts
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
interface Account {
|
||||
id: number;
|
||||
username: string;
|
||||
avatar_url: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
interface Gallery {
|
||||
id: string;
|
||||
title: string;
|
||||
account: Account;
|
||||
media: Media[];
|
||||
tags: Tag[];
|
||||
cover: Media;
|
||||
}
|
||||
|
||||
type MediaMimeType = 'image/jpeg' | 'image/png' | 'image/gif';
|
||||
type MediaType = 'image';
|
||||
type MediaExt = 'jpeg' | 'png' | 'gif';
|
||||
|
||||
interface Tag {
|
||||
tag: string;
|
||||
display: string;
|
||||
background_id: string;
|
||||
accent: string;
|
||||
is_promoted: boolean;
|
||||
}
|
||||
|
||||
interface Media {
|
||||
id: string;
|
||||
account_id: number;
|
||||
mime_type: MediaMimeType;
|
||||
type: MediaType;
|
||||
name: string;
|
||||
basename: string;
|
||||
url: string;
|
||||
ext: MediaExt;
|
||||
width: number;
|
||||
height: number;
|
||||
size: number;
|
||||
metadata: {
|
||||
title: string;
|
||||
description: string;
|
||||
is_animated: boolean;
|
||||
is_looping: boolean;
|
||||
duration: number;
|
||||
has_sound: boolean;
|
||||
},
|
||||
created_at: string;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
type MediaPlatform = 'ios' | 'android' | 'api' | 'web';
|
||||
interface Comment {
|
||||
id: number;
|
||||
parent_id: number;
|
||||
comment: string;
|
||||
account_id: number;
|
||||
post_id: string;
|
||||
upvote_count: number;
|
||||
downvote_count: number;
|
||||
point_count: number;
|
||||
vote: null; // ?
|
||||
platform_id: number;
|
||||
platform: MediaPlatform;
|
||||
created_at: string;
|
||||
updated_at: "2021-10-01T00:08:51Z",
|
||||
deleted_at: null,
|
||||
next: null; //?
|
||||
comments: Comment[];
|
||||
account: {
|
||||
id: number;
|
||||
username: string;
|
||||
avatar: string;
|
||||
}
|
||||
}
|
||||
|
||||
11
src/util.ts
Normal file
11
src/util.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const proxyURL = (url: string): string =>
|
||||
url.replace(/^https?:\/\/[^.]*\.imgur.com\//, '/');
|
||||
|
||||
export const linkify = (content: string) =>
|
||||
content.replace(
|
||||
/https?:\/\/[^.]*\.imgur.com\/([\/_a-zA-Z0-9-]+)\.gifv/g,
|
||||
'<video src="/$1.mp4" class="commentVideo commentObject" loop="" autoplay=""></video>'
|
||||
).replace(
|
||||
/https?:\/\/[^.]*\.imgur.com\/([\/_a-zA-Z0-9-]+\.[a-z0-9A-Z]{2,6})/g,
|
||||
'<a href="/$1" target="_blank"><img class="commentImage commentObject" src="/$1" loading="lazy" /></a>'
|
||||
);
|
||||
Reference in New Issue
Block a user