Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
Implement basic rate-limiting (#18)
Browse files Browse the repository at this point in the history
* implement basic rate-limiting commandline option

* add rateLimit option to readme
  • Loading branch information
charlesangus authored May 8, 2021
1 parent f4bd236 commit 3fb7ad6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ $ go run deleter.go
## Cookies

Cookies are saved to `$HOME/.go-cookies` if the `$GOCOOKIES` variable is not set (see https://github.com/juju/persistent-cookiejar).

## Options

### Rate-limiting

Facebook will temp-block you if you make too many requests too quickly. Run the command with the `-rateLimit <time in ms>` to introduce a delay before each request. If you're getting hung up on just searches or deletes, you can disable rate-limiting for one or the other with `-limitSearch=0` or `-limitDelete=0`.

E.g. one of:

```
$ ./deleter-linux -rateLimit 500
$ ./deleter-linux -rateLimit 500 -limitSearch=0
$ ./deleter-linux -rateLimit 500 -limitDelete=0
```

26 changes: 26 additions & 0 deletions deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package main

import (
"flag"
"fmt"
"github.com/AlecAivazis/survey"
"github.com/cheggaaa/pb/v3"
Expand Down Expand Up @@ -43,6 +44,10 @@ var categoriesMap = map[string]string{

var tokensInURLs = [...]string{"/removecontent", "/delete", "/report", "/events/remove.php", "&amp;content_type=4&amp;", "action=delete"}

var rateLimit int
var limitSearch bool
var limitDelete bool

type requester struct {
client *http.Client
jar *cookiejar.Jar
Expand Down Expand Up @@ -201,6 +206,9 @@ func (actRead *activityReader) ReadItems(year int, month int, category string) {
moreCounter := 1
var searchString string
for {
if limitSearch {
time.Sleep(time.Duration(rateLimit) * time.Millisecond)
}
actRead.StoreItemsFromOutput(output, category)

searchString = sectionIDStr + `_more_` + strconv.Itoa(moreCounter)
Expand Down Expand Up @@ -474,6 +482,10 @@ func (del *deleter) DeleteElement(elem *deleteElement) {
}
}()

if limitDelete {
time.Sleep(time.Duration(rateLimit) * time.Millisecond)
}

if elem.token == "/report" {
// Removing tags in activity log has to request "Report",
// then select "It's spam", then "Remove tag"
Expand All @@ -491,6 +503,20 @@ func (del *deleter) DeleteElement(elem *deleteElement) {
}

func main() {
flag.IntVar(&rateLimit, "rateLimit", 0, "Wait this many milliseconds between requests.")
flag.BoolVar(&limitSearch, "limitSearch", true, "Rate-limit searching for things to delete.")
flag.BoolVar(&limitDelete, "limitDelete", true, "Rate-limit deleting things.")
flag.Parse()
if rateLimit > 0 {
if limitSearch && limitDelete {
fmt.Printf("Waiting %d ms before search and delete requests.\n", rateLimit)
} else if limitSearch {
fmt.Printf("Waiting %d ms before search requests.\n", rateLimit)
} else if limitDelete {
fmt.Printf("Waiting %d ms before delete requests.\n", rateLimit)
}
}

req := newRequester()
fbl := newFbLogin(req)
actRead := activityReader{req, fbl, make([]deleteElement, 0), make([]string, 0)}
Expand Down

0 comments on commit 3fb7ad6

Please sign in to comment.