Browse Source

gets tweets, english and catalan works, next step add spanish

master
arnaucode 7 years ago
parent
commit
80d0e254f7
11 changed files with 216 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +37
    -0
      README.md
  3. +57
    -0
      color.go
  4. +26
    -0
      festival.go
  5. +16
    -0
      main.go
  6. +2
    -0
      readCa.sh
  7. +40
    -0
      readConfigTokensAndConnect.go
  8. +2
    -0
      readEs.sh
  9. +13
    -0
      timeline.go
  10. BIN
      twitterReader
  11. +22
    -0
      waitTime.go

+ 1
- 0
.gitignore

@ -0,0 +1 @@
twitterConfig.json

+ 37
- 0
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"
}
```

+ 57
- 0
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
}

+ 26
- 0
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))
}

+ 16
- 0
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)
}
}
}

+ 2
- 0
readCa.sh

@ -0,0 +1,2 @@
#!/bin/bash
festcat_llegeix <<< $1

+ 40
- 0
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
}

+ 2
- 0
readEs.sh

@ -0,0 +1,2 @@
#!/bin/bash
festcat_llegeix <<< $1

+ 13
- 0
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)
}

BIN
twitterReader


+ 22
- 0
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)
}

Loading…
Cancel
Save