entry.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "container/list"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "os/exec"
  10. "strings"
  11. "./crawler"
  12. )
  13. // ListPagesComics ...
  14. func ListPagesComics(url string) *list.List {
  15. var list = list.New()
  16. var crawl = crawler.New(url)
  17. listUrls := crawl.ListLink()
  18. for elem := listUrls.Front(); elem != nil; elem = elem.Next() {
  19. image := elem.Value.(string)
  20. if strings.Contains(image, ".jpg") {
  21. file := fmt.Sprintf("%s%s", url, image)
  22. list.PushBack(file)
  23. }
  24. }
  25. return list
  26. }
  27. // DownloadFile ...
  28. func DownloadFile(url string, filepath string) error {
  29. out, err := os.Create(filepath)
  30. if err != nil {
  31. return err
  32. }
  33. defer out.Close()
  34. // Get the data
  35. resp, err := http.Get(url)
  36. if err != nil {
  37. return err
  38. }
  39. if resp.StatusCode == 404 || resp.ContentLength == -1 {
  40. return errors.New("File not found.")
  41. }
  42. defer resp.Body.Close()
  43. // Write the body to file
  44. _, err = io.Copy(out, resp.Body)
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. func Exists(name string) bool {
  51. if _, err := os.Stat(name); err != nil {
  52. if os.IsNotExist(err) {
  53. return false
  54. }
  55. }
  56. return true
  57. }
  58. func main() {
  59. path := "./dr-stone"
  60. pathFile7z := fmt.Sprintf("%s%s", path, "/dr-stone-t")
  61. for i := 132; i <= 160; i++ {
  62. folder := fmt.Sprintf("%s/T%d", path, i)
  63. var page = 0
  64. notErr := true
  65. for notErr {
  66. url := fmt.Sprintf("http://lelscano.com//mangas/dr-stone/%d/", i)
  67. if page < 10 {
  68. url = fmt.Sprintf(url+"0%d.jpg", page)
  69. } else {
  70. url = fmt.Sprintf(url+"%d.jpg", page)
  71. }
  72. fmt.Println(url)
  73. if Exists(folder) == false {
  74. os.MkdirAll(folder, os.ModePerm)
  75. }
  76. file := fmt.Sprintf("%s/ep%d.jpg", folder, page)
  77. if Exists(file) == false {
  78. err := DownloadFile(url, file)
  79. if err != nil {
  80. fmt.Println(err)
  81. os.Remove(file)
  82. notErr = false
  83. } else {
  84. fmt.Println("", folder, " => ", page)
  85. page++
  86. }
  87. }
  88. }
  89. fileCompress := fmt.Sprintf("%s%d.cbz", pathFile7z, i)
  90. cmd := exec.Command("7z", "a", fileCompress, folder)
  91. err := cmd.Run()
  92. if err != nil {
  93. fmt.Println(err)
  94. }
  95. }
  96. }