Skip to content

Commit

Permalink
Remove user stream and events from TwitterBee, as Twitter shut down t…
Browse files Browse the repository at this point in the history
…hat API
  • Loading branch information
muesli committed Oct 26, 2020
1 parent 4cb87ec commit 1caa3e3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 376 deletions.
161 changes: 2 additions & 159 deletions bees/twitterbee/twitterbee.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package twitterbee

import (
"net/url"
"strings"
"time"

"github.com/ChimeraCoder/anaconda"
Expand Down Expand Up @@ -136,164 +135,8 @@ func (mod *TwitterBee) Run(eventChan chan bees.Event) {
mod.self, err = mod.twitterAPI.GetSelf(url.Values{})
mod.handleAnacondaError(err, "Could not get own user object from Twitter API")

mod.handleStream()
}

func (mod *TwitterBee) handleStreamEvent(item interface{}) {
switch status := item.(type) {
case anaconda.DirectMessage:
// mod.Logf("DM: %s %s", status.Text, status.Sender.ScreenName)
ev := bees.Event{
Bee: mod.Name(),
Name: "direct_message",
Options: []bees.Placeholder{
{
Name: "username",
Type: "string",
Value: status.Sender.ScreenName,
},
{
Name: "text",
Type: "string",
Value: status.Text,
},
},
}
mod.evchan <- ev

case anaconda.Tweet:
// mod.Logf("Tweet: %+v %s %s", status, status.Text, status.User.ScreenName)

ev := bees.Event{
Bee: mod.Name(),
Name: "tweet",
Options: []bees.Placeholder{
{
Name: "username",
Type: "string",
Value: status.User.ScreenName,
},
{
Name: "text",
Type: "string",
Value: status.Text,
},
{
Name: "url",
Type: "url",
Value: "https://twitter.com/statuses/" + status.IdStr,
},
},
}

for _, mention := range status.Entities.User_mentions {
if mention.Screen_name == mod.self.ScreenName {
if status.RetweetedStatus != nil {
// someone retweeted a tweet from us
ev.Name = "retweet"
} else {
// someone mentioned us
ev.Name = "mention"
}
break
}
}

if status.User.ScreenName == mod.self.ScreenName {
if status.RetweetedStatus != nil {
// we retweeted a tweet
ev.Options.SetValue("username", "string", status.RetweetedStatus.User.ScreenName)
ev.Name = "retweeted"
} else {
// regular tweet
ev.Name = "tweeted"
}
}

mod.evchan <- ev

case anaconda.EventTweet:
ev := bees.Event{
Bee: mod.Name(),
Name: "",
Options: []bees.Placeholder{
{
Name: "username",
Type: "string",
Value: status.Source.ScreenName,
},
{
Name: "text",
Type: "string",
Value: status.TargetObject.Text,
},
{
Name: "url",
Type: "url",
Value: "https://twitter.com/statuses/" + status.TargetObject.IdStr,
},
},
}

switch status.Event.Event {
case "favorited_retweet":
fallthrough
case "favorite":
ev.Name = "like"
case "unfavorited_retweet":
fallthrough
case "unfavorite":
ev.Name = "unlike"
default:
mod.Logln("Unhandled event type", status.Event.Event)
mod.Logf("Event Tweet: %+v", status)
return
}

if status.Source.ScreenName == mod.self.ScreenName {
// If we're the source of this event, use the passive form of the event name
// and change the username to the original tweet author
ev.Options.SetValue("username", "string", status.TargetObject.User.ScreenName)

if strings.HasSuffix(ev.Name, "e") {
ev.Name += "d"
} else {
ev.Name += "ed"
}
}

mod.evchan <- ev

case anaconda.LimitNotice:
mod.Logf("Limit: %+v", status)
case anaconda.DisconnectMessage:
mod.Logf("Disconnect: %+v", status)
case anaconda.UserWithheldNotice:
mod.Logf("User Withheld: %+v", status)
case anaconda.StatusWithheldNotice:
mod.Logf("Status Withheld: %+v", status)
case anaconda.Friendship:
mod.Logf("Friendship: %s", status.Screen_name)
case anaconda.Relationship:
mod.Logf("Relationship: %s", status.Source.Screen_name)
case anaconda.Event:
mod.Logf("Event: %+v", status)
default:
// mod.Logf("Unhandled type %+v", item)
}
}

func (mod *TwitterBee) handleStream() {
s := mod.twitterAPI.UserStream(url.Values{})

for {
select {
case <-mod.SigChan:
return
case item := <-s.C:
mod.handleStreamEvent(item)
}
}
// wait for the bee to be shut down
<-mod.SigChan
}

// ReloadOptions parses the config options and initializes the Bee.
Expand Down
217 changes: 0 additions & 217 deletions bees/twitterbee/twitterbeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,223 +138,6 @@ func (factory *TwitterBeeFactory) Actions() []bees.ActionDescriptor {
return actions
}

// Events describes the available events provided by this Bee.
func (factory *TwitterBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{
{
Namespace: factory.Name(),
Name: "direct_message",
Description: "is triggered when someone sends you a direct message",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the DM's author",
Type: "string",
},
{
Name: "text",
Description: "text content of the DM",
Type: "string",
},
},
},
{
Namespace: factory.Name(),
Name: "tweeted",
Description: "is triggered when you tweeted something",
Options: []bees.PlaceholderDescriptor{
{
Name: "text",
Description: "text content of the tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "tweet",
Description: "is triggered whenever someone you follow tweets",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the tweet's author",
Type: "string",
},
{
Name: "text",
Description: "text content of the tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "mention",
Description: "is triggered whenever someone mentions you on Twitter",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the mention's author",
Type: "string",
},
{
Name: "text",
Description: "text content of the mention",
Type: "string",
},
{
Name: "url",
Description: "URL of the mention",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "retweeted",
Description: "is triggered when you retweeted something",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the user that you retweeted",
Type: "string",
},
{
Name: "text",
Description: "Text of the retweeted tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the retweeted tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "retweet",
Description: "is triggered when someone retweets one of your tweets",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the user that retweeted your tweet",
Type: "string",
},
{
Name: "text",
Description: "Text of the retweeted tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the retweeted tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "liked",
Description: "is triggered when you liked something",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the user that originally wrote the liked tweet",
Type: "string",
},
{
Name: "text",
Description: "Text of the liked tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the liked tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "like",
Description: "is triggered when someone likes one of your tweets",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the user that liked your tweet",
Type: "string",
},
{
Name: "text",
Description: "Text of the liked tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the liked tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "unliked",
Description: "is triggered when you unliked something",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the user that originally wrote the unliked tweet",
Type: "string",
},
{
Name: "text",
Description: "Text of the unliked tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the unliked tweet",
Type: "url",
},
},
},
{
Namespace: factory.Name(),
Name: "unlike",
Description: "is triggered when someone un-likes one of your tweets",
Options: []bees.PlaceholderDescriptor{
{
Name: "username",
Description: "Twitter handle of the user that liked your tweet",
Type: "string",
},
{
Name: "text",
Description: "Text of the liked tweet",
Type: "string",
},
{
Name: "url",
Description: "URL of the liked tweet",
Type: "url",
},
},
},
}
return events
}

func init() {
f := TwitterBeeFactory{}
bees.RegisterFactory(&f)
Expand Down

0 comments on commit 1caa3e3

Please sign in to comment.