{"size": 1880, "ext": "js", "max_stars_repo_path": "app/Controllers/Http/Api/V1/Steam/Game/TrendingController.js", "max_stars_repo_name": "InGaming/SteamHub.API", "max_stars_repo_stars_event_min_datetime": "2019-01-03T10:22:19.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-03T10:22:19.000Z", "max_issues_repo_path": "app/Controllers/Http/Api/V1/Steam/Game/TrendingController.js", "max_issues_repo_name": "InGaming/SteamHub.API", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/Controllers/Http/Api/V1/Steam/Game/TrendingController.js", "max_forks_repo_name": "InGaming/SteamHub.API", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.8412698413, "max_line_length": 129, "alphanum_fraction": 0.5031914894}
'use strict'
const cheerio = require('cheerio')
const requested = require('request')
const Trending = use('App/Models/Api/V1/Steam/Game/Trending')
const Env = use('Env')
const _ = require('lodash')

class TrendingController {
  store ({ request }) {
    const key = request.get().key
    if (key !== Env.get('API_KEY')) {
      return 'error'
    }

    // Use abstract equality == for "is number" test
    function isEven (n) {
      return n == parseFloat(n)? !(n%2) : void 0
    }

    // Use strict equality === for "is number" test
    function isEvenStrict (n) {
      return n === parseFloat(n)? !(n%2) : void 0
    }

    setTimeout(function timer () {
      requested('https://store.steampowered.com/stats/Steam-Game-and-Player-Statistics?l=chinese', async function (error, body) {
        if (!error) {
          let now = []
          let total = []
          let title = []
          let appids = []
          const $ = cheerio.load(body.body)
          $('.player_count_row td span').each((idx, element) => {
            let $element = $(element)
            if (isEvenStrict(idx)) {
              now.push({
                now: $element.text()
              })
            } else {
              total.push({
                total: $element.text()
              })
            }
          })
          $('.gameLink').each((idx, element) => {
            let $element = $(element)
            let removeURL = $element.attr('href').replace(/^https:\/\/store\.steampowered\.com\/app\//g, '')
            let id = removeURL.match(/^[0-9]*/g)
            appids.push({
              appid: id.toString()
            })
            title.push({
              title: $element.text()
            })
          })
          Trending.create({'Data': JSON.stringify(_.merge(appids, title, total, now))})
        }
      })
    }, 500)
  }
}

module.exports = TrendingController