Skip to content

Instantly share code, notes, and snippets.

@tansengming
Last active February 11, 2020 22:22
Show Gist options
  • Select an option

  • Save tansengming/bc5e54d02534c0de35c8900bffd6e0ac to your computer and use it in GitHub Desktop.

Select an option

Save tansengming/bc5e54d02534c0de35c8900bffd6e0ac to your computer and use it in GitHub Desktop.

Revisions

  1. tansengming revised this gist Feb 11, 2020. 2 changed files with 15 additions and 10 deletions.
    21 changes: 13 additions & 8 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,15 @@
    require 'csv'
    require 'time'
    require 'rake/clean'
    require 'irb'
    require 'pathname'
    require 'json'
    require 'rake/clean'
    require 'time'

    class Tweet
    attr_reader :tweet_id, :created_at

    def initialize(row)
    @tweet_id = row['tweet_id']
    @created_at = Time.parse(row['timestamp'])
    @tweet_id = row.dig('tweet', 'id')
    @created_at = Time.parse(row.dig('tweet', 'created_at'))
    @row = row
    end
    end
    @@ -78,9 +79,13 @@ end

    CLEAN.include 'daily/tweets-*.html'

    task :parse_csv do
    file = 'tweets.csv'
    tweets = CSV.read(file, headers:true).map{|row| Tweet.new(row)}
    task :parse_json do
    tweet_path = Pathname.new('tweet.js')
    tweet_json = tweet_path.read[25..-1]
    tweets = JSON
    .parse(tweet_json)
    .map{ |row| Tweet.new(row) }

    mkdir_p 'daily'

    (1..12).each do |month|
    4 changes: 2 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    - retrieve your twitter archive from https://twitter.com/settings/account
    - make sure `tweets.csv` is in the same folder as the Rakefile
    - `rake clean && rake parse_csv`
    - make sure `tweet.js` is in the same folder as the Rakefile
    - `rake clean && rake parse_json`
    - `open index.html`
  2. tansengming revised this gist Mar 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    }

    var currentTime = new Date();
    window.location = "daily/tweets-" + currentTime.getMonth().pad() + "-" + currentTime.getDate().pad() + ".html"
    window.location = "daily/tweets-" + (currentTime.getMonth() + 1).pad() + "-" + currentTime.getDate().pad() + ".html"
    </script>
    </body>
    </html>
  3. tansengming revised this gist Mar 5, 2017. No changes.
  4. tansengming revised this gist Mar 5, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,7 @@ class DayPage
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=480">
    </head>
    <body>
    HEADEOF
  5. tansengming revised this gist Mar 5, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    - retrieve your twitter archive from https://twitter.com/settings/account
    - make sure `tweets.csv` is in the same folder as the Rakefile
    - `rake clean && rake parse_csv`
    - `rake clean && rake parse_csv`
    - `open index.html`
  6. tansengming revised this gist Mar 5, 2017. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <script type="text/javascript">
    Number.prototype.pad = function(size) {
    var s = String(this);
    while (s.length < (size || 2)) {s = "0" + s;}
    return s;
    }

    var currentTime = new Date();
    window.location = "daily/tweets-" + currentTime.getMonth().pad() + "-" + currentTime.getDate().pad() + ".html"
    </script>
    </body>
    </html>
  7. tansengming revised this gist Mar 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rakefile
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ class DayPage

    def call
    header +
    tweets.map{|tweet| tweet.created_at.year}.uniq.sort.map do |year|
    tweets.map{|tweet| tweet.created_at.year}.uniq.sort.reverse.map do |year|
    [
    "<h2>#{year}</h2>",
    tweets.select{|t| t.created_at.year == year }.map(&:tweet_id).map{|id| embed(id)}
  8. tansengming revised this gist Mar 5, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -75,18 +75,19 @@ class DayPage
    end
    end

    CLEAN.include 'tweets-*.html'
    CLEAN.include 'daily/tweets-*.html'

    task :parse_csv do
    file = 'tweets.csv'
    tweets = CSV.read(file, headers:true).map{|row| Tweet.new(row)}
    mkdir_p 'daily'

    (1..12).each do |month|
    (1..31).each do |day|
    tweets_on_this_day = tweets.select{|tweet| tweet.created_at.month == month && tweet.created_at.day == day}.sort_by(&:created_at)

    if tweets_on_this_day.any?
    tweet_filename = "tweets-#{sprintf '%02d', month}-#{sprintf '%02d', day}.html"
    tweet_filename = "daily/tweets-#{sprintf '%02d', month}-#{sprintf '%02d', day}.html"

    puts "Writing #{tweet_filename}..."
    open(tweet_filename, 'w') do |f|
  9. tansengming revised this gist Mar 5, 2017. 2 changed files with 3 additions and 0 deletions.
    File renamed without changes.
    3 changes: 3 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    - retrieve your twitter archive from https://twitter.com/settings/account
    - make sure `tweets.csv` is in the same folder as the Rakefile
    - `rake clean && rake parse_csv`
  10. tansengming created this gist Mar 5, 2017.
    98 changes: 98 additions & 0 deletions janky_twitter_day.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    require 'csv'
    require 'time'
    require 'rake/clean'
    require 'irb'

    class Tweet
    attr_reader :tweet_id, :created_at

    def initialize(row)
    @tweet_id = row['tweet_id']
    @created_at = Time.parse(row['timestamp'])
    @row = row
    end
    end

    class DayPage
    attr_reader :tweets, :filename
    def self.call(tweets)
    new(tweets).call
    end

    def initialize(tweets)
    @tweets = tweets
    end

    def call
    header +
    tweets.map{|tweet| tweet.created_at.year}.uniq.sort.map do |year|
    [
    "<h2>#{year}</h2>",
    tweets.select{|t| t.created_at.year == year }.map(&:tweet_id).map{|id| embed(id)}
    ]
    end.flatten.join("\n") +
    footer
    end

    def embed(id)
    str=<<~EMBEDEOF
    <blockquote class="twitter-tweet" data-lang="en">
    <a href="https://twitter.com/sengming/status/#{id}"></a>
    </blockquote>
    EMBEDEOF
    end

    def header
    str=<<~HEADEOF
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    </head>
    <body>
    HEADEOF
    end

    def footer
    str=<<~FOOTEREOF
    <script>window.twttr = (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
    if (d.getElementById(id)) return t;
    js = d.createElement(s);
    js.id = id;
    js.src = "https://platform.twitter.com/widgets.js";
    fjs.parentNode.insertBefore(js, fjs);
    t._e = [];
    t.ready = function(f) {
    t._e.push(f);
    };
    return t;
    }(document, "script", "twitter-wjs"));</script>
    </body>
    FOOTEREOF
    end
    end

    CLEAN.include 'tweets-*.html'

    task :parse_csv do
    file = 'tweets.csv'
    tweets = CSV.read(file, headers:true).map{|row| Tweet.new(row)}

    (1..12).each do |month|
    (1..31).each do |day|
    tweets_on_this_day = tweets.select{|tweet| tweet.created_at.month == month && tweet.created_at.day == day}.sort_by(&:created_at)

    if tweets_on_this_day.any?
    tweet_filename = "tweets-#{sprintf '%02d', month}-#{sprintf '%02d', day}.html"

    puts "Writing #{tweet_filename}..."
    open(tweet_filename, 'w') do |f|
    f.write DayPage.(tweets_on_this_day)
    end
    end
    end
    end
    end