Created
April 22, 2018 14:32
-
-
Save kestein/4dcf30ffd246c59f3a9dff7d8cbcdaec to your computer and use it in GitHub Desktop.
Launch a thread with the specified subject on any 4chan board
This file contains 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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
"strings" | |
) | |
const baseURL = "https://boards.4chan.org" | |
func main() { | |
cliFlags := os.Args[1:] | |
var board, topic string | |
if len(cliFlags) < 2 { | |
board = "g" | |
topic = "dpt" | |
} else { | |
board = cliFlags[0] | |
topic = cliFlags[1] | |
} | |
boardURL := fmt.Sprintf("%s/%s", baseURL, board) | |
boardCatalog := fmt.Sprintf("%s/catalog.json", boardURL) | |
resp, err := http.Get(boardCatalog) | |
checkErr(err, fmt.Sprintf("Could not GET %s catalog JSON", board)) | |
defer resp.Body.Close() | |
catalogJson, err := ioutil.ReadAll(resp.Body) | |
checkErr(err, "Could not read response body") | |
var threads []map[string]interface{} | |
checkErr(json.Unmarshal([]byte(catalogJson), &threads), "Could not unmarshall json") | |
threadNum := findThread(threads, &topic) | |
if threadNum > 0 { | |
// print out a URL to feed into chrome | |
_ = exec.Command("google-chrome", fmt.Sprintf("%s/thread/%d", boardURL, threadNum)).Start() | |
} else { | |
log.Println("Could not find topic. Check catalog") | |
_ = exec.Command("google-chrome", fmt.Sprintf("%s/%s", boardURL, topic)).Start() | |
} | |
} | |
func findThread(threads []map[string]interface{}, topic *string) int { | |
for _, page := range threads { | |
threads := page["threads"].([]interface{}) | |
for _, p := range threads { | |
post := p.(map[string]interface{}) | |
sub, exists := post["sub"] | |
if !exists { | |
continue | |
} | |
if strings.Contains(strings.ToLower(sub.(string)), *topic) { | |
return int(post["no"].(float64)) | |
} | |
} | |
} | |
return -1 | |
} | |
func checkErr(err error, message string) { | |
if err != nil { | |
log.Fatal(message, " ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: 4chan <board> <subject>