Browse Source

init again

master
mark 2 years ago
commit
978bb3a398
6 changed files with 189 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +5
    -0
      README.md
  3. +22
    -0
      UNLICENSE
  4. +9
    -0
      go.mod
  5. +37
    -0
      go.sum
  6. +114
    -0
      main.go

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
tmw-go
runtest.sh

+ 5
- 0
README.md View File

@@ -0,0 +1,5 @@
just [tmw](https://git.szy.io/mdszy/tmw) but written in go instead

eventually this will replace tmw

but for now it doesn't actually work

+ 22
- 0
UNLICENSE View File

@@ -0,0 +1,22 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

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 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.

+ 9
- 0
go.mod View File

@@ -0,0 +1,9 @@
module git.szy.io/mdszy/tmw-go

go 1.14

require (
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f
github.com/dghubble/oauth1 v0.6.0
toast.cafe/x/libuconf v0.0.0-20200419230957-881131b6a896
)

+ 37
- 0
go.sum View File

@@ -0,0 +1,37 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
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 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
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/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
toast.cafe/x/libuconf v0.0.0-20200419230957-881131b6a896 h1:ksv6p6Qtpkh7NllBysOfQ2evSWK8yxm05a+xt1kSf94=
toast.cafe/x/libuconf v0.0.0-20200419230957-881131b6a896/go.mod h1:TaBPB8REkQ/uD2infB+S2QvIUMXVR4L45S1S8EYW5oM=

+ 114
- 0
main.go View File

@@ -0,0 +1,114 @@
package main

import (
"log"
"os"
"os/signal"
"syscall"

"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
"toast.cafe/x/libuconf"
)

func downloadFromTweet(t *twitter.Tweet) {
log.Printf("Checking for media in tweet %v", t.IDStr)
if t.ExtendedEntities == nil {
log.Println("No media found.")
return
}

entities := t.ExtendedEntities.Media

for _, item := range entities {
log.Printf("%v", item.URL)
}
}

func twitterLogin(consumerKey, consumerSecret, appToken, tokenSecret string) (*twitter.Client, error) {
config := oauth1.NewConfig(consumerKey, consumerSecret)
token := oauth1.NewToken(appToken, tokenSecret)

httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)

return client, nil
}

func setupDemux() twitter.SwitchDemux {
demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) {
downloadFromTweet(tweet)
}

return demux
}

func setupStream(u *twitter.User, c *twitter.Client) (*twitter.Stream, error) {

filterStreamParams := &twitter.StreamFilterParams{
Follow: []string{u.IDStr},
StallWarnings: twitter.Bool(true),
}

stream, err := c.Streams.Filter(filterStreamParams)

if err != nil {
return stream, err
}

return stream, nil

}

func main() {
optionset := &libuconf.OptionSet{
AppName: "tmw",
}

var consumerKey, consumerSecret, token, tokenSecret *string

consumerKey = optionset.String("consumerkey", "", "Twitter application Consumer Key")
consumerSecret = optionset.String("consumersecret", "", "Twitter application Consumer Secret")
token = optionset.String("token", "", "Twitter application Access Token")
tokenSecret = optionset.String("tokensecret", "", "Twitter application Access Token Secret")

err := optionset.ParseEnv()

if err != nil {
panic(err)
}

client, err := twitterLogin(*consumerKey, *consumerSecret, *token, *tokenSecret)

if err != nil {
panic(err)
}

verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
}

user, _, _ := client.Accounts.VerifyCredentials(verifyParams)
log.Printf("Logged in as %v.\n", user.Name)

demux := setupDemux()

log.Println("Starting stream...")

stream, err := setupStream(user, client)

if err != nil {
panic(err)
}

go demux.HandleChan(stream.Messages)

ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println(<-ch)

log.Println("Stopping stream...")
stream.Stop()
}

Loading…
Cancel
Save