Created
August 28, 2013 16:36
-
-
Save chzyer/6368119 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 | |
// open http://localhost:8000 | |
import ( | |
"net/http" | |
"io/ioutil" | |
"regexp" | |
) | |
var RePic = regexp.MustCompile(`"http://pic2.52pk.com/files[^"]+`) | |
func item(w http.ResponseWriter, req *http.Request) { | |
q := req.URL.Query() | |
url := "" | |
for k, _ := range q { | |
url = k | |
break | |
} | |
data := getContent("http://anime.52pk.com/manhua/narutocn/" + url + ".shtml") | |
re := regexp.MustCompile(`"http://anime.52pk.com/manhua/narutocn/` + url + `_\d+.shtml"`) | |
urlsByte := re.FindAllSubmatch(data, -1) | |
if len(urlsByte) <= 0 { return } | |
urls := make([]string, len(urlsByte)) | |
length := 0 | |
for _, u := range urlsByte { | |
uu := string(u[0]) | |
uu = uu[1: len(uu)-1] | |
isSame := false | |
for _, url := range urls { | |
if url == uu { | |
isSame = true | |
break | |
} | |
} | |
if isSame { continue } | |
urls[length] = uu | |
length ++ | |
} | |
urls = urls[:length] | |
w.Header().Set("Content-Type", "text/html;charset=UTF-8") | |
for _, u := range urls { | |
data := getContent(u) | |
url := RePic.Find(data)[1:] | |
w.Write([]byte(`<img src="` + string(url) + `" />`)) | |
w.(http.Flusher).Flush() | |
} | |
re = regexp.MustCompile(`xh"><a href="[^"]+/(\d+)\.shtml`) | |
next := re.FindSubmatch(data) | |
if len(next) <= 0 { | |
w.Write([]byte("<br/>没有下一话了")) | |
} else { | |
w.Write([]byte(`<br><a href="?` + string(next[1]) + `">下一话</a>`)) | |
} | |
} | |
func getContent(path string) []byte { | |
ret, err := http.Get(path) | |
if err != nil { | |
panic(err) | |
} | |
defer ret.Body.Close() | |
body, err := ioutil.ReadAll(ret.Body) | |
if err != nil { panic(err) } | |
return body | |
} | |
func index(w http.ResponseWriter, req *http.Request) { | |
data := getContent("http://manhua.52pk.com/ip/manhua_10.html") | |
data = regexp.MustCompile(`http://anime.52pk.com/manhua/narutocn/(\d+)\.shtml`).FindSubmatch(data)[1] | |
w.Header().Set("Location", "/huoying?" + string(data)) | |
w.WriteHeader(302) | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", index) | |
mux.HandleFunc("/huoying", item) | |
panic(http.ListenAndServe(":8000", mux)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment