Skip to content

Commit

Permalink
use HomeTimeline
Browse files Browse the repository at this point in the history
  • Loading branch information
theoremoon committed Mar 18, 2023
1 parent 8db25e3 commit 1f0097f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
15 changes: 15 additions & 0 deletions database.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"log"

_ "github.com/mattn/go-sqlite3"
)
Expand Down Expand Up @@ -34,3 +35,17 @@ func insertShellGei(db *sql.DB, userID int64, screenName string, tweetID int64,
}
return nil
}

func isProcessed(db *sql.DB, tweetID int64) bool {
var cnt int
err := db.QueryRow("select count(*) from shellgeis where tweet_id = ?", tweetID).Scan(&cnt)

if err != nil {
log.Printf("isProcessed: %v\n", err)
return false
}
if cnt == 0 {
return false
}
return true
}
44 changes: 23 additions & 21 deletions main.go
@@ -1,3 +1,4 @@
//go:build !test
// +build !test

package main
Expand All @@ -15,7 +16,6 @@ import (
"log"
"net/url"
"os"
"strings"
"sync"
"time"

Expand All @@ -39,6 +39,9 @@ func processTweet(tweet anaconda.Tweet, self anaconda.User, api *anaconda.Twitte
if !isFollower(api, tweet) {
return
}
if isProcessed(db, tweet.Id) {
return
}

t, err := tweet.CreatedAtTime()
if err != nil {
Expand All @@ -58,9 +61,7 @@ func processTweet(tweet anaconda.Tweet, self anaconda.User, api *anaconda.Twitte
insertResult(db, tweet.Id, result, err)

if err != nil {
if err.(*stdError) == nil {
_, _ = api.PostTweet("@theoldmoon0602 internal error", url.Values{})
}
log.Printf("runcmd: %v\n", err)
return
}

Expand All @@ -75,7 +76,7 @@ func processTweet(tweet anaconda.Tweet, self anaconda.User, api *anaconda.Twitte
return
}

/// ShellgeiBot main function
// / ShellgeiBot main function
func botMain(twitterConfigFile, botConfigFile string) {
twitterKey, err := twitter.ParseTwitterKey(twitterConfigFile)
if err != nil {
Expand All @@ -91,34 +92,35 @@ func botMain(twitterConfigFile, botConfigFile string) {
anaconda.SetConsumerKey(twitterKey.ConsumerKey)
anaconda.SetConsumerSecret(twitterKey.ConsumerSecret)
api := anaconda.NewTwitterApi(twitterKey.AccessToken, twitterKey.AccessSecret)
api.SetLogger(anaconda.BasicLogger)

v := url.Values{}
self, err := api.GetSelf(v)
self, err := api.GetSelf(url.Values{})
if err != nil {
log.Fatal(err)
}

config, err := parseBotConfig(botConfigFile)
if err != nil {
log.Fatal(err)
}
v.Set("track", strings.Join(config.Tags, ","))
stream := api.PublicStreamFilter(v)

config, err := parseBotConfig(botConfigFile)
ticker := time.NewTimer(1 * time.Minute)
v := url.Values{}
v.Set("count", "200")
for {
t := <-stream.C
switch tweet := t.(type) {
case anaconda.Tweet:
config, err = parseBotConfig(botConfigFile)
if err != nil {
_, _ = api.PostTweet("@theoldmoon0602 Internal error", v)
log.Fatal(err)
}
tweets, err := api.GetHomeTimeline(v)
if err != nil {
log.Printf("GetHomeTimeline: %v", err)
<-ticker.C
continue
}

go func() {
processTweet(tweet, self, api, db, config)
}()
for _, tweet := range tweets {
processTweet(tweet, self, api, db, config)
v.Set("since_id", tweet.IdStr)
}

<-ticker.C
}
}

Expand Down
1 change: 1 addition & 0 deletions twitter.go
Expand Up @@ -197,6 +197,7 @@ func isFollower(api *anaconda.TwitterApi, tweet anaconda.Tweet) bool {
v := url.Values{}
u, err := api.GetUsersShowById(tweet.User.Id, v)
if err != nil {
log.Printf("isFollower %v\n", err)
return false
}
return u.Following
Expand Down

0 comments on commit 1f0097f

Please sign in to comment.