repl.it
@Kognise/

Hackathons

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
index.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
const fetch = require('node-fetch')
const cheerio = require('cheerio')
const express = require('express')

const getHackathons = async () => {
  const res = await fetch('https://hackathons.hackclub.com/')
  const html = await res.text()
  const $ = cheerio.load(html)

  return $('#__next > main > div > div > a[itemscope]').toArray().map((item) => {
    const $$ = cheerio.load(item)

    return {
      name: $$('h3').text(),
      url: $$('span[itemprop=url]').text(),
      image: $$('img').attr('src'),
      dates: {
        start: new Date($$('span[itemprop=startDate]').text()),
        end: new Date($$('span[itemprop=endDate]').text())
      }
    }
  })
}

const app = express()

app.get('/', async (req, res) => {
  const hackathons = await getHackathons()

  const htmlList = hackathons.map((hackathon) => `
    <li>
      <a href='${hackathon.url}' target='_blank'>
        ${hackathon.name}
      </a>

      (from ${hackathon.dates.start.toDateString()} to ${hackathon.dates.end.toDateString()})
    </li>
  `).join('')

  res.status(200).send(`
    <!DOCTYPE html>
    <html>
      <head>
        <title>Hackathons</title>
        <link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/kognise/[email protected]/dist/dark.min.css'>
      </head>
      <body>
        <h1>Hack Club Hackathons</h1>
        <ul>${htmlList}</ul>
      </body>
    </html>
  `)
})

app.listen(() => console.log('> Server listening!'))
Fetching token
?