Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplementation with new scraper in node.js #22

Merged
merged 7 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 48 additions & 35 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,61 @@ name: Docker

on:
push:
# Publish `main` as Docker `latest` image.
branches:
- main
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}

jobs:
# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push:

runs-on: ubuntu-20.04
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v4

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v1
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Setup Docker build
id: buildx
uses: docker/setup-buildx-action@v1

- name: Build and push backend
id: docker_build_backend
uses: docker/build-push-action@v2
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
context: ./lasvecka-python
file: ./lasvecka-python/prod.Dockerfile
push: true
tags: gudchs/lasvecka_backend:latest

- name: Build and push frontend
id: docker_build_frontend
uses: docker/build-push-action@v2
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: ./lasvecka-react
file: ./lasvecka-react/prod.Dockerfile
push: true
tags: gudchs/lasvecka_frontend:latest

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
context: ./lasvecka-node
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
23 changes: 6 additions & 17 deletions dev.docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
version: '2'
version: "3"

services:
backend:
container_name: lasvecka-python
app:
container_name: lasvecka-node
build:
context: ./lasvecka-python
dockerfile: dev.Dockerfile
ports:
- 5000:5000
volumes:
- ./backend:/lasvecka-python

frontend:
container_name: lasvecka-react
build:
context: ./lasvecka-react
dockerfile: dev.Dockerfile
stdin_open: true
context: ./lasvecka-node
dockerfile: Dockerfile
ports:
- 3000:3000
volumes:
- ./frontend:/usr/src/lasveckor/frontend
- ./data:/usr/src/app/data
19 changes: 19 additions & 0 deletions lasvecka-node/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:lts

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000
CMD [ "node", "index.js" ]
81 changes: 81 additions & 0 deletions lasvecka-node/calc_date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const moment = require('moment');
const fs = require('fs');
const scrape = require('./lasveckor_scraper.js');
var dateDict = {};

// check if data.json exists
if (!fs.existsSync('./data/data.json')) {
scrape().then((res) => {
dateDict = res
});
} else {
dateDict = JSON.parse(fs.readFileSync('./data/data.json'));

//check if it was updated after 1/7 this year and if 1/7 has occured this year
let updated = moment(dateDict["updated"]);
let firstOfJuly = moment().month(6).date(1);
if (updated.isBefore(firstOfJuly) && moment().isAfter(firstOfJuly)) {
scrape().then((res) => {
dateDict = res
});
}
}

function readDatePeriod(currDate) {
let soughtDate = "";
let diff = -1000;
for (let dat in dateDict) {
// if dat is updated, easter_start, easter_end or ord_cont then skip
if (dat === "updated" || dat === "easter_start" || dat === "easter_end" || dat === "ord_cont") {
continue;
}
let deltaT = moment(dat).diff(currDate, 'days');
if (deltaT === 0) {
return { date: dat, type: dateDict[dat] };
} else if (deltaT > diff && deltaT < 0) {
soughtDate = dat;
diff = deltaT;
}
}
return { date: soughtDate, type: dateDict[soughtDate] };
}

function handleEaster(easterStartDiff, easterEndDiff) {
if (easterStartDiff >= 0 && easterEndDiff <= 0) {
return "Självstudier";
} else if (easterEndDiff > 0) {
let weeks = Math.floor(easterEndDiff / 7);
return "Lv " + (weeks + 4);
}
}

function computeTime() {
// Find date where value is easter_start, easter_end and ord_cont in json file
let EASTER_START = dateDict["easter_start"]
let ORD_CONT = dateDict["ord_cont"]
let currentDate = moment();
let easterEndCheck = currentDate.diff(ORD_CONT, 'days');
let easterStartCheck = currentDate.diff(EASTER_START, 'days');
let { date: dat, type: typ } = readDatePeriod(currentDate);
if (typ === "exam_period") {
let deltaT = currentDate.diff(moment(dat), 'days');
if (deltaT > 7) {
return "Självstudier";
} else {
return "Tentavecka";
}
}
if (easterEndCheck >= 0 || easterStartCheck >= 0) {
return handleEaster(easterStartCheck, easterEndCheck);
} else {
let deltaT = currentDate.diff(moment(dat), 'days');
let weeks = Math.floor(deltaT / 7);
if (weeks > 7) {
return "Självstudier";
} else {
return "LV " + (weeks + 1);
}
}
}

module.exports = computeTime;
1 change: 1 addition & 0 deletions lasvecka-node/data/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"2023-08-28":"study_period","2023-10-21":"exam_period","2023-10-30":"study_period","2024-01-08":"exam_period","2024-01-15":"study_period","2024-03-09":"exam_period","2024-03-18":"study_period","easter_start":"2024-04-03","easter_end":"2024-04-05","ord_cont":"2024-04-08","2024-05-25":"exam_period","updated":"2023-11-06"}
91 changes: 91 additions & 0 deletions lasvecka-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const express = require('express');
const moment = require('moment');
const app = express();
const computeTime = require('./calc_date.js');
const port = process.env.PORT || 3000;

app.use(express.static('public'));

app.get('/', (req, res) => {
let studyweek = computeTime();
let studyweekNum = studyweek.replace('LV ', '').replace('Självstudier', 'S').replace('Tentavecka', 'T');
// eg. 2023-W45
let week = moment().format('YYYY-[W]WW');
let data = { studyweek, week, studyweekNum };
res.send(render(data));
});

app.get('/api', (req, res) => {
res.send(computeTime());
});

app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})

const render = (data) => {
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>läsvecka.nu | ${data.studyweek}</title>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" id="favicon">
<link rel="manifest" href="/site.webmanifest">
<meta name="description" content="läsvecka.nu ger dig aktuell läsvecka, utan några konstigheter!">
<meta property="og:type" content="website">
<meta property="og:url" content="https://lasvecka.nu/">
<meta property="og:title" content="läsvecka.nu | ${data.studyweek}">
<style>
html, body { height: 100%; background-color: #90c0de; overflow: hidden; }
time {
position: absolute;
top: 50%;
left: 0;
right: 0;
margin: -110px 0 0 0;
height: 220px;
text-align: center;
color: #1c7bb7;
font-family: Arial, serif;
font-size: 260px;
line-height: 227px;
font-weight: bold;
}
</style>
</head>
<body>
<time datetime="${data.week}">${data.studyweek}</time>
<script>
(function () {
document.head = document.head || document.getElementsByTagName('head')[0];
var canvas = document.createElement('canvas'),
img = document.createElement('img'),
oldLink = document.getElementById('favicon'),
link = oldLink.cloneNode(true),
week = '${data.studyweekNum}';
if (canvas.getContext) {
canvas.height = canvas.width = 16;
var ctx = canvas.getContext('2d'),
textWidth = ctx.measureText(week).width;
img.onload = function () {
ctx.drawImage(this, 0, 0);
ctx.font = 'bold 10px "helvetica", sans-serif';
ctx.fillStyle = '#066EB0';
ctx.fillText(week, (canvas.width / 2) - (textWidth / 2), 12);
link.href = canvas.toDataURL('image/png');
document.head.removeChild(oldLink);
document.head.appendChild(link);
};
img.src = 'faviconbkg.png';
}
})();

setTimeout(function () { location.reload(); }, 25886000);
</script>
</body>
</html>`;
return html.replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '').replace(/ {2,}/g, '');
}
51 changes: 51 additions & 0 deletions lasvecka-node/lasveckor_scraper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
const axios = require('axios');

async function scrape() {
console.log("Scraping data from student.chalmers.se")
let url = "https://www.student.chalmers.se/sp/academic_year_list"

const response = await axios.get(url, { responseType: "arraybuffer" })
let data = response.data.toString('latin1');

data = data.replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '');
data = data.match(/<table width="100%" border="0" cellspacing="0" cellpadding="3">(.*?)<\/table>/s)[0]

// some things to make it nicer
data = data.replace(/<\/tr>/g, '\n')
data = data.replace(/<tr align="left">/g, '')
data = data.replace(/<tr align="left" class="fade">/g, '')
data = data.replace(/<td>/g, '')
data = data.replace(/<\/td>/g, '')
data = data.split('\n')

let result = {}
for (let i = 0; i < data.length; i++) {
let line = data[i]
if (line.startsWith('Läsperiod')) {
let date = line.match(/(\d{4}-\d{2}-\d{2})/)[0]
result[date] = "study_period"
} else if (line.startsWith('Tentamensperiod')) {
let date = line.match(/(\d{4}-\d{2}-\d{2})/)[0]
result[date] = "exam_period"
} else if (line.startsWith("Omtentamensperiod påsk")) {
let date1 = line.match(/(\d{4}-\d{2}-\d{2})/)[0]
let date2 = line.match(/(\d{4}-\d{2}-\d{2})/g)[1]
result["easter_start"] = date1
result["easter_end"] = date2

var easter_start = new Date(date1)
// find first monday after easter
while (easter_start.getDay() != 1) {
easter_start.setDate(easter_start.getDate() + 1)
}
result["ord_cont"] = easter_start.toISOString().slice(0, 10)
}
}

result["updated"] = new Date().toISOString().slice(0, 10)
await fs.promises.writeFile('./data/data.json', JSON.stringify(result))
return result
}

module.exports = scrape;
Loading