Skip to content

Examples

Alexandre Grison edited this page Feb 26, 2016 · 1 revision

Extract some tweets talking about #golang

Let's say we want to grab the last tweets about #golang with less than 120 characters and mentioning someone, it would be possible by loading these tweets into a Dataset, adding dynamic columns (to compute the length and extract the tags and mentions), and constraints to filter tweets with more than 120 characters and no mentions.

package main

import (
	"fmt"
	"github.com/ChimeraCoder/anaconda"
	tablib "github.com/agrison/go-tablib"
	"net/url"
	"regexp"
	"strings"
)

func main() {
	anaconda.SetConsumerKey("...")
	anaconda.SetConsumerSecret("...")
	api := anaconda.NewTwitterApi("...", "...")
	defer api.Close()

	// create a new dataset
	ds := tablib.NewDataset([]string{"User", "Tweet", "Date"})

	// search twitter
	v := url.Values{}
	v.Set("count", "50")
	searchResult, _ := api.GetSearch("golang", v)
	for _, tweet := range searchResult.Statuses {
		t, _ := tweet.CreatedAtTime()
		ds.AppendValues("@"+tweet.User.ScreenName, tweet.Text, t)
	}

	// compute the length of the tweet content
	ds.AppendDynamicColumn("Length", func(row []interface{}) interface{} {
		return len(row[1].(string))
	})
	// add a constraint to validate tweets with size < 120
	ds.ConstrainColumn("Length", func(value interface{}) bool {
		return value.(int) < 120
	})
	// extract the tags in the tweet
	ds.AppendDynamicColumn("Tags", func(row []interface{}) interface{} {
		return extract("#", row[1].(string))
	})
	// extract the mentions in the tweet
	ds.AppendDynamicColumn("Mentions", func(row []interface{}) interface{} {
		return extract("@", row[1].(string))
	})
	// add a constraint to validate tweets containing mentions
	ds.ConstrainColumn("Mentions", func(value interface{}) bool {
		return len(value.(string)) > 0
	})

        filtered := ds.ValidSubset().sort("Length")
	fmt.Println(filtered.Tabular(tablib.TabularCondensed))
}

func extract(start, str string) string {
	return strings.Join(regexp.MustCompile("("+start+"\\w+)").FindAllString(str, 10), ", ")
}

Which will output something like this

---------------------  -----------------------------------------------------------------------------------------------------------------------  -------------------------  -----------  ---------------------------------  ----------------------------------------
                User                                                                                                                    Tweet                       Date       Length                               Tags                                  Mentions
---------------------  -----------------------------------------------------------------------------------------------------------------------  -------------------------  -----------  ---------------------------------  ----------------------------------------
         @koolhead17                                                                                   RT @RanjibDey: need golang jira client       2016-02-26T07:11:33Z           38                                                                   @RanjibDey
        @slteichmann                                                            @BjoernSchilberg https://t.co/8XZWwwNuZH #Golang reached Uber       2016-02-26T07:57:18Z           61                            #Golang                          @BjoernSchilberg
       @martinhaynes                                           GopherJS (@GopherJS) a @golang to #JavaScript complier https://t.co/5eEeZOxk76       2016-02-26T09:05:05Z           78                        #JavaScript                        @GopherJS, @golang
              @tam_x                                        RT @ramusara: 明日〜 > Golang 勉強会 in Kagawa https://t.co/TD97gYVy1w #golang       2016-02-26T08:02:33Z           90                            #golang                                 @ramusara
    @Luke_Bennellick                               Very cool 5 minute screencasts similar to Ruby Tapas, for @golang https://t.co/dziqVPhgu6       2016-02-26T09:53:36Z           90                                                                      @golang
            @shijucv                          The #Golang Daily is out! https://t.co/PpJ5ewE1Yq Stories via @tiagoprn @mind_scratch @4gophers       2016-02-26T08:19:51Z           95                            #Golang       @tiagoprn, @mind_scratch, @4gophers
    @AllegroTechBlog                          RT @janiszt: So You Wanna Go Fast? https://t.co/u1CvXJvBKk by @tyler_treat #golang #perfmatters       2016-02-26T09:38:51Z           95              #golang, #perfmatters                    @janiszt, @tyler_treat
        @javascriptd                         RT @martinhaynes: GopherJS (@GopherJS) a @golang to #JavaScript complier https://t.co/5eEeZOxk76       2016-02-26T09:37:29Z           96                        #JavaScript         @martinhaynes, @GopherJS, @golang
        @insights_js                         RT @martinhaynes: GopherJS (@GopherJS) a @golang to #JavaScript complier https://t.co/5eEeZOxk76       2016-02-26T09:32:33Z           96                        #JavaScript         @martinhaynes, @GopherJS, @golang
          @JohnKoepi                   Golang UK Conference 2015 - Andrew Gerrand - Stupid Gopher Tricks https://t.co/yNQsE8AsMt via @YouTube       2016-02-26T09:28:08Z          102                                                                     @YouTube
     @DarkFox_h4ck3r                  RT @gohackernews: Take a REST with HTTP/2, Protobufs, and Swagger #grpc #golang https://t.co/UJSPmuzqHY       2016-02-26T09:48:47Z          103                     #grpc, #golang                             @gohackernews
             @VJCORE               RT @mholt6: WOW, @srcgraph's new design and service offering sure is slick!! Graph all the #golang things.       2016-02-26T08:27:24Z          106                            #golang                        @mholt6, @srcgraph
            @abionic              @RanjibDey this #console client for Jira in #golang (apparently from Netflix folks) https://t.co/vKrIV4E9B6       2016-02-26T09:00:24Z          107                  #console, #golang                                @RanjibDey
              @tam_x                         RT @MrExcluded: Golang 勉強会 in Kagawa に参加を申し込みました! https://t.co/ZyW5smAdfv #golang       2016-02-26T09:40:02Z          111                            #golang                               @MrExcluded
          @PEFunCast       Should you be coding in #Ruby or #Java or #GoLang? Have a listen at https://t.co/FRqTbcODTh @ITunesPodcast #ruby f       2016-02-26T09:15:02Z          114       #Ruby, #Java, #GoLang, #ruby                            @ITunesPodcast
---------------------  -----------------------------------------------------------------------------------------------------------------------  -------------------------  -----------  ---------------------------------  ----------------------------------------

Now let's say you want to export that to a Postgres (imagine you have loaded much much more tweets), you can create a script to insert into Postgres and work directly with it:

fmt.Println(filtered.Postgres("tweets"))

And you can just load the following:

CREATE TABLE IF NOT EXISTS tweets
(
	id SERIAL PRIMARY KEY,
	User TEXT,
	Tweet TEXT,
	Date TIMESTAMP,
	Length NUMERIC,
	Tags TEXT,
	Mentions TEXT
);

INSERT INTO tweets VALUES(1, '@koolhead17', 'RT @RanjibDey: need golang jira client', '2016-02-26T07:11:33Z', 38, '', '@RanjibDey');
INSERT INTO tweets VALUES(2, '@slteichmann', '@BjoernSchilberg https://t.co/8XZWwwNuZH #Golang reached Uber', '2016-02-26T07:57:18Z', 61, '#Golang', '@BjoernSchilberg');
INSERT INTO tweets VALUES(3, '@martinhaynes', 'GopherJS (@GopherJS) a @golang to #JavaScript complier https://t.co/5eEeZOxk76', '2016-02-26T09:05:05Z', 78, '#JavaScript', '@GopherJS, @golang');
INSERT INTO tweets VALUES(4, '@tam_x', 'RT @ramusara: 明日〜 > Golang 勉強会 in Kagawa https://t.co/TD97gYVy1w #golang', '2016-02-26T08:02:33Z', 90, '#golang', '@ramusara');
INSERT INTO tweets VALUES(5, '@Luke_Bennellick', 'Very cool 5 minute screencasts similar to Ruby Tapas, for @golang https://t.co/dziqVPhgu6', '2016-02-26T09:53:36Z', 90, '', '@golang');
INSERT INTO tweets VALUES(6, '@shijucv', 'The #Golang Daily is out! https://t.co/PpJ5ewE1Yq Stories via @tiagoprn @mind_scratch @4gophers', '2016-02-26T08:19:51Z', 95, '#Golang', '@tiagoprn, @mind_scratch, @4gophers');
INSERT INTO tweets VALUES(7, '@AllegroTechBlog', 'RT @janiszt: So You Wanna Go Fast? https://t.co/u1CvXJvBKk by @tyler_treat #golang #perfmatters', '2016-02-26T09:38:51Z', 95, '#golang, #perfmatters', '@janiszt, @tyler_treat');
INSERT INTO tweets VALUES(8, '@javascriptd', 'RT @martinhaynes: GopherJS (@GopherJS) a @golang to #JavaScript complier https://t.co/5eEeZOxk76', '2016-02-26T09:37:29Z', 96, '#JavaScript', '@martinhaynes, @GopherJS, @golang');
INSERT INTO tweets VALUES(9, '@insights_js', 'RT @martinhaynes: GopherJS (@GopherJS) a @golang to #JavaScript complier https://t.co/5eEeZOxk76', '2016-02-26T09:32:33Z', 96, '#JavaScript', '@martinhaynes, @GopherJS, @golang');
INSERT INTO tweets VALUES(10, '@JohnKoepi', 'Golang UK Conference 2015 - Andrew Gerrand - Stupid Gopher Tricks https://t.co/yNQsE8AsMt via @YouTube', '2016-02-26T09:28:08Z', 102, '', '@YouTube');
INSERT INTO tweets VALUES(11, '@DarkFox_h4ck3r', 'RT @gohackernews: Take a REST with HTTP/2, Protobufs, and Swagger #grpc #golang https://t.co/UJSPmuzqHY', '2016-02-26T09:48:47Z', 103, '#grpc, #golang', '@gohackernews');
INSERT INTO tweets VALUES(12, '@VJCORE', 'RT @mholt6: WOW, @srcgraph''s new design and service offering sure is slick!! Graph all the #golang things.', '2016-02-26T08:27:24Z', 106, '#golang', '@mholt6, @srcgraph');
INSERT INTO tweets VALUES(13, '@abionic', '@RanjibDey this #console client for Jira in #golang (apparently from Netflix folks) https://t.co/vKrIV4E9B6', '2016-02-26T09:00:24Z', 107, '#console, #golang', '@RanjibDey');
INSERT INTO tweets VALUES(14, '@tam_x', 'RT @MrExcluded: Golang 勉強会 in Kagawa に参加を申し込みました! https://t.co/ZyW5smAdfv #golang', '2016-02-26T09:40:02Z', 111, '#golang', '@MrExcluded');
INSERT INTO tweets VALUES(15, '@PEFunCast', 'Should you be coding in #Ruby or #Java or #GoLang? Have a listen at https://t.co/FRqTbcODTh @ITunesPodcast #ruby f', '2016-02-26T09:15:02Z', 114, '#Ruby, #Java, #GoLang, #ruby', '@ITunesPodcast');

COMMIT;
Clone this wiki locally