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

20
pages/album.go Normal file
View File

@@ -0,0 +1,20 @@
package pages
import (
"codeberg.org/video-prize-ranch/go-rimgu/api"
"github.com/gofiber/fiber/v2"
)
func HandleAlbum(c *fiber.Ctx) error {
// https://imgur.com/a/DfEsrAB
album, err := api.FetchAlbum(c.Params("albumID"))
if err != nil {
return err
}
return c.Render("gallery", fiber.Map{
"album": album,
})
}

53
pages/f.ts Normal file
View File

@@ -0,0 +1,53 @@
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));
};

55
pages/h.ts Normal file
View File

@@ -0,0 +1,55 @@
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,
});
};

29
pages/media.go Normal file
View File

@@ -0,0 +1,29 @@
package pages
import (
"io"
"net/http"
"github.com/gofiber/fiber/v2"
)
func HandleMedia(c *fiber.Ctx) error {
res, err := FetchMedia(c.Params("baseName") + "." + c.Params("extension"))
if err != nil {
return err
}
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
c.Set("Content-Type", res.Header.Get("Content-Type"));
c.Write(data)
return nil
}
func FetchMedia(filename string) (*http.Response, error) {
res, err := http.Get("https://i.imgur.com/" + filename)
return res, err
}