rogeraabbccdd / Linebot-Deals

A line bot to get deals information of steam app.
10 stars 2 forks source link

幫你補一下搜尋功能 #1

Closed r3850355 closed 5 years ago

r3850355 commented 5 years ago

讓你可以拉出圖片組進flex message裡面

要我幫你組好flex message也是可以 XD

index.js

const search = require('./search')

search('half').then(res => {
  console.log(res)
})

search.js

const request = require('request')
const cheerio = require('cheerio')
const _ = require('lodash')

const url = 'https://isthereanydeal.com/search'

module.exports = function search (text) { 
  return new Promise ((reslove, reject) => {
    let searchList = []
    request(`${url}/?q=${text}`, (err, res, body) => {
      const $ = cheerio.load(body)
      let gameName = []
      let gameImage = []

      // search for name
      $('a.card__title').each( function (idx, el) {
        gameName.push($(this).text())
      })

      // search for image
      $('.card__img div').each( function (idx, el) {
        gameImage.push($(this).attr('data-img-sm'))
      })

      // make new array
      _.each(gameName, (el, idx) => {
        searchList.push({
          name: el,
          image: gameImage[idx]
        })
      })

      reslove(searchList)
    })
  })
}
r3850355 commented 5 years ago

忘了補輸出 有的他本來就沒圖片

// 輸入 half
[
  { name: 'Half - Horrorgame', image: undefined },
  {
    name: 'Half dead',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/434730/header_292x136.jpg'
  },
  {
    name: 'The Other Half',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/917920/header_292x136.jpg'
  },
  {
    name: 'Half-Life',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/70/header_292x136.jpg'
  },
  { name: 'HALF DEAD COMPLETE BUNDLE', image: undefined },
  {
    name: 'Half-Life: Source',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/280/header_292x136.jpg'
  },
  {
    name: 'Half-Life 1: Source',
    image: 'https://steamcdn-a.akamaihd.net/steam/subs/38/header_292x136.jpg'
  },
  {
    name: 'HALF DEAD 2',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/914260/header_292x136.jpg'
  },
  {
    name: 'Half Past Disaster Soundtrack',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/689630/header_292x136.jpg'
  },
  {
    name: 'Half-Life Soundtrack',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/323130/header_292x136.jpg'
  },
  {
    name: 'Half-Life 2: Update',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/290930/header_292x136.jpg'
  },
  {
    name: 'Mad Men: The Better Half',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/637278/header_292x136.jpg'
  },
  {
    name: 'Half Past Impossible',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/758700/header_292x136.jpg'
  },
  {
    name: 'Half-Life: Opposing Force',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/50/header_292x136.jpg'
  },
  {
    name: 'Half Past Disaster',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/683630/header_292x136.jpg'
  },
  {
    name: 'Half-Life 2: Deathmatch',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/320/header_292x136.jpg'
  },
  {
    name: 'Half-Life Deathmatch: Source',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/360/header_292x136.jpg'
  },
  {
    name: 'Crossout - Third Half Pack',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/878630/header_292x136.jpg'
  },
  { name: 'Half-Life 1 Anthology', image: undefined },
  { name: 'Half Life Complete', image: undefined },
  {
    name: 'Half-Rats: Parasomnia',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/638360/header_292x136.jpg'
  },
  {
    name: 'Half-Life 2: DownFall',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/587650/header_292x136.jpg'
  },
  {
    name: 'Fear Half Factor',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/805690/header_292x136.jpg'
  },
  { name: 'Half-Face Games Bundle', image: undefined },
  {
    name: 'Half-Rats: Parasomnia - OST',
    image: 'https://steamcdn-a.akamaihd.net/steam/apps/748070/header_292x136.jpg'
  }
]
rogeraabbccdd commented 5 years ago

我觀察過好像只有上 steam 的有圖片,應該不需要用爬蟲,抓 API 裡的 steam ID 就行了

r3850355 commented 5 years ago

主要是怕有人遊戲名稱空格不正確 或是不知道全名可以打一半用查的 XD

rogeraabbccdd commented 5 years ago

搜尋功能的話 itad 的 API 也有,所以如果打出錯誤的遊戲時機器人會找第一筆問 LINE_tQBfISECCp

rogeraabbccdd commented 5 years ago

不然就是多顯示幾筆囉

r3850355 commented 5 years ago

讚讚 像我就是遊戲記不起來全名的

rogeraabbccdd commented 5 years ago

已調整搜尋建議為 5 筆,https://github.com/rogeraabbccdd/Linebot-Deals/commit/7ec0a468fde3c7240aa8216cfdef6d5f3eebd007