Last active
January 17, 2023 09:52
-
-
Save random-robbie/51cf26bdbb782b5e8178a49b01421b37 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"regexp" | |
"strings" | |
) | |
func main() { | |
url := "https://themes.svn.wordpress.org" | |
response, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer response.Body.Close() | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Use regular expression to match only alpha-numeric directories with _ or - | |
re := regexp.MustCompile(`(?m)[A-Za-z0-9_-]+/`) | |
matches := re.FindAllString(string(body), -1) | |
// Remove / from the end of each directory name | |
for i := range matches { | |
matches[i] = strings.TrimSuffix(matches[i], "/") | |
} | |
// Filter out directories that are not within the desired length | |
var filteredMatches []string | |
for _, theme := range matches { | |
if len(theme) >= 3 && len(theme) <= 32 { | |
filteredMatches = append(filteredMatches, theme) | |
} | |
} | |
// Remove duplicates | |
filteredMatches = removeDuplicates(filteredMatches) | |
// Open the file in append mode | |
file, err := os.OpenFile("wordpress-themes.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
for _, theme := range filteredMatches { | |
fmt.Println(theme) | |
_, err := file.WriteString(""+theme+"") | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
} | |
// Function to remove duplicates from a slice of strings | |
func removeDuplicates(elements []string) []string { | |
// Use map to record duplicates as we find them. | |
encountered := map[string]bool{} | |
result := []string{} | |
for v := range elements { | |
if encountered[elements[v]] != true { | |
// Record this element as an encountered element. | |
encountered[elements[v]] = true | |
// Append to result slice. | |
result = append(result, elements[v]) | |
} | |
} | |
// Return the new slice. | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment