Skip to content

Commit

Permalink
refactor part2
Browse files Browse the repository at this point in the history
  • Loading branch information
nils committed Dec 31, 2023
1 parent b245452 commit c0aa97c
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 271 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/jekyll.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
# documentation.

# Sample workflow for building and deploying a Jekyll site to GitHub Pages
name: Deploy Jekyll site to Pages
name: Deploy Jekyll site to Pages, un push and periodically rebuild to collect new data

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]

schedule:
# “At minute 13 past every 2nd hour.”
- cron: "13 */2 * * *"

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand Down Expand Up @@ -39,9 +43,15 @@ jobs:
ruby-version: '3.1' # Not needed with a .ruby-version file
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
cache-version: 0 # Increment this number if you need to re-download cached gems
- name: Setup Nodejs
uses: actions/setup-node@v3
with:
node-version: '18.x'
- name: Setup Pages
id: pages
uses: actions/configure-pages@v4
- name: Pull in all data from https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0/
run: ./motie-cache.sh
- name: Build with Jekyll
# Outputs to the './_site' directory by default
run: bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ _site
vendor
node_modules

moties/**/moties/
moties/
_data/moties/
87 changes: 32 additions & 55 deletions _includes/motie-year.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
<!doctype html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Moties met stemmingen van de Tweede Kamer {{ page.jaar }}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="/assets/css/styles.css">
<script>var moties = [];</script>
</head>
<body>

<div id="progressMoties" class="progress" role="progressbar" aria-label="Basic example" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="height: 1px">
<div class="progress-bar" style="width: 0%;"></div>
</div>

<div id="moties" class="container">

<div class="container">

<h1>Moties met stemmingen van de Tweede Kamer in {{ year }}</h1>

<p>Een overzicht van alle moties waarover gestemd is in de Tweede Kamer in {{ year }}.</p>

</div>
{% if site.data.moties[year] != null %}

{% assign motiesSorted = site.data.moties[year] | group_by_exp: "motie", "motie[1].Zaak[0].GestartOp | slice: 0,10" | sort: "name" | reverse %}

<div id="moties" class="container">
{% assign previousDate = "" %}
{% for motieDateGroup in motiesSorted %}
{% assign currentDate = motieDateGroup.name %}
<div id="date-{{ currentDate }}">
<div class="d-flex flex-nowrap sticky-top bg-body">
<div id="date-{{ currentDate }}" class="sticky-top">
<div class="d-flex flex-nowrap bg-body">
<div class="mx-1">
<a href="#date-{{ previousDate }}">{% include symbol-up1.html %}</a>
</div>
Expand All @@ -35,54 +33,33 @@
<a href="#">{% include symbol-down1.html %}</a>
</div>
</div>
{% for kv in motieDateGroup.items %}
{% assign currentId = kv[0] %}
{% assign motie = kv[1] %}
<script>moties.push({"date": "{{currentDate}}", "Id": "{{kv[0]}}"});</script>
{% endfor %}
</div>

{% for kv in motieDateGroup.items %}
{% assign currentId = kv[0] %}
<div motieId="{{ currentId }}" year="{{ year }}" class="motie-placeholder"></div>
{% endfor %}
{% assign previousDate = currentDate %}
{% endfor %}
</div>

{% endif %}

<footer>
<small>
<a href="https://opendata.tweedekamer.nl/documentatie/odata-api">data sources</a>
<a href="https://github.com/tkmoties/tkmoties.github.io">page sources</a>
</small>
</footer>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"/></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>

{% if site.data.moties[year] != null %}
<script src="/assets/js/load-moties.js"></script>
<script>
window.addEventListener('load', function () {
(async() => {
const dateSeparators = document.querySelectorAll('.date-separator');
dateSeparators[0].parentNode.childNodes[1].childNodes[1].href = '#';
for (var idx = 0; idx < dateSeparators.length - 1; idx += 1) {
dateSeparators[idx].parentNode.childNodes[5].childNodes[1].href = dateSeparators[idx+1].childNodes[1].href;
}

let motieCounter = 0;
await moties.forEach(motie => {
fetch(`http://localhost:4000/moties/{{ year }}/moties/${motie.Id}.html`)
.then(response => {
if (response.status != 200) {
console.log(response)
}
return response;
})
.then(response => response.text())
.then(response => {
document.querySelector(`#date-${motie.date}`)
.insertAdjacentHTML('beforeend', response);
motieCounter += 1;
document.querySelector('#progressMoties')
.childNodes[1].style.width = `${100 * motieCounter / moties.length}%`;

if (motieCounter === moties.length) {
document.querySelector('#progressMoties').hidden = true;
}
})
});
})();
fixDateseparators();
loadMoties();
});

</script>
</html>
{% endif %}
11 changes: 11 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Moties met stemmingen van de Tweede Kamer</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="/assets/css/styles.css">
</head>
{{ content }}
</html>
8 changes: 8 additions & 0 deletions _templates/year-index-front.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
layout: default
redirect_from: /
---

{% assign year = page.path | split: "/" | slice: 1 | append: "" | slice: 2,4 %}

{% include motie-year.html %}
1 change: 1 addition & 0 deletions _templates/year-index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
layout: default
---

{% assign year = page.path | split: "/" | slice: 1 | append: "" | slice: 2,4 %}
Expand Down
30 changes: 30 additions & 0 deletions assets/js/load-moties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
async function fixDateseparators() {
const dateSeparators = document.querySelectorAll('.date-separator');
dateSeparators[0].parentNode.childNodes[1].childNodes[1].href = '#';
for (var idx = 0; idx < dateSeparators.length - 1; idx += 1) {
dateSeparators[idx].parentNode.childNodes[5].childNodes[1].href = dateSeparators[idx+1].childNodes[1].href;
}
}

async function loadMoties() {
let motieCounter = 0;
let moties = [...document.querySelectorAll('.motie-placeholder')];
moties.forEach(elem => {
const year = elem.getAttribute('year');
const Id = elem.getAttribute('motieId');
fetch(`/moties/${year}/moties/${Id}.html`)
.then(response => {
if (response.status != 200) {
console.log(response)
}
return response;
})
.then(response => response.text())
.then(response => {
motieCounter += 1;
elem.insertAdjacentHTML('beforeend', response);
document.querySelector('#progressMoties').children[0]
.style.width = `${100 * motieCounter / moties.length}%`;
})
});
};
47 changes: 0 additions & 47 deletions get-last-api-timestamp.js

This file was deleted.

6 changes: 6 additions & 0 deletions motie-cache.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
echo "Set up dirs"

mkdir -p _data/moties/

for year in $(seq 2007 2024); do
mkdir -p moties/$year/moties;
cp _templates/year-index.html moties/$year/index.html;
done

cp _templates/year-index-front.html moties/2023/index.html;

echo "Start caching from https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0"

node motie-cache2.js
8 changes: 7 additions & 1 deletion motie-cache2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
- Create a page in moties/{year}/{id}.html
*/


var fs = require('fs');

const topVal = process.argv[2];
let top = ''
if (typeof topVal !== 'undefined') {
top = `&$top=${topVal}`;
}

const urlApi = 'https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0';

const motieQuery = `
Besluit?
&$orderby=ApiGewijzigdOp desc
${top}
&$count=true
&$expand=Zaak(
$expand=ZaakActor(
Expand Down
Loading

0 comments on commit c0aa97c

Please sign in to comment.