Skip to content

Instantly share code, notes, and snippets.

@remogatto
Created June 10, 2011 07:45
Show Gist options
  • Save remogatto/1018401 to your computer and use it in GitHub Desktop.
Save remogatto/1018401 to your computer and use it in GitHub Desktop.
WorldOfSpectrum Resource Fetcher
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))
}
@remogatto
Copy link
Author

Indeed, I'm wondering about that too :) I found this: https://github.com/smallfish/ftp.go
At a first glance, I don't like its API very much. Alternatively, we can use wget but it will work on unix-flavored system only. Or we can write a better ftp client. What do you think?

@remogatto
Copy link
Author

ftp.go seems quite useless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment