Created
July 10, 2026 14:25
-
-
Save sankalp-khare/7b82d7b10ce6305b7f53b828332cbb04 to your computer and use it in GitHub Desktop.
Update tracker of torrent files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // iterates over the torrents in ./Torrents | |
| // and updates the tracker of all torrents (top-level, not recursive) | |
| // put this in a directory, do a | |
| // `go mod init <whatever>` and `go mod tidy` to get dependencies | |
| // update "foo" "bar" "baz" to the desired values & | |
| // go run . | |
| import ( | |
| "fmt" | |
| "os" | |
| "path/filepath" | |
| "github.com/anacrolix/torrent/bencode" | |
| ) | |
| type TorrentFile struct { | |
| Announce string `bencode:"announce"` | |
| Info map[string]interface{} `bencode:"info"` | |
| } | |
| func main() { | |
| // iterate on all .torrent files in ./Torrents (symlink as needed) | |
| pattern := filepath.Join("./Torrents", "*.torrent") | |
| matches, err := filepath.Glob(pattern) | |
| if err != nil { | |
| fmt.Printf("Pattern error: %v\n", err) | |
| return | |
| } | |
| for _, path := range matches { | |
| // 1. Read the .torrent file | |
| data, err := os.ReadFile(path) | |
| if err != nil { | |
| fmt.Println("Error reading file:", err) | |
| return | |
| } | |
| // 2. Unmarshal contents | |
| var tf TorrentFile | |
| err = bencode.Unmarshal(data, &tf) | |
| if err != nil { | |
| fmt.Println("Error decoding torrent:", err) | |
| return | |
| } | |
| // Print Announce URL | |
| fmt.Println("Announce URL:", tf.Announce) | |
| // 3. Conditionally Update (comment this block if you just want to print the Announce URLs) | |
| if tf.Announce == "foo" || tf.Announce == "bar" { | |
| tf.Announce = "baz" | |
| newData, err := bencode.Marshal(tf) | |
| if err != nil { | |
| fmt.Println("Error encoding updated torrent:", err) | |
| return | |
| } | |
| err = os.WriteFile(path, newData, 0644) | |
| if err != nil { | |
| fmt.Println("Error writing torrent file:", err) | |
| } | |
| fmt.Println(">> Updated Announce URL:", tf.Announce) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment