wolfyflutter

62. lahelu.com [search] [meme]

Sep 21st, 2025 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.53 KB | Source Code | 0 0
  1. /*
  2.   base : https://lahelu.com/
  3.   req by : bang chandra
  4.   note : 2 fungsi, search by query, dan nampilin rekomendasi
  5.          pakai cursor buat next page
  6.   node : v24.5.0
  7.   last update : 23 september 2025
  8.   by : wolep
  9. */
  10.  
  11. const lahelu = {
  12.     get field() {
  13.         return {
  14.             FOR_YOU: 5,
  15.             FRESH: 6,
  16.             VIRAL: 7
  17.         }
  18.     },
  19.  
  20.     _mintaJson: async function (pathName, urlSearchParams) {
  21.         const url = new URL('https://lahelu.com')
  22.         url.pathname = pathName
  23.         url.search = new URLSearchParams(urlSearchParams)
  24.         const response = await fetch(url)
  25.         if (!response.ok) throw Error(`${response.status} ${response.statusText} on ${pathName}. payload from server:\n${await response.text() || '(kosong)'}`)
  26.         const json = await response.json()
  27.         return json
  28.     },
  29.  
  30.     _serialize: function (json, onWhat, cursor) {
  31.         const result = json.postInfos.map(p => {
  32.             return {
  33.                 title: p.title || `(no title)`,
  34.                 hashtag: p.hashtags.join(', ') || `(no hastag)`,
  35.                 media: p?.content?.[0] || null
  36.             }
  37.         })
  38.         if (!result.length) throw Error(`tidak ada data ${onWhat} di index cursor ${cursor}`)
  39.         return result
  40.     },
  41.  
  42.     search: async function (query, cursor = 0) {
  43.         // validate string
  44.         if (typeof (query) != 'string' || !query.trim().length) throw Error(`query harus string dan gak boleh kosong ya!`)
  45.  
  46.         const json = await this._mintaJson('/api/post/get-search', {
  47.             query, cursor
  48.         })
  49.         return this._serialize(json, 'search', cursor)
  50.     },
  51.  
  52.     recommendations: async function (field = this.field.FOR_YOU, cursor = 0) {
  53.         // validate field
  54.         if (!Object.values(this.field).includes(field)) throw Error(`invalid field value`)
  55.  
  56.         const json = await this._mintaJson('/api/post/get-recommendations', {
  57.             field, cursor
  58.         })
  59.         return this._serialize(json, 'rekomendasi', cursor)
  60.     }
  61. }
  62.  
  63. // cara pakai
  64. lahelu.search('meme', 0)
  65.     .then(console.log)
  66.     .catch(e => console.log(e.message))
  67.  
  68. /* output
  69. [
  70.   {
  71.     title: 'Meme halal hari ini',
  72.     hashtag: 'relate',
  73.     media: {
  74.       type: 0,
  75.       value: 'https://cache.lahelu.com/19e6ece1-6166-4a66-8395-885b18ec8f75.webp',
  76.       width: 720,
  77.       height: 398
  78.     }
  79.   },,... (dan objek lainnya)
  80. ]
  81. */
  82.  
  83. /*
  84. atauu..
  85. lahelu.recommendations(lahelu.field.FOR_YOU, 0)
  86.     .then(console.log)
  87.     .catch(e => console.log(e.message))
  88. */
Tags: search meme
Advertisement
Add Comment
Please, Sign In to add comment