This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 0.3.0
- Loading branch information
Showing
13 changed files
with
380 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.2.1 | ||
0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/fallenstedt/twitter-stream/example | ||
|
||
replace github.com/fallenstedt/twitter-stream => /Users/alex/Projects/twitter-stream/ | ||
|
||
go 1.15 | ||
|
||
require github.com/fallenstedt/twitter-stream v0.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/fallenstedt/twitter-stream v0.2.1 h1:lhnQDj1R9od8ZpiHya5tgbBon1eQH4Iqwlj0hqqLnK8= | ||
github.com/fallenstedt/twitter-stream v0.2.1/go.mod h1:e3GVow5/CaCeacD7kMH7ubyKHUNVSNntzFddzmzwP/8= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
twitterstream "github.com/fallenstedt/twitter-stream" | ||
"time" | ||
) | ||
|
||
const key = "YOUR_KEY" | ||
const secret = "YOUR_SECRET" | ||
|
||
|
||
func main() { | ||
// Use your favorite function from below here | ||
startStream() | ||
//addRules() | ||
//getRules() | ||
//deleteRules() | ||
} | ||
|
||
|
||
func startStream() { | ||
tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(key, secret).RequestBearerToken() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
api := twitterstream.NewTwitterStream(tok.AccessToken) | ||
|
||
err = api.Stream.StartStream("") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
go func() { | ||
for message := range api.Stream.GetMessages() { | ||
if message.Err != nil { | ||
panic(message.Err) | ||
} | ||
fmt.Println(string(message.Data)) | ||
} | ||
}() | ||
|
||
time.Sleep(time.Second * 30) | ||
} | ||
|
||
func addRules() { | ||
tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(key, secret).RequestBearerToken() | ||
if err != nil { | ||
panic(err) | ||
} | ||
api := twitterstream.NewTwitterStream(tok.AccessToken) | ||
res, err := api.Rules.AddRules(`{ | ||
"add": [ | ||
{"value": "cat has:images", "tag": "cat tweets with images"} | ||
] | ||
}`, true) // dryRun is set to true | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if res.Errors != nil && len(res.Errors) > 0 { | ||
//https://developer.twitter.com/en/support/twitter-api/error-troubleshooting | ||
panic(fmt.Sprintf("Received an error from twiiter: %v", res.Errors)) | ||
} | ||
|
||
fmt.Println("I have created this many rules: ") | ||
fmt.Println(res.Meta.Summary.Created) | ||
} | ||
|
||
func getRules() { | ||
tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(key, secret).RequestBearerToken() | ||
if err != nil { | ||
panic(err) | ||
} | ||
api := twitterstream.NewTwitterStream(tok.AccessToken) | ||
res, err := api.Rules.GetRules() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if res.Errors != nil && len(res.Errors) > 0 { | ||
//https://developer.twitter.com/en/support/twitter-api/error-troubleshooting | ||
panic(fmt.Sprintf("Received an error from twiiter: %v", res.Errors)) | ||
} | ||
|
||
fmt.Println(res.Data) | ||
} | ||
|
||
|
||
func deleteRules() { | ||
tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(key, secret).RequestBearerToken() | ||
if err != nil { | ||
panic(err) | ||
} | ||
api := twitterstream.NewTwitterStream(tok.AccessToken) | ||
|
||
// use api.Rules.GetRules to find the ID number for an existing rule | ||
res, err := api.Rules.AddRules(`{ | ||
"delete": { | ||
"ids": ["1234567890"] | ||
} | ||
}`, true) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if res.Errors != nil && len(res.Errors) > 0 { | ||
//https://developer.twitter.com/en/support/twitter-api/error-troubleshooting | ||
panic(fmt.Sprintf("Received an error from twiiter: %v", res.Errors)) | ||
} | ||
|
||
fmt.Println(res) | ||
} |
Oops, something went wrong.