Skip to content

Commit

Permalink
Cache purge integration (#6)
Browse files Browse the repository at this point in the history
* Initial conversion, probably doesnt work

* python conversion

* delete the exe, we have Releases now!
  • Loading branch information
amidatelion authored May 30, 2023
1 parent 2fd42d0 commit b4dd7b3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Binary file removed bta-wiki-import.exe
Binary file not shown.
1 change: 1 addition & 0 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ func Run() {
RootCmd.AddCommand(ParseCmd)
RootCmd.AddCommand(ImportCmd)
RootCmd.AddCommand(LintCmd)
RootCmd.AddCommand(PurgeCmd)
RootCmd.Execute()
}
94 changes: 94 additions & 0 deletions cmd/cache-purge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cmd

import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"

"github.com/spf13/cobra"
)

var (
flagApiURL string
flagListURL string
)

var PurgeCmd = &cobra.Command{
Use: "cache-purge",
Short: "pull the list of current mechs and run a cache purge against them",
Run: func(cmd *cobra.Command, args []string) {

// API URL for the MediaWiki API
apiURL := flagApiURL
// URL for the text file containing page titles
listURL := flagListURL

// Fetching the text file from the URL
response, err := http.Get(listURL)
if err != nil {
fmt.Println("Failed to fetch the text file:", err)
return
}
defer response.Body.Close()

// Splitting the text file into lines and iterating over each line
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}

params := url.Values{}
params.Set("action", "purge")
params.Set("titles", line)
params.Set("format", "json")

req, err := http.NewRequest("POST", apiURL, strings.NewReader(params.Encode()))
if err != nil {
fmt.Println("Failed to create the POST request:", err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failed to send the POST request:", err)
return
}
defer resp.Body.Close()

// Printing the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Failed to read the response body:", err)
return
}
fmt.Println(string(body))

// Sleeping for 10 seconds
time.Sleep(10 * time.Second)
}

if err := scanner.Err(); err != nil {
fmt.Println("Failed to read the text file:", err)
return
}
},
}

func init() {
PurgeCmd.Flags().StringVarP(
&flagApiURL, "url", "u", "",
"the wiki URL to execute the cache purge against. Expects https://WEBSITE/api.php",
)
PurgeCmd.Flags().StringVarP(
&flagListURL, "list", "l", "",
"the new-line delineated list of pages. Expects https://WEBSITE/files/list_of_some_kind.txt",
)
}

0 comments on commit b4dd7b3

Please sign in to comment.