arnaucube
arnaucube/twitterReaderPublic Self-hosted repository
gets tweets, english and catalan works, next step add spanish
arnaucode idoctnef@openmailbox.org
Tue, 16 May 2017 21:30:20 +0200
commit

80d0e254f70f62b4f82e902c8f56857d5d5560e6

parent

36849e3f7630f923d3ad8f80478209ce2b37e27f

A .gitignore

@@ -0,0 +1,1 @@

+twitterConfig.json
A README.md

@@ -0,0 +1,37 @@

+# twitterReader +twitter reader assistant written in Go lang + +uses festival for the text to speech features + + +to execute +``` +./twitterReader +``` + +install festival +``` +sudo apt-get install festival festvox-kallpc16k +``` +install spanish +``` +sudo apt-get install festvox-ellpc11k +``` +install catalan voice +``` +sudo add-apt-repository ppa:zeehio/festcat +sudo apt-get update +sudo apt-get install festival-ca festvox-ca-ona-hts festcat-utils +``` + +needs the twitter account tokens in the file twitterConfig.json +```json +{ + "username": "account_name", + "consumer_key": "xxxxxxxxxxxxxx", + "consumer_secret": "xxxxxxxxxxx", + "access_token_key": "xxxxxxxxxx", + "access_token_secret": "xxxxxxx" +} + +```
A color.go

@@ -0,0 +1,57 @@

+package main + +import "fmt" + +//Color struct, defines the color +type Color struct{} + +var c Color + +//DarkGray color +func (c Color) DarkGray(t string) { + fmt.Print("\x1b[30;1m") //dark gray + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Red color +func (c Color) Red(t string) { + fmt.Print("\x1b[31;1m") //red + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Green color +func (c Color) Green(t string) { + fmt.Print("\x1b[32;1m") //green + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Yellow color +func (c Color) Yellow(t string) { + fmt.Print("\x1b[33;1m") //yellow + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Blue color +func (c Color) Blue(t string) { + fmt.Print("\x1b[34;1m") //blue + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Purple color +func (c Color) Purple(t string) { + fmt.Print("\x1b[35;1m") //purple + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +} + +//Cyan color +func (c Color) Cyan(t string) { + fmt.Print("\x1b[36;1m") //cyan + fmt.Println(t) + fmt.Print("\x1b[0m") //defaultColor +}
A festival.go

@@ -0,0 +1,26 @@

+package main + +import ( + "fmt" + "os/exec" + "strings" +) + +func festivalEn(text string) { + path, err := exec.LookPath("festival") + if err != nil { + return + } + festival := exec.Command(path, "--tts") + reader := strings.NewReader(text) + festival.Stdin = reader + err = festival.Run() +} + +func festivalCa(text string) { + out, err := exec.Command("bash", "readCa.sh", text).Output() + if err != nil { + fmt.Println("error") + } + fmt.Println(string(out)) +}
A main.go

@@ -0,0 +1,16 @@

+package main + +func main() { + c.Cyan("hi") + client := readConfigTokensAndConnect() + tweets := getTimeline(client) + for i := 0; i < len(tweets); i++ { + c.Cyan(tweets[i].User.ScreenName + ": " + tweets[i].Text) + c.Green(tweets[i].Lang) + if tweets[i].Lang == "en" { + festivalEn(tweets[i].User.ScreenName + ": " + tweets[i].Text) + } else { + festivalCa(tweets[i].User.ScreenName + ": " + tweets[i].Text) + } + } +}
A readCa.sh

@@ -0,0 +1,2 @@

+#!/bin/bash +festcat_llegeix <<< $1
A readConfigTokensAndConnect.go

@@ -0,0 +1,40 @@

+package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + + "github.com/dghubble/go-twitter/twitter" + "github.com/dghubble/oauth1" +) + +//Config stores the data from json twitterConfig.json file +type Config struct { + ConsumerKey string `json:"consumer_key"` + ConsumerSecret string `json:"consumer_secret"` + AccessTokenKey string `json:"access_token_key"` + AccessTokenSecret string `json:"access_token_secret"` +} + +func readConfigTokensAndConnect() (client *twitter.Client) { + var config Config + file, e := ioutil.ReadFile("twitterConfig.json") + if e != nil { + fmt.Println("error:", e) + } + content := string(file) + json.Unmarshal([]byte(content), &config) + fmt.Println("twitterConfig.json read comlete") + + fmt.Print("connecting to twitter api --> ") + configu := oauth1.NewConfig(config.ConsumerKey, config.ConsumerSecret) + token := oauth1.NewToken(config.AccessTokenKey, config.AccessTokenSecret) + httpClient := configu.Client(oauth1.NoContext, token) + // twitter client + client = twitter.NewClient(httpClient) + + fmt.Println("connection successfull") + + return client +}
A readEs.sh

@@ -0,0 +1,2 @@

+#!/bin/bash +festcat_llegeix <<< $1
A timeline.go

@@ -0,0 +1,13 @@

+package main + +import ( + "github.com/dghubble/go-twitter/twitter" +) + +func getTimeline(client *twitter.Client) []twitter.Tweet { + homeTimelineParams := &twitter.HomeTimelineParams{ + Count: 4, + } + tweets, _, _ := client.Timelines.HomeTimeline(homeTimelineParams) + return (tweets) +}
A waitTime.go

@@ -0,0 +1,22 @@

+package main + +import ( + "fmt" + "strconv" + "time" +) + +func waitMinutes(minutes int) { + //wait to avoid the twitter api limitation + timeToSleep := time.Duration(minutes) * time.Minute + fmt.Println("waiting " + strconv.Itoa(minutes) + " min") + fmt.Println(time.Now().Local()) + time.Sleep(timeToSleep) +} +func waitSeconds(seconds int) { + //wait to avoid the twitter api limitation + timeToSleep := time.Duration(seconds) * time.Second + fmt.Println("waiting " + strconv.Itoa(seconds) + " seconds") + fmt.Println(time.Now().Local()) + time.Sleep(timeToSleep) +}