-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d9f1f46
Showing
7 changed files
with
198 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.idea | ||
/settings.yaml | ||
/tweetbot | ||
/lastTweetId |
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 @@ | ||
Copyright 2020 Tomas Norre Mikkelsen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,31 @@ | ||
# Twitter Bot | ||
|
||
This is a small and very simple twitter bot, which retweets based on search queries. | ||
|
||
## How to run this bot | ||
|
||
Add a `settings.yaml` file, same structure as `settings.example.yaml` and add your API Tokens etc. plus the search strings you want to retweet. | ||
|
||
When done, you can run it by doing: | ||
|
||
```shell | ||
$ go run tweetbot.go | ||
``` | ||
|
||
You can also compile a binary, depending on where it has to run | ||
|
||
```shell | ||
$ go build tweetbot.go | ||
``` | ||
|
||
or if you want it to run on you raspberry pi. | ||
|
||
```shell | ||
env GOOS=linux GOARCH=arm GOARM=5 go build tweetbot.go | ||
``` | ||
|
||
## Credits | ||
|
||
Thanks to [Miloskrca](https://github.com/miloskrca) for helping with the initial development and for introducing me to Golang. | ||
|
||
|
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,10 @@ | ||
module github.com/tomasnorre/tweetbot | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/adam-lavrik/go-imath v0.0.0-20200104135348-19d2b923d06c // indirect | ||
github.com/dghubble/oauth1 v0.6.0 // indirect | ||
github.com/tomasnorre/go-twitter v0.0.0-20200524180231-3470d4a44a9f // indirect | ||
gopkg.in/yaml.v2 v2.3.0 // indirect | ||
) |
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,21 @@ | ||
github.com/adam-lavrik/go-imath v0.0.0-20200104135348-19d2b923d06c h1:msbxYZZrXgKCMiV4QvBLMtSZ/YGqRnVtXuWMv7Vjvhw= | ||
github.com/adam-lavrik/go-imath v0.0.0-20200104135348-19d2b923d06c/go.mod h1:XjvqMUpGd3Xn9Jtzk/4GEBCSoBX0eB2RyriXgne0IdM= | ||
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY= | ||
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f h1:M2wB039zeS1/LZtN/3A7tWyfctiOBL4ty5PURBmDdWU= | ||
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE= | ||
github.com/dghubble/oauth1 v0.6.0 h1:m1yC01Ohc/eF38jwZ8JUjL1a+XHHXtGQgK+MxQbmSx0= | ||
github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk= | ||
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU= | ||
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY= | ||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= | ||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/tomasnorre/go-twitter v0.0.0-20200524180231-3470d4a44a9f h1:bIEju18NbObQt3n4wRLzYt1DjkmHXz2+blabns8tblg= | ||
github.com/tomasnorre/go-twitter v0.0.0-20200524180231-3470d4a44a9f/go.mod h1:5zTOQb2kIAyFzd89k/zH1PuGAXTO/a80RDBYnjTNMFQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= | ||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
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,10 @@ | ||
twitter: | ||
oauth_access_token: <string> | ||
oauth_access_token_secret: <string> | ||
consumer_key: <string> | ||
consumer_secret: <string> | ||
hash: | ||
- <one> | ||
- <two> | ||
- <three> | ||
|
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,115 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/adam-lavrik/go-imath/ix" | ||
"github.com/dghubble/oauth1" | ||
"github.com/tomasnorre/go-twitter/twitter" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Configuration struct { | ||
Twitter TwitterConf `yaml:"twitter"` | ||
} | ||
|
||
type TwitterConf struct { | ||
OauthAccessToken string `yaml:"oauth_access_token"` | ||
OauthAccessTokenSecret string `yaml:"oauth_access_token_secret"` | ||
ConsumerKey string `yaml:"consumer_key"` | ||
ConsumerSecret string `yaml:"consumer_secret"` | ||
Hash []string `yaml:"hash"` | ||
} | ||
|
||
func check(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} | ||
|
||
// WriteToFile will print any string of text to a file safely by | ||
// checking for errors and syncing at the end. | ||
func WriteToFile(filename string, data string) error { | ||
file, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = io.WriteString(file, data) | ||
check(err) | ||
return file.Sync() | ||
} | ||
|
||
func main() { | ||
|
||
file, err := os.Open("settings.yaml") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
filecontent, err := ioutil.ReadAll(file) | ||
check(err) | ||
|
||
var conf Configuration | ||
err = yaml.Unmarshal(filecontent, &conf) | ||
check(err) | ||
|
||
fileReference, err := os.Open("lastTweetId") | ||
check(err) | ||
|
||
lastTweetId, err := ioutil.ReadAll(fileReference) | ||
check(err) | ||
|
||
config := oauth1.NewConfig(conf.Twitter.ConsumerKey, conf.Twitter.ConsumerSecret) | ||
token := oauth1.NewToken(conf.Twitter.OauthAccessToken, conf.Twitter.OauthAccessTokenSecret) | ||
httpClient := config.Client(oauth1.NoContext, token) | ||
|
||
// Twitter client | ||
client := twitter.NewClient(httpClient) | ||
lastTweetIdValue, err := strconv.ParseInt(string(lastTweetId), 0, 64) | ||
check(err) | ||
|
||
out := make(chan *twitter.Search) | ||
|
||
for _, h := range conf.Twitter.Hash { | ||
go func(hash string) { | ||
// Search Tweets | ||
search, _, err := client.Search.Tweets(&twitter.SearchTweetParams{ | ||
Query: hash, | ||
Count: 5, | ||
ResultType: "recent", | ||
SinceID: lastTweetIdValue, | ||
}) | ||
check(err) | ||
out <- search | ||
}(h) | ||
} | ||
|
||
var tweetIds []int | ||
|
||
for i := 0; i < len(conf.Twitter.Hash); i++ { | ||
search := <-out | ||
|
||
for _, tweet := range search.Statuses { | ||
if tweet.RetweetedStatus != nil { | ||
continue | ||
} | ||
tweetIds = append(tweetIds, int(tweet.ID)) | ||
fmt.Println(tweet.ID, tweet.Text) | ||
|
||
var statusRetweetParam *twitter.StatusRetweetParams | ||
client.Statuses.Retweet(tweet.ID, statusRetweetParam) | ||
|
||
} | ||
} | ||
close(out) | ||
|
||
// Write latestTweetId to know where to start on next execution. | ||
latestTweetId := strconv.Itoa(ix.MaxSlice(tweetIds)) | ||
WriteToFile("lastTweetId", latestTweetId) | ||
} |