2024-09-11 03:16:56 +00:00
'use strict' ;
2024-09-11 03:16:53 +00:00
2024-09-11 03:16:57 +00:00
const config = require ( 'config' ) ;
2024-09-11 03:16:53 +00:00
const fetch = require ( 'node-fetch' ) ;
2024-09-11 03:16:57 +00:00
async function imgurImageApi ( post ) {
return fetch ( ` https://api.imgur.com/3/image/ ${ post . host . id } ` , {
headers : {
Authorization : ` Client-ID ${ config . methods . imgur . clientId } ` ,
} ,
} ) . then ( res => res . json ( ) ) . then ( ( res ) => {
if ( res . status !== 200 ) {
2024-09-11 03:16:57 +00:00
throw new Error ( ` Could not fetch info for imgur image ' ${ post . host . id } ': ${ res . data . error } ` ) ;
2024-09-11 03:16:57 +00:00
}
return {
album : null ,
items : [ {
id : res . data . id ,
url : res . data . animated ? res . data . mp4 : res . data . link ,
title : res . data . title ,
description : res . data . description ,
type : res . data . animated ? 'video/mp4' : res . data . type ,
datetime : new Date ( res . data . datetime * 1000 ) ,
original : res . data ,
} ] ,
} ;
} ) ;
}
2024-09-11 03:16:56 +00:00
2024-09-11 03:16:57 +00:00
async function imgurImage ( post ) {
2024-09-11 03:16:57 +00:00
return imgurImageApi ( post ) ;
/ *
* as of late 2019 , imgur requires log in to view albums and gallery images
2024-09-11 03:16:56 +00:00
const res = await fetch ( ` https://imgur.com/ ${ post . host . id } ` ) ;
const html = await res . text ( ) ;
2024-09-11 03:16:54 +00:00
2024-09-11 03:16:56 +00:00
if ( res . status !== 200 ) {
2024-09-11 03:16:57 +00:00
if ( config . methods . imgur . clientId ) {
console . log ( '\x1b[31m%s\x1b[0m' , ` Could not scrape info for imgur image ' ${ post . host . id } ' ( ${ res . statusText } ), trying API fallback ( ${ post . permalink } ) ` ) ;
return imgurImageApi ( post ) ;
}
2024-09-11 03:16:57 +00:00
throw new Error ( ` Could not scrape info for imgur image ' ${ post . host . id } ' ( ${ res . statusText } ), no API fallback configured ` ) ;
2024-09-11 03:16:56 +00:00
}
const dataString = html . replace ( /\s+/g , ' ' ) . match ( /}}, item:(.*)}; var PREBID_TIMEOUT/ ) [ 1 ] ;
const data = JSON . parse ( dataString ) ;
2024-09-11 03:16:56 +00:00
return {
2024-09-11 03:16:56 +00:00
album : null ,
items : [ {
id : data . hash ,
url : data . animated ? ` https://i.imgur.com/ ${ post . host . id } .mp4 ` : ` https://i.imgur.com/ ${ post . host . id } ${ data . ext } ` ,
title : data . title ,
description : data . description ,
type : data . animated ? 'video/mp4' : data . mimetype ,
datetime : new Date ( data . timestamp || data . datetime ) ,
} ] ,
} ;
2024-09-11 03:16:57 +00:00
* /
2024-09-11 03:16:56 +00:00
}
2024-09-11 03:16:53 +00:00
module . exports = imgurImage ;