Skip to content

Instantly share code, notes, and snippets.

@jirutka
Created September 20, 2015 21:38

Revisions

  1. jirutka created this gist Sep 20, 2015.
    169 changes: 169 additions & 0 deletions README.adoc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,169 @@
    = Twitter statistics for #WebExpo 2015

    * Number of tweets (excluding RTs): 1139
    * Number of favourites: 2804
    * Number of retweets: 1090
    * Number of tweeting users: 341
    == TOP tweeting users

    List of most active users sorted by number of their tweets with hashtag #WebExpo.

    |===
    | Rank | User(s) | Tweets

    | 1.
    | https://twitter.com/webexpo[@webexpo]
    | 55

    | 2.
    | https://twitter.com/imrichl[@imrichl]
    | 40

    | 3.
    | https://twitter.com/Dzulaya[@Dzulaya]
    | 37

    | 4.
    | https://twitter.com/BoBMarvan[@BoBMarvan]
    | 28

    | 5.
    | https://twitter.com/janjadud[@janjadud]
    | 19

    | 6.
    | https://twitter.com/iKristy[@iKristy]
    | 18

    | 7.
    | https://twitter.com/PavelUngr[@PavelUngr], https://twitter.com/Pavlina_speaks[@Pavlina_speaks], https://twitter.com/avast_antivirus[@avast_antivirus], https://twitter.com/jirihelmich[@jirihelmich], https://twitter.com/tesino82[@tesino82]
    | 17

    | 8.
    | https://twitter.com/faxecz[@faxecz]
    | 16

    | 9.
    | https://twitter.com/ProductivityTOP[@ProductivityTOP], https://twitter.com/dmelich[@dmelich], https://twitter.com/pp2tech[@pp2tech]
    | 15

    | 10.
    | https://twitter.com/JakubJirutka[@JakubJirutka], https://twitter.com/MichalAndera[@MichalAndera]
    | 14

    |===


    == TOP favourited tweets

    |===
    | Rank | Tweet(s) | Favourites

    | 1.
    | @robinpokorny: https://twitter.com/robinpokorny/status/645114476379930624[O #webexpo se mluví na ČT24! Akorát by jim měli říct, že je to "on-line". http://t.co/1i341MZBll]
    | 48

    | 2.
    | @otecfura: https://twitter.com/otecfura/status/645263965459730432[Právě jsem se někomu na záchodě podíval na penis a usmál se. A pak že jsou lidi od IT asociálové. #webexpo]
    | 36

    | 3.
    | @JirkaCh: https://twitter.com/JirkaCh/status/645313820609613824[#Webexpo je vývojářská konference a je na něm vidět, jak se mění oblékání mužů v #IT. Více košil a sak, méně vytahaných triček #webexpo2015]
    | 27

    | 4.
    | @ZdenekLanc: https://twitter.com/ZdenekLanc/status/645522415682998272[Na webu @CzechTV dnes streamujeme vybrané #webexpo talks! http://t.co/t0ncCrLBOy #webexpo2015]
    | 26

    | 5.
    | @atpok: https://twitter.com/atpok/status/645241706216984576[Jestli vás zaujal @jirijerabek testováním konceptů v BBC, tady si ještě přečtěte: http://t.co/RBgUI0ipIj #webexpo]

    @faxecz: https://twitter.com/faxecz/status/645264146368323585["... a tohle, co mi rikate, to je fakt, nebo si to myslite?" Dulezita otazka na klienta! Petr Šídlo na #webexpo]

    @filippodstavec: https://twitter.com/filippodstavec/status/645528032120324100["Správný analytik vždycky najde graf, který roste" #truestory #webexpo by @VojtaRocek]
    | 22

    | 6.
    | @signalizer: https://twitter.com/signalizer/status/645615358741712898[~1700 visitors, 17+ awesome design talks, tons of caffeine consumed. Tired, proud and happy. Thanks!

    ☑ #webexpo 2015 http://t.co/TbdUGgGkcY]
    | 20

    | 7.
    | @adactio: https://twitter.com/adactio/status/645527520775929856[My floating head looms above @Anna_Debenham’s #WebExpo presentation. http://t.co/khbXL1u7cZ]
    | 19

    | 8.
    | @PavelUngr: https://twitter.com/PavelUngr/status/645169383069892609[Přednáška mela být v češtině, ale prý resérčři dělají kastmr a jůzr rýsérč, aby dosáhli valjejbl biznys. Tak vám nevím :-) #webexpo]
    | 18

    | 9.
    | @BoBMarvan: https://twitter.com/BoBMarvan/status/645246036978597888[A framework k testování konceptů od @JiriJerabek bude na http://t.co/fxL8UpKB8k #webexpo]
    | 16

    | 10.
    | @madciapka: https://twitter.com/madciapka/status/645505035435155456[Raňajky na #webexpo majú dosť vysoký hipster index :-) http://t.co/h8JUISLo2N]

    @filiphracek: https://twitter.com/filiphracek/status/645623569234329600[Líbila se vám moje #webexpo přednáška? Dejte si ji ještě jednou, bez mých hloupých keců. https://t.co/4GJ9EqYTeE]

    @Michelle_LSKT: https://twitter.com/Michelle_LSKT/status/645629104977506306[Rozhovor @annaud a @SarkaStrossova shrnuje #webexpo:
    A: Šárko, taks neumřela!
    Š: Já to nestihla!]
    | 15

    |===


    == TOP retweeted tweets

    |===
    | Rank | Tweet(s) | Retweets

    | 1.
    | @ZdenekLanc: https://twitter.com/ZdenekLanc/status/645522415682998272[Na webu @CzechTV dnes streamujeme vybrané #webexpo talks! http://t.co/t0ncCrLBOy #webexpo2015]
    | 24

    | 2.
    | @robinpokorny: https://twitter.com/robinpokorny/status/645114476379930624[O #webexpo se mluví na ČT24! Akorát by jim měli říct, že je to "on-line". http://t.co/1i341MZBll]
    | 22

    | 3.
    | @izatlouk: https://twitter.com/izatlouk/status/645520271491887104[Na včerejší #webexpo party jsem přišla o fitbit. Nenašla jej nějaká dobrá duše mezi vámi? Značka ztráty a nálezy...]
    | 15

    | 4.
    | @schambep: https://twitter.com/schambep/status/645533932587511808[Studenti @stunome spolupracovali na novem developerskeho portalu CS https://t.co/74wsgFZf7r @csas_devs #webexpo #api http://t.co/olAunsizQG]
    | 14

    | 5.
    | @dejfnn: https://twitter.com/dejfnn/status/645159031770873856[Data znamenají víc než názory @paunikar #webexpo]

    @atpok: https://twitter.com/atpok/status/645241706216984576[Jestli vás zaujal @jirijerabek testováním konceptů v BBC, tady si ještě přečtěte: http://t.co/RBgUI0ipIj #webexpo]
    | 11

    | 6.
    | @filippodstavec: https://twitter.com/filippodstavec/status/645528032120324100["Správný analytik vždycky najde graf, který roste" #truestory #webexpo by @VojtaRocek]
    | 10

    | 7.
    | @faxecz: https://twitter.com/faxecz/status/645264146368323585["... a tohle, co mi rikate, to je fakt, nebo si to myslite?" Dulezita otazka na klienta! Petr Šídlo na #webexpo]

    @webexpo: https://twitter.com/webexpo/status/645612199948484608[“Bad news! If you are not passionate enough, you’ll quit one day. Remember this.” Dominika Blackappl #webexpo]
    | 9

    | 8.
    | @codeas: https://twitter.com/codeas/status/645137234379808768[Vizualizace nejvytíženějších poboček @Ceskasporitelna http://t.co/JsTsYuOaeZ #webexpo #webapi
    @csas_devs @jansechovec http://t.co/zOJibOxcq6]

    @janmelvil: https://twitter.com/janmelvil/status/645553956328992769[TIP: Využijte kuponu WEBEXPO15 v našem eshopu (http://t.co/vaON4ObYtV). Dosáhnete cca 30% slevy! Vyprší dnes o půlnoci. #webexpo @webexpo]

    @divani: https://twitter.com/divani/status/645615301002964993["Krátkodobé ciele vám neprinesú šťastie. Vyberajte si prácu, s ktorou môžete zmeniť svet." Dominka Blackappl na #webexpo]
    | 8

    |===


    ''''
    _Generated on 2015-09-20 23:27:03 +0200._
    125 changes: 125 additions & 0 deletions stats.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,125 @@
    #!/usr/bin/env ruby
    #
    # Quick & dirty script to generate some Twitter statistics for events.
    #
    # Usage:
    # 1. gem install twitter
    # 2. Register your “app” on https://apps.twitter.com/apps and set OAuth
    # credentials here.
    # 3. Profit!
    #
    # License:
    # Creative Commons CC-Zero This file is made available under the Creative
    # Commons CC0 1.0 Universal Public Domain Dedication.
    # https://creativecommons.org/publicdomain/zero/1.0/deed.en

    require 'ostruct'
    require 'twitter'


    ######## Configuration ########

    HASHTAG = '#WebExpo'
    DATE_FROM = '2015-09-18'
    DATE_TO = '2015-09-21'

    USERS_LIST_LIMIT = 10
    FAVORITES_LIST_LIMIT = 10
    RETWEETS_LIST_LIMIT = 8

    client = Twitter::REST::Client.new do |config|
    config.consumer_key = 'YOUR_CONSUMER_KEY'
    config.consumer_secret = 'YOUR_CONSUMER_SECRET'
    config.access_token = 'YOUR_ACCESS_TOKEN'
    config.access_token_secret = 'YOUR_ACCESS_SECRET'
    end

    year = DATE_FROM.split('-').first


    ######## Functions ########

    def top_tweets_by(field, tweets)
    tweets.lazy
    .group_by { |tw| tw.public_send(field) }
    .sort.reverse
    .map { |count, tweets| [count, tweets.sort_by(&:created_at)] }
    end

    def top_users_by_tweets_count(tweets)
    tweets.lazy
    .group_by(&:user)
    .map { |user, tweets| [user, tweets.count] }
    .group_by(&:last) # group by number of tweets
    .sort.reverse
    .map { |count, tuples| [count, tuples.map(&:first)] } # extract users as value of the entry
    .map { |count, users| [count, users.sort_by(&:screen_name)] }
    end

    def title(level, text)
    puts "\n\n#{'=' * level} #{text}\n\n"
    end

    def table(headers, data, &block)
    lines = ['|===', '| ' + headers.join(' | '), '']
    data.each_with_index do |item, idx|
    (yield item, idx + 1).each do |col|
    lines << "| #{col}"
    end
    lines << ''
    end
    lines << '|===' << ''
    puts lines.join("\n")
    end

    format_user = ->(user) { "#{user.uri}[@#{user.screen_name}] (#{user.followers_count})" }
    format_tweet = ->(tweet) { "@#{tweet.user.screen_name}: #{tweet.uri}[#{tweet.text}]" }


    ######## Main ########

    tweets = client.search("#{HASHTAG} since:#{DATE_FROM} until:#{DATE_TO}", result_type: :all)
    .reject { |tweet| tweet.retweet? }

    users = tweets.map(&:user).uniq
    favourites_count = tweets.map(&:favorite_count).reduce(:+)
    retweets_count = tweets.map(&:retweet_count).reduce(:+)
    top_users = top_users_by_tweets_count(tweets).take(USERS_LIST_LIMIT)
    top_favorites = top_tweets_by(:favorite_count, tweets).take(FAVORITES_LIST_LIMIT)
    top_retweets = top_tweets_by(:retweet_count, tweets).take(RETWEETS_LIST_LIMIT)


    # Print report in AsciiDoc

    title 1, "Twitter statistics for #{HASHTAG} #{year}"

    puts "* Number of tweets (excluding RTs): #{tweets.count}"
    puts "* Number of favourites: #{favourites_count}"
    puts "* Number of retweets: #{retweets_count}"
    puts "* Number of tweeting users: #{users.count}"


    title 2, 'TOP tweeting users'

    puts "List of most active users sorted by number of their tweets with hashtag #{HASHTAG}.\n\n"

    table ['Rank', 'User(s)', 'Tweets'], top_users do |(count, users), idx|
    ["#{idx}.", users.map(&format_user).join(', '), count]
    end


    title 2, 'TOP favourited tweets'

    table ['Rank', 'Tweet(s)', 'Favourites'], top_favorites do |(count, tweets), idx|
    ["#{idx}.", tweets.map(&format_tweet).join("\n\n"), count]
    end


    title 2, 'TOP retweeted tweets'

    table ['Rank', 'Tweet(s)', 'Retweets'], top_retweets do |(count, tweets), idx|
    ["#{idx}.", tweets.map(&format_tweet).join("\n\n"), count]
    end

    puts "\n\n''''"
    puts "_Generated on #{Time.now}._"