Add Twitter and Reddit loaders
parent
b5745e1be5
commit
94ee25e197
|
|
@ -0,0 +1,37 @@
|
|||
import ILoader from "./ILoader"
|
||||
import { SzuruPost, SzuruTag } from "../SzuruTypes";
|
||||
|
||||
export default class Reddit implements ILoader {
|
||||
canImport(location: Location): boolean {
|
||||
return (
|
||||
location.host == "reddit.com" ||
|
||||
location.host == "www.reddit.com" ||
|
||||
location.host == "old.reddit.com"
|
||||
);
|
||||
}
|
||||
|
||||
async grabPost(dom: Document): Promise<SzuruPost | null> {
|
||||
let post = new SzuruPost();
|
||||
post.source = dom.location.href;
|
||||
|
||||
// Set image url
|
||||
// For old.reddit.com
|
||||
const oldLinkElements = dom.querySelectorAll("a.title");
|
||||
if (oldLinkElements.length > 0) {
|
||||
post.imageUrl = (oldLinkElements[0] as HTMLAnchorElement).href;
|
||||
}
|
||||
|
||||
// For new reddit
|
||||
const newLinkElements = dom.querySelectorAll("div[data-test-id='post-content'] > div > a");
|
||||
if (newLinkElements.length > 0) {
|
||||
post.imageUrl = (newLinkElements[0] as HTMLAnchorElement).href;
|
||||
}
|
||||
|
||||
// If post has enough filled fields to be considered a valid post
|
||||
if (post.imageUrl) {
|
||||
return post;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
import ILoader from "./ILoader"
|
||||
import { SzuruPost, SzuruTag } from "../SzuruTypes";
|
||||
|
||||
function getOriginalImage(imageUrl: string) {
|
||||
const x = imageUrl.replace(/:large|:med|:small|:orig$/, ""); // Remove `:large`, `:small` etc from url
|
||||
return x + ":orig"; // Add :orig to url (to get original image)
|
||||
}
|
||||
|
||||
export default class Twitter implements ILoader {
|
||||
canImport(location: Location): boolean {
|
||||
return location.host == "twitter.com";
|
||||
}
|
||||
|
||||
async grabPost(dom: Document): Promise<SzuruPost | null> {
|
||||
let post = new SzuruPost();
|
||||
post.source = dom.location.href;
|
||||
|
||||
// Set image url
|
||||
// When clicked on post (not fullscreen, with post text etc)
|
||||
const overlayElement = dom.querySelector("#permalink-overlay-dialog");
|
||||
if (overlayElement) {
|
||||
const cardImageElement = overlayElement.querySelector("div[data-element-context='platform_photo_card'] > img");
|
||||
if (cardImageElement) {
|
||||
post.imageUrl = getOriginalImage((cardImageElement as HTMLImageElement).src);
|
||||
}
|
||||
}
|
||||
|
||||
// When clicked on image (fullscreen)
|
||||
const mediaImageElements = dom.querySelectorAll("div.Gallery-media > img.media-image");
|
||||
if (mediaImageElements.length > 0) {
|
||||
post.imageUrl = getOriginalImage((mediaImageElements[0] as HTMLImageElement).src);
|
||||
}
|
||||
|
||||
// If post has enough filled fields to be considered a valid post
|
||||
if (post.imageUrl) {
|
||||
return post;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,3 +2,5 @@ export { default as Gelbooru } from "./Gelbooru";
|
|||
export { default as Danbooru } from "./Danbooru";
|
||||
export { default as Shimmie2 } from "./Shimmie2";
|
||||
export { default as Moebooru } from "./Moebooru";
|
||||
export { default as Reddit } from "./Reddit";
|
||||
export { default as Twitter } from "./Twitter"
|
||||
|
|
|
|||
Loading…
Reference in New Issue