Skip to content

Commit

Permalink
autopush@1456993047
Browse files Browse the repository at this point in the history
  • Loading branch information
denkhaus committed Mar 3, 2016
1 parent 1cefde2 commit 87b3fd3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
17 changes: 17 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
type Config struct {
ctx *cli.Context
neoHost string
neoPort int
neoUsername string
neoPassword string
twitterConsumerKey string
twitterConsumerSecret string
twitterUserKey string
Expand All @@ -25,6 +28,10 @@ func NewConfig(ctx *cli.Context) (*Config, error) {
if cnf.neoHost == "" {
return nil, errors.New("neo host is not defined")
}
cnf.neoPort = ctx.GlobalInt("port")
if cnf.neoPort == 0 {
return nil, errors.New("neo port is not defined")
}

cnf.twitterConsumerKey = ctx.GlobalString("consumer-key")
if cnf.twitterConsumerKey == "" {
Expand Down Expand Up @@ -57,3 +64,13 @@ func (p *Config) ScreenName() (string, error) {

return p.screenName, nil
}

func (p *Config) NeoUsername() string {
p.neoUsername = p.ctx.GlobalString("user")
return p.neoUsername
}

func (p *Config) NeoPassword() string {
p.neoPassword = p.ctx.GlobalString("password")
return p.neoPassword
}
73 changes: 46 additions & 27 deletions engine.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"database/sql"
"fmt"
"net/http"
"net/url"
"time"

"strings"
"github.com/juju/errors"
"github.com/kurrik/oauth1a"
"github.com/kurrik/twittergo"
_ "gopkg.in/cq.v1"
"github.com/jmcvetta/neoism"
)

const (
Expand Down Expand Up @@ -45,6 +44,28 @@ func (p *Engine) createClient() {
p.client = twittergo.NewClient(config, user)
}

func (p *Engine) openDatabase() (*neoism.Database, error){

var userInfo string
if p.cnf.neoUsername != ""{
userInfo = fmt.Sprintf("%s:%s", p.cnf.neoUsername, p.cnf.neoPassword)

if len(strings.Split(userInfo, ":")) != 2{
return nil, errors.New("invalid neo4j credentials")
}

}


db, err := neoism.Connect()
if err != nil {
return errors.Annotate(err, "open database")
}
defer db.Close()


}

func (p *Engine) AddUser() error {
p.createClient()

Expand Down Expand Up @@ -95,56 +116,54 @@ func (p *Engine) getTweets(screenName string, maxID uint64) (twittergo.Timeline,
return results, nil
}

func (p *Engine) tweetsImport(db *sql.DB, tweets twittergo.Timeline) error {
func (p *Engine) tweetsImport(db *neoism.Database, tweets twittergo.Timeline) error {
stmt, err := db.Prepare(CYPHER_TWEETS_IMPORT)
if err != nil {
return errors.Annotate(err, "db prepare")
}
defer stmt.Close()

rows, err := stmt.Query(tweets)
_, err := stmt.Exec(tweets)
if err != nil {
return errors.Annotate(err, "db query")
return errors.Annotate(err, "db exec")
}
defer rows.Close()

return nil
}

func (p *Engine) tweetsGetMaxID(screenName string) (uint64, error) {
db, err := sql.Open("neo4j-cypher", p.cnf.neoHost)
if err != nil {
return 0, errors.Annotate(err, "open database")
}
defer db.Close()

stmt, err := db.Prepare(CYPHER_TWEETS_MAX_ID)
db, err := p.openDatabase()
if err != nil {
return 0, errors.Annotate(err, "db prepare")
return 0, errors.Annotate(err, "open database")
}
defer stmt.Close()

rows, err := stmt.Query(screenName)
if err != nil {



var maxID uint64
cq := neoism.CypherQuery{
Statement: CYPHER_TWEETS_MAX_ID,
Parameters: neoism.Props{"screen_name": screenName},
Result: &maxID,
}

if err := db.Cypher(&cq); err != nil {
return 0, errors.Annotate(err, "db query")
}
defer rows.Close()

var maxID uint64
err = rows.Scan(&maxID)
if err != nil {
return 0, errors.Annotate(err, "scan query")
}


return maxID, nil
}

func (p *Engine) AddTweets() error {
p.createClient()

db, err := sql.Open("neo4j-cypher", p.cnf.neoHost)
db, err := neoism.Connect(p.cnf.neoHost)
if err != nil {
return errors.Annotate(err, "open database")
return 0, errors.Annotate(err, "open database")
}


defer db.Close()

screenName, err := p.cnf.ScreenName()
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,24 @@ func main() {
Name: "host, d",
Usage: "Neo4j host",
EnvVar: "NEO4_HOST",
Value: "http://localhost:7474",
Value: "localhost",
},
cli.IntFlag{
Name: "port, P",
Usage: "Neo4j port",
EnvVar: "NEO4_PORT",
Value: 7474,
},
cli.StringFlag{
Name: "user, u",
Usage: "Neo4j username",
EnvVar: "NEO4_USERNAME",
},
cli.StringFlag{
Name: "password, p",
Usage: "Neo4j password",
EnvVar: "NEO4_PASSWORD",
},

cli.StringFlag{
Name: "consumer-key",
Usage: "Twitter Consumer Key",
Expand Down

0 comments on commit 87b3fd3

Please sign in to comment.