Commit 960d3a922fe028c3e167cee1bce3c25816ae2b78

Authored by James McMinn
1 parent 3e670f4295
Exists in master

Removed the format flag. It served no purprose other than to confuse. A better s…

…olution would be to transform the format after it has been streamed.

Showing 1 changed file with 4 additions and 40 deletions Side-by-side Diff

... ... @@ -11,7 +11,6 @@ import (
11 11 "os"
12 12 "runtime"
13 13 "strconv"
14   - "strings"
15 14 )
16 15  
17 16 const RECV_BUF_LEN = 1024 * 1024
... ... @@ -20,16 +19,12 @@ const MAX_CHAN_LEN = 10000
20 19 const MODE_STREAM = 0
21 20 const MODE_FILE = 1
22 21  
23   -const FORMAT_SNOW = 0
24   -const FORMAT_STREAM = 1
25   -
26 22 var (
27 23 consumerKey *string = flag.String("ck", "", "Consumer Key")
28 24 consumerSecret *string = flag.String("cs", "", "Consumer Secret")
29 25 ot *string = flag.String("ot", "", "OAuth Token")
30 26 osec *string = flag.String("os", "", "OAuthTokenSecret")
31 27 inputFile *string = flag.String("if", "", "Input File")
32   - format *string = flag.String("format", "", "File Format")
33 28 port *int = flag.Int("port", 8053, "Port to listen on. Default: 8053")
34 29 firehose chan twitter.Tweet = make(chan twitter.Tweet, MAX_CHAN_LEN)
35 30 aliveStreams map[chan *[]byte]bool = make(map[chan *[]byte]bool)
... ... @@ -43,19 +38,12 @@ func main() {
43 38  
44 39 if *inputFile != "" {
45 40 mode = MODE_FILE
46   - if *format == "snow" {
47   - fileFormat = FORMAT_SNOW
48   - } else if *format == "stream" {
49   - fileFormat = FORMAT_STREAM
50   - } else {
51   - fmt.Println("Must specify file type as either -snow or -stream. See -help for details.")
52   - return
53   - }
54 41 } else if *consumerKey != "" || *consumerSecret != "" || *ot != "" || *osec != "" {
55 42 if *consumerKey == "" || *consumerSecret == "" || *ot == "" || *osec == "" {
56 43 fmt.Println("Must specify all of -ck, -cs, -ot and -os. See -help for details.")
57 44 return
58 45 }
  46 + mode = MODE_STREAM
59 47 } else {
60 48 fmt.Println("Must specify either Twitter OAuth details or file location and format. See -help for details.")
61 49 return
... ... @@ -83,6 +71,7 @@ func main() {
83 71 }
84 72 }
85 73  
  74 +// Reads a file into a channel
86 75 func readFileInto(into chan *[]byte) {
87 76 f, err := os.Open(*inputFile)
88 77 if err != nil {
... ... @@ -106,12 +95,7 @@ func readFileInto(into chan *[]byte) {
106 95 break
107 96 }
108 97  
109   - var t twitter.Tweet
110   - if fileFormat == FORMAT_STREAM {
111   - t = twitter.JSONtoTweet(line)
112   - } else {
113   - t = parseSNOW(line)
114   - }
  98 + t := twitter.JSONtoTweet(line)
115 99  
116 100 j, err := twitter.TweetToJSON(t)
117 101 if err != nil {
... ... @@ -122,26 +106,6 @@ func readFileInto(into chan *[]byte) {
122 106 }
123 107 }
124 108  
125   -func parseSNOW(line []byte) twitter.Tweet {
126   - // TODO: Handle ParseInt errors
127   - t := new(twitter.Tweet)
128   - parts := strings.SplitN(string(line), "\t", 11)
129   - code := parts[5]
130   - if code == "200" {
131   - id, _ := strconv.ParseInt(parts[6], 10, 64)
132   - username := parts[7]
133   - text := parts[8]
134   - time, _ := strconv.ParseUint(parts[9], 10, 64)
135   - t.Id = id
136   - t.User.Name = username
137   - t.Text = text
138   - t.Timestamp = time
139   - return *t
140   - } else {
141   - return nil
142   - }
143   -}
144   -
145 109 func handleConnection(conn net.Conn) {
146 110 stream := make(chan *[]byte, MAX_CHAN_LEN)
147 111  
... ... @@ -156,7 +120,7 @@ func handleConnection(conn net.Conn) {
156 120 t := <-stream
157 121 _, err := conn.Write(*t)
158 122 if err != nil {
159   - println("Closing connection: ", err.Error())
  123 + log.Println("Closing connection: ", err.Error())
160 124 break
161 125 }
162 126 }