Skip to content

Instantly share code, notes, and snippets.

@penguwin
Last active February 19, 2017 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penguwin/430cbe35fc6c6f7ba50ebb0722ee67bb to your computer and use it in GitHub Desktop.
Save penguwin/430cbe35fc6c6f7ba50ebb0722ee67bb to your computer and use it in GitHub Desktop.
instagrambee
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nicolas Martin <penguwingithub@gmail.com>
*/
// Package instagrambee is a Bee that can interface with instagram
package instagrambee
import (
"fmt"
"github.com/carbocation/go-instagram/instagram"
"github.com/muesli/beehive/bees"
)
// InstagramBee is a Bee that can interact with instagram
type InstagramBee struct {
bees.Bee
client *instagram.Client
clientID string
clientSecret string
accessToken string
}
// Action triggers the actions passed to it.
func (mod *InstagramBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}
switch action.Name {
case "follow":
var userID string
action.Options.Bind("userid", &userID)
fmt.Println(userID)
_, err := mod.client.Relationships.Follow(userID)
if err != nil {
mod.LogErrorf("error following user %s on Instagram %v", userID, err)
}
case "unfollow":
var userID string
action.Options.Bind("userid", &userID)
_, err := mod.client.Relationships.Unfollow(userID)
if err != nil {
mod.LogErrorf("Error unfollowing user on Instagram %v", err)
}
case "block":
var userID string
action.Options.Bind("userid", &userID)
_, err := mod.client.Relationships.Block(userID)
if err != nil {
mod.LogErrorf("Error blocking user on Instagram %v", err)
}
case "unblock":
var userID string
action.Options.Bind("userid", &userID)
_, err := mod.client.Relationships.Unblock(userID)
if err != nil {
mod.LogErrorf("Error unblocking user on Instagram %v", err)
}
case "approve":
var userID string
action.Options.Bind("userid", &userID)
_, err := mod.client.Relationships.Approve(userID)
if err != nil {
mod.LogErrorf("Error approving user %v", err)
}
case "like":
var mediaID string
action.Options.Bind("media_ID", &mediaID)
err := mod.client.Likes.Like(mediaID)
if err != nil {
mod.LogErrorf("Error liking media %v", err)
}
case "unlike":
var mediaID string
action.Options.Bind("media_ID", &mediaID)
err := mod.client.Likes.Unlike(mediaID)
if err != nil {
mod.LogErrorf("Error unliking media %v", err)
}
default:
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}
return outs
}
// Run executes the Bee's event loop.
func (mod *InstagramBee) Run(eventChan chan bees.Event) {
mod.client = instagram.NewClient(nil)
mod.client.ClientSecret = mod.clientSecret
mod.client.AccessToken = mod.accessToken
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *InstagramBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("client_id", &mod.clientID)
options.Bind("client_secret", &mod.clientSecret)
options.Bind("access_token", &mod.accessToken)
}
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nicolas Martin <penguwingithub@gmail.com>
*/
// Package instagrambee is a Bee that can interface with instagram
package instagrambee
import "github.com/muesli/beehive/bees"
// InstagramBeeFactory os a factory for InstagramBees.
type InstagramBeeFactory struct {
bees.BeeFactory
}
// New returns a new Bee instance configured with the supplied options.
func (factory *InstagramBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := InstagramBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)
return &bee
}
// ID returns the ID of this Bee.
func (factory *InstagramBeeFactory) ID() string {
return "instagrambee"
}
// Name returns the name of this Bee.
func (factory *InstagramBeeFactory) Name() string {
return "Instagram"
}
// Description returns the desciption of this Bee.
func (factory *InstagramBeeFactory) Description() string {
// TODO: Specify 'stuff'
return "Do stuff on Instagram"
}
// Image returns the filename of an image for this Bee.
func (factory *InstagramBeeFactory) Image() string {
return factory.ID() + ".png" // TODO: Add picture
}
// LogoColor returns ther preferred logo background color (used by the admin interface).
func (factory *InstagramBeeFactory) LogoColor() string {
return "#DB7093" // TODO: Find fitting color
}
// Options returns the options available to configure this Bee.
func (factory *InstagramBeeFactory) Options() []bees.BeeOptionDescriptor {
return []bees.BeeOptionDescriptor{
{
Name: "client_ID",
Description: "Client ID for the Instagram API",
Type: "string",
},
{
Name: "client_secret",
Description: "Client secret for the Instagram API",
Type: "string",
},
{
Name: "access_token",
Description: "Access token Instagram API",
Type: "string",
},
}
}
// Actions describes the available actions provided by this Bee.
func (factory *InstagramBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Name: "follow",
Description: "Follows another user on Instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "userid",
Description: "Instagram ID of the user you want to follow",
Type: "string",
Mandatory: true,
},
},
},
{
Namespace: factory.Name(),
Name: "unfollow",
Description: "unfollows another user on instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "userid",
Description: "instagram id of the user you want to follow",
Type: "string",
Mandatory: true,
},
},
},
{
Namespace: factory.Name(),
Name: "block",
Description: "blocks another user on instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "userid",
Description: "instagram id of the user you want to block",
Type: "string",
Mandatory: true,
},
},
},
{
Namespace: factory.Name(),
Name: "unblock",
Description: "unblocks another user on instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "userid",
Description: "instagram id of the user you want to unblock",
Type: "string",
Mandatory: true,
},
},
},
{
Namespace: factory.Name(),
Name: "approve",
Description: "approves another user on instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "userid",
Description: "instagram id of the user you want to approve",
Type: "string",
Mandatory: true,
},
},
},
{
Namespace: factory.Name(),
Name: "like",
Description: "likes media on instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "mediaid",
Description: "instagram id of the media you want to like",
Type: "string",
Mandatory: true,
},
},
},
{
Namespace: factory.Name(),
Name: "unlike",
Description: "unlikes media on instagram",
Options: []bees.PlaceholderDescriptor{
{
Name: "mediaid",
Description: "instagram id of the media you want to unlike",
Type: "string",
Mandatory: true,
},
},
},
}
return actions
}
func init() {
f := InstagramBeeFactory{}
bees.RegisterFactory(&f)
}
FO[0000] Registering Resource Resource=HiveResource
INFO[0000] Registering Resource Resource=BeeResource
INFO[0000] Registering Resource Resource=ChainResource
INFO[0000] Registering Resource Resource=ActionResource
INFO[0000]
INFO[0000] Beehive is buzzing...
INFO[0000] Worker bee ready: testbee - testbee
INFO[0000] Worker bee ready: Instagrambee - Instagrambee
INFO[0000] [testbee]: Connecting to IRC: irc.freenode.org:6667
INFO[0006] [testbee]: Connected to IRC: irc.freenode.org:6667
INFO[0016]
INFO[0016] Event received: testbee / message - A message was received over IRC, either in a channel or a private query
INFO[0016] Options: {channel string #beehive_test}
INFO[0016] Options: {user string test_case}
INFO[0016] Options: {hostmask string 48514da@gateway/web/freenode/ip.84.133.20.218}
INFO[0016] Options: {text string 460563723}
INFO[0016] Executing chain: Follow user - Follow user on Instagram
INFO[0016] Executing action: Instagrambee / follow - Follows another user on Instagram
INFO[0016] Options: {userID string 460563723}
INFO[0017] [Instagrambee]:
ERRO[0017] error following user on Instagram (0):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment