-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch.js
111 lines (88 loc) · 2.93 KB
/
twitch.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
const apiURL = 'https://wind-bow.glitch.me/twitch-api'
const usersList = ['ESL_SC2', 'OgamingSC2', 'cretetion', 'freecodecamp', 'storbeck', 'habathcx', 'RobotCaleb', 'noobs2ninjas']
let usersData = []
let streamsData = []
const usersUrls = usersList.map(user => apiURL + '/users/' + user)
const streamsUrls = usersList.map(user => apiURL + '/streams/' + user)
function getUsers() {
Promise.all(usersUrls.map(url =>
fetch(url).then(resp => resp.text())
)).then(texts => {
texts.map(text => {
usersData.push(JSON.parse(text))
})
console.log(usersData)
getStreams()
})
}
function getStreams() {
Promise.all(streamsUrls.map(url =>
fetch(url).then(resp => resp.text())
)).then(texts => {
texts.map(text => {
streamsData.push(JSON.parse(text))
})
console.log(streamsData)
buildUsersAndStreams()
})
}
function buildUsersAndStreams() {
for (const index in streamsData) {
const s = streamsData[index]
const u = usersData[index]
const isLive = s.stream ? true : false
const li = document.createElement('li')
li.classList.add('stream')
if (isLive) li.classList.add('live')
const icon = renderIcon(s, u)
const name = renderName(s, u)
const status = renderStatus(s, u)
li.appendChild(icon)
li.appendChild(name)
li.appendChild(status)
renderUsersAndStreams(li)
}
}
function renderUsersAndStreams(li) {
const ul = document.querySelector('ul.streams')
ul.appendChild(li)
}
function renderIcon(stream, user) {
const icon = document.createElement('div')
icon.classList.add('icon')
const iconImage = document.createElement('img')
iconImage.src = user.logo ? user.logo : 'https://blog.roblox.com/wp-content/uploads/2016/12/Twitch-Icon.png'
iconImage.alt = user.display_name
icon.appendChild(iconImage)
return icon
}
function renderName(stream, user) {
const name = document.createElement('div')
name.classList.add('name')
const nameLink = document.createElement('a')
nameLink.href = 'https://www.twitch.tv/' + user.name
nameLink.appendChild(document.createTextNode(user.display_name))
name.appendChild(nameLink)
return name
}
function renderStatus(stream, user) {
const status = document.createElement('div')
status.classList.add('status')
const statusInner = document.createElement('div')
statusInner.appendChild(document.createTextNode('Streaming '))
if (stream.stream) {
const game = document.createElement('span')
game.classList.add('game')
game.appendChild(document.createTextNode(stream.stream.game))
const gameStatus = document.createElement('span')
gameStatus.classList.add('game-status')
gameStatus.appendChild(document.createTextNode(stream.stream.channel.status))
statusInner.appendChild(game)
status.appendChild(statusInner)
status.appendChild(gameStatus)
} else {
status.appendChild(document.createTextNode('offline'))
}
return status
}
getUsers()