Skip to content

Instantly share code, notes, and snippets.

@tomnomnom
Last active November 23, 2024 23:31
Show Gist options
  • Save tomnomnom/93dfb0f4a427e7d3c179d42b8921c80e to your computer and use it in GitHub Desktop.
Save tomnomnom/93dfb0f4a427e7d3c179d42b8921c80e to your computer and use it in GitHub Desktop.

Revisions

  1. tomnomnom renamed this gist Nov 23, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. tomnomnom revised this gist Nov 23, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion intstructions.md
    Original file line number Diff line number Diff line change
    @@ -58,4 +58,4 @@ gnuplot tweets.gnuplot

    That should produce `tweet-times.png`

    You'll probably want to adjust the title etc so it doesn't say "Tweets by TomNomNOm", and the date range (`set xrange ["2008-01-01":"2025-01-01"]`) to match your own data.
    You'll probably want to adjust the title etc so it doesn't say "Tweets by TomNomNom", and the date range (`set xrange ["2008-01-01":"2025-01-01"]`) to match your own data.
  3. tomnomnom revised this gist Nov 23, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions intstructions.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Plotting tweets with gnuplot

    Guide to producing [a chart like this one](https://bsky.app/profile/tomnomnom.com/post/3lblghzb6qc2u).

    This is a bit of a hack job, natch.

    Grab your Twitter archive and extract it. You need to find `data/tweet-headers.js` and make a copy of it:
  4. tomnomnom revised this gist Nov 23, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion intstructions.md
    Original file line number Diff line number Diff line change
    @@ -56,4 +56,4 @@ gnuplot tweets.gnuplot

    That should produce `tweet-times.png`

    You'll probably want to adjust the title etc so it doesn't say "Tweets by TomNomNOm"
    You'll probably want to adjust the title etc so it doesn't say "Tweets by TomNomNOm", and the date range (`set xrange ["2008-01-01":"2025-01-01"]`) to match your own data.
  5. tomnomnom created this gist Nov 23, 2024.
    59 changes: 59 additions & 0 deletions intstructions.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    # Plotting tweets with gnuplot

    This is a bit of a hack job, natch.

    Grab your Twitter archive and extract it. You need to find `data/tweet-headers.js` and make a copy of it:

    ```
    cp data/tweet-headers.js tweets.json
    ```

    The data itself is valid JSON so you need to remove the variable name and equals at the top of the file in `tweets.json`:

    ```javascript
    window.YTD.tweet_headers.part0 = [
    {
    "tweet" : {
    "tweet_id" : "1858951780102443094",
    "user_id" : "17440273",
    "created_at" : "Tue Nov 19 19:13:36 +0000 2024"
    }
    },
    // ...
    ```

    Make it look like this:

    ```json
    [
    {
    "tweet" : {
    "tweet_id" : "1858951780102443094",
    "user_id" : "17440273",
    "created_at" : "Tue Nov 19 19:13:36 +0000 2024"
    }
    },
    ```

    We want to parse that `created_at` field and get just the year-month-day and time of day into a file for `gnuplot` to read.
    I've written a little python script to do that (`tz-adjust.py`) , for which you may need to `pip3 install pytz`.

    Sorry, I don't really know python so I might have done something silly in this bit, but it seemed like a good choice at the time. You might want to change the timezone in the script to match your own.

    You'll need `jq` too for this next bit:

    ```
    jq -r '.[].tweet.created_at' < tweets.json | python3 tz-adjust.py > tweet-times.txt
    ```

    This is using `jq` to extract the created time, the python script to parse the time and adjust the timezone, and write the results to `tweet-times.txt`

    Now we can run the `gnuplot` script (you'll need to install `gnuplot`, obv):

    ```
    gnuplot tweets.gnuplot
    ```

    That should produce `tweet-times.png`

    You'll probably want to adjust the title etc so it doesn't say "Tweets by TomNomNOm"
    26 changes: 26 additions & 0 deletions tweets.gnuplot
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    set datafile separator " "

    set terminal pngcairo size 1200,800
    set output "tweet-times.png"

    set title "Tweets by TomNomNom"
    set xlabel "Year"
    set ylabel "Time of day (GMT)"

    set xdata time
    set timefmt "%Y-%m-%d"
    set format x "%Y"
    set xrange ["2008-01-01":"2025-01-01"]

    set ydata time
    set timefmt "%H:%M"
    set format y "%H:%M"
    set yrange ["00:00":"23:59"]

    set grid

    set style data points
    set pointsize 0.5
    set key off

    plot 'tweet-times.txt' using (timecolumn(1,"%Y-%m-%d")):2
    10 changes: 10 additions & 0 deletions tz-adjust.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    import sys
    import pytz
    from datetime import datetime

    output_tz = pytz.timezone('Europe/London')

    for line in sys.stdin:
    dt = datetime.strptime(line.rstrip(), '%a %b %d %H:%M:%S %z %Y')
    local = dt.astimezone(output_tz)
    print(local.strftime('%Y-%m-%d %H:%M'))