Browse Source

downloads images and deletes tweets

master
mark 2 years ago
parent
commit
1f74bc2162
2 changed files with 34 additions and 7 deletions
  1. +2
    -1
      .gitignore
  2. +32
    -6
      main.go

+ 2
- 1
.gitignore View File

@@ -1,2 +1,3 @@
tmw-go
runtest.sh
runtest.sh
*.jpg

+ 32
- 6
main.go View File

@@ -38,17 +38,16 @@ func downloadFile(url, filepath string) error {
return nil
}

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

entities := t.ExtendedEntities.Media

for _, item := range entities {
log.Printf("%v", item.MediaURLHttps)
log.Printf("Downloading media: %v", item.MediaURLHttps)

url := item.MediaURLHttps
splitURL := strings.Split(url, "/")
@@ -59,6 +58,17 @@ func downloadFromTweet(t *twitter.Tweet) error {
if err != nil {
return err
}

log.Printf("Successfully downloaded %v", filename)
}

if burn {
log.Println("Deleting tweet...")
err := yeetTweet(c, t)
if err != nil {
return err
}
log.Println("Tweet deleted.")
}

return nil
@@ -74,10 +84,24 @@ func twitterLogin(consumerKey, consumerSecret, appToken, tokenSecret string) (*t
return client, nil
}

func setupDemux() twitter.SwitchDemux {
func yeetTweet(c *twitter.Client, t *twitter.Tweet) error {
destroyParams := &twitter.StatusDestroyParams{
ID: t.ID,
}

_, _, err := c.Statuses.Destroy(t.ID, destroyParams)

if err != nil {
return err
}

return nil
}

func setupDemux(c *twitter.Client, burn bool) twitter.SwitchDemux {
demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) {
downloadFromTweet(tweet)
downloadFromTweet(c, tweet, burn)
}

return demux
@@ -106,11 +130,13 @@ func main() {
}

var consumerKey, consumerSecret, token, tokenSecret *string
var burn *bool

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")
burn = optionset.Bool("burn", true, "Whether to delete the tweet after downloading media.")

err := optionset.ParseEnv()

@@ -132,7 +158,7 @@ func main() {
user, _, _ := client.Accounts.VerifyCredentials(verifyParams)
log.Printf("Logged in as %v.\n", user.Name)

demux := setupDemux()
demux := setupDemux(client, *burn)

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



Loading…
Cancel
Save