1. 8

Recently I got locked out of my YouTube account. Instead of spending more time trying to fix another problem with a Google product, I decided to start self-hosting my videos.

This is one of those test videos, which is a timelapse of developing a view counter.

  1. 5

    Textual summary:

    • With the Bottle web framework on the back end:

      • Define a route GET /viewcounter?url=… that reads that page’s viewcount from the website.viewcount column in a database and returns it in a JSON response.
      • Define a route POST /viewcounter?url=… that increments the viewcount for that page.
        • The view count is updated probabilistically – incremented by 10, with a 1 in 10 chance. This is presumably to make it harder to detect exactly when other users view the page.
    • In the layout HTML file with the template for all pages, put a script tag that runs at page load. In that script:

      • Read the current URL from window.location to pass to the subsequent requests.
      • Use fetch to hit the GET route defined above, then read the views field from the returned JSON data and put its text on the page inside the #views element.
      • Use navigator.sendBeacon to hit the POST route defined above. (Since this runs on page load, just loading the page registers a view.)
    1. 2

      Thanks for getting it onto writing.

      • Just to give some more info, the data is stored in a Postgresql server.
      • The Bottle web app is running on AWS Lambda, using a custom WSGI wrapper I wrote.
    2. 2

      Song name?

      1. 2

        Alison Wonderland - Drugs

      2. 2

        Are you going to publish the view data next to the video’s? Or is it just for your own analytics?

        1. 3

          I am planning to display it next to the videos. For my own analytics, I usually just use my server logs.

        2. 1

          FYI: The “Newer Posts” and “Older Posts” labels are mixed up on your home page!

          1. 1

            More livecoding videos like this please!