Skip to content

Instantly share code, notes, and snippets.

@xmmti
Last active June 10, 2021 05:37
Show Gist options
  • Save xmmti/81cb37a96585aac87d12dc02ad0b62b4 to your computer and use it in GitHub Desktop.
Save xmmti/81cb37a96585aac87d12dc02ad0b62b4 to your computer and use it in GitHub Desktop.
Rule34.paheal.net- Endpoint - Google Apps Script

Full Code

// Google Apps Script Lib: Cheerio 
let $ = (html)=> Cheerio.load(html);

// Set WebSite URL
let urlProtocol = "https://"
let urlBase = "rule34.paheal.net"

/* Utility API EndPoint Function */

const rule34 = (path) => path ? urlProtocol + urlBase + path : urlProtocol + urlBase + "/"
// == https://rule34.paheal.net/
const rule34API = (path) => path ? urlProtocol + urlBase + "/api/internal/" + path : urlProtocol + urlBase + "/api/"
// == https://rule34.paheal.net/api/internal/{{your text}} or https://rule34.paheal.net/api/

/* Utility Scraping Function */

/*
GET /api/internal/autocomplete?s={{Your key_word}}

Host: https://rule34.paheal.net
----Response will be JSON Object---
{some_thing: 97, name_of_something_else: 12}
*/
const jsonSearch = (keyword) => JSON.parse(getContent(rule34API("/autocomplete?s=") + keyword.replace(" ", "_")))

/*
GET /post/list/{{Tag}}/{{PageNumber}}

Host: https://rule34.paheal.net
----Response will be HTML Page---
{{HTML}}
*/
const getTagContent = (tag, page) => getContent(rule34("/post/list/") + tag + "/" + (page ? page : "1"))

function getContent(url, option){
 return UrlFetchApp.fetch(url, option).getContentText()
}

/* Http Requset, and grab data */

  /* Search for tag, and get the first result.
    Use Object.keys to get just names as Array
  */

  /*
  Pass HTML string to Cheerio.Load then set CSS Selector,
  in my case Selector will grabbing Array of HTMLElement (NodeList)
  so I use .each() Method.
  */
function test(){
     let tag = "Adventure_Time";
      let dataArray = [];
      $(getTagContent(tag))("#image-list [data-ext]").each(function (i, elem) {
        dataArray.push({
          imageExt: elem.attribs["data-ext"],
          // elem.firstChild.firstChild == [data-ext] > a.shm-thumb-link > img
          imagePrv: elem.firstChild.firstChild.attribs.src,
          imageInfo: {
            //
            size: elem.firstChild.firstChild.attribs.alt.split("\n")[1].split("//")[0],
            fileSize: elem.firstChild.firstChild.attribs.alt.split("\n")[1].split("//")[1]
          },
          imageLink: decodeURIComponent(elem.children[2].attribs.href)
        })
    });
    console.log(dataArray)
}

const doGet = function(e){
  if (e.parameter.type){
    switch(e.parameter.type){ 
     case 'search':
      let search = Object.keys( jsonSearch(e.parameter.word) )
      return ContentService.createTextOutput(JSON.stringify({status: "done", data: search})).setMimeType(ContentService.MimeType.JSON)
      break;
    case 'list':
      let tag = e.parameter.tag;
      let dataArray = [];
      $(getTagContent(tag))("#image-list [data-ext]").each(function (i, elem) {
        dataArray.push({
          imageExt: elem.attribs["data-ext"],
          // elem.firstChild.firstChild == [data-ext] > a.shm-thumb-link > img
          imagePrv: elem.firstChild.firstChild.attribs.src,
          imageInfo: {
            //
            size: elem.firstChild.firstChild.attribs.alt.split("\n")[1].split("//")[0],
            fileSize: elem.firstChild.firstChild.attribs.alt.split("\n")[1].split("//")[1]
          },
          imageLink: decodeURIComponent(elem.children[2].attribs.href)
        })
    });
      return ContentService.createTextOutput(JSON.stringify({status: "done", data: dataArray})).setMimeType(ContentService.MimeType.JSON)
      break;
    default: 
  return ContentService.createTextOutput(JSON.stringify({status: "error", data: "type is not true"})).setMimeType(ContentService.MimeType.JSON)
    }
  } else {
    return ContentService.createTextOutput(JSON.stringify({status: "error", data: null})).setMimeType(ContentService.MimeType.JSON)
  }
}

Library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment