Created
June 10, 2011 07:45
-
-
Save remogatto/1018401 to your computer and use it in GitHub Desktop.
WorldOfSpectrum Resource Fetcher
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 ( | |
"path" | |
"regexp" | |
"http" | |
"log" | |
"io/ioutil" | |
"flag" | |
"fmt" | |
"os" | |
) | |
var patterns = []string{"*.tap.zip", "*.sna.zip", "*.z80.zip"} | |
func knownType(filename string) bool { | |
for _, t := range patterns { | |
matched, err := path.Match(t, filename) | |
if err != nil { | |
log.Print(err) | |
} | |
if matched { | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
help := flag.Bool("help", false, "Show usage") | |
flag.Usage = func() { | |
fmt.Fprintf(os.Stderr, "WorldOfSpectrum games fetcher\n\n") | |
fmt.Fprintf(os.Stderr, "Usage:\n\n") | |
fmt.Fprintf(os.Stderr, "\twosfetch regexp\n\n") | |
fmt.Fprintf(os.Stderr, "Options are:\n\n") | |
flag.PrintDefaults() | |
} | |
flag.Parse() | |
if *help == true { | |
flag.Usage() | |
return | |
} | |
re, _ := regexp.Compile("ftp://([a-zA-Z0-9\\-\\/\\.\\?_]+)") | |
client := new(http.Client) | |
query := "http://www.worldofspectrum.org/infoseek.cgi?regexp=" + http.URLEscape(flag.Arg(0)) | |
log.Println("Query string:", query) | |
log.Println("Fetching...") | |
response, err := client.Get(query) | |
if err != nil { | |
log.Println(err) | |
} | |
body, _ := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Println(response.Status) | |
uris := re.FindAllString(string(body), -1) | |
matches := make([]string, 0) | |
for _, uri := range uris { | |
if knownType(path.Base(uri)) { | |
matches = append(matches, uri) | |
log.Printf("[%d] - %s", len(matches), uri) | |
} | |
} | |
log.Printf("Found %d matching resources", len(matches)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ftp.go seems quite useless