This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HsCalendar.js
140 lines (125 loc) · 3.64 KB
/
HsCalendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const axios = require('axios')
const cheerio = require('cheerio')
const moment = require('moment')
/** HsCalendar builds an JS Object from the HorribleSubs release schedule
* @param {boolean} debug - Debug mode. Default is false
*/
class HsCalendar {
constructor (debug) {
this.debug = debug || false
this._baseURL = 'https://horriblesubs.info/release-schedule/'
this._axios = axios.create({
baseURL: this._baseURL,
timeout: 30000
})
}
/**
* Get the schedule for the wanted day. Default is today.
*
* @param {string} day
* @returns {Promise}
*/
getDay (day) {
return this._axios.get().then(
(res) => {
let $ = cheerio.load(res.data)
let week = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
let animes = []
// Selected day (numeric)
let sDay = week.indexOf(day.toLowerCase())
let schedule = $('.schedule-today-table').toArray()
$(schedule[sDay]).children().children().children().children().toArray().map((e) => {
let anime = {
'name': $(e).text(),
'slug': $(e).attr('href').replace('/shows/', '')
}
animes.push(anime)
})
return animes
},
(error) => {
console.error('Error getting source!')
if (this.debug) { console.log(error) }
}
)
}
/**
* Get the schedule for the week.
*
* @returns {Promise}
*/
getWeek () {
return this._axios.get().then(
(res) => {
let $ = cheerio.load(res.data)
let week = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
let animes = []
let schedule = $('.schedule-today-table').toArray()
// TODO: skip last index
$(schedule).children().children().children().children().toArray().map((e, index) => {
if (index !== 8) {
animes.push({
'name': $(e).text(),
'slug': $(e).attr('href').replace('/shows/', '')
})
}
})
return animes
},
(error) => {
console.error('Error getting source!')
if (this.debug) { console.log(error) }
}
)
}
/**
* Get the schedule for the week with the time of the day
* extract from testing the method:
*
[{
"day": "Monday",
"animes": [{
"time": "2018-03-26T08:15:00Z",
"slug": "/shows/micchiri-neko",
"title": "Micchiri Neko"
}]
}]
*
* @returns {Promise}
*/
getWeekWithTime() {
return this._axios.get()
.then(res => {
let $ = cheerio.load(res.data)
let days = $(".entry-content h2.weekday").toArray().slice(0, -1)
let tables = $(".entry-content table").toArray().slice(0, -1)
return days.map(el => $(el).text())
.map((day, index) => {
let table = $(tables[index])
let titles = table.find('.schedule-page-show').toArray()
let times = table.find('.schedule-time').toArray()
let animes = titles.map(el => $(el))
.map((el, index) => {
let title = el.text()
let link = el.find('a')
let slug = link && link.attr('href')
let time = $(times[index]).text()
let utcTime = moment.utc(`${day} ${time}`, "dddd HH:mm").add(7, 'hour').format()
return {
time: utcTime,
slug,
title
}
})
return {
day,
animes
}
})
}).catch(error => {
console.error('Error getting source!')
if (this.debug) { console.log(error) }
})
}
}
module.exports = HsCalendar