Created
March 5, 2022 09:15
-
-
Save gangjun06/d457f7e8922ab4916884278601ec738b to your computer and use it in GitHub Desktop.
클래스카드의 단어장을 Anki단어장으로 변환하여 줍니다.
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 ( | |
"bufio" | |
"encoding/csv" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
uuid "github.com/satori/go.uuid" | |
) | |
const ( | |
ID = 583647 // https://www.classcard.net/set/<이부분의 숫자를 이 변수에 넣으세요.> | |
) | |
func main() { | |
targetURL := fmt.Sprintf("https://www.classcard.net/set/%d", ID) | |
fmt.Println("Parsing from:", targetURL) | |
resp, err := http.Get(targetURL) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != 200 { | |
log.Fatalf("status code error: %d %s", resp.StatusCode, resp.Status) | |
} | |
doc, err := goquery.NewDocumentFromReader(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
file, err := os.Create("./output.csv") | |
if err != nil { | |
log.Fatal(err) | |
} | |
wr := csv.NewWriter(bufio.NewWriter(file)) | |
newpath := filepath.Join(".", "audio") | |
_ = os.MkdirAll(newpath, os.ModePerm) | |
title := "C-" + strings.ReplaceAll(strings.TrimSpace(doc.Find(".m-t-sm.font-32.cc-ellipsis.l2").Text()), " ", "-") | |
backParser := regexp.MustCompile(`(<div class="text-left">|</div>|\n|\t)`) | |
doc.Find(".flip-card > .flip-card-inner").Each(func(i int, s *goquery.Selection) { | |
cardFront := s.Find(".flip-card-front") | |
front := strings.TrimSpace(cardFront.Find(".card-content .font-bold").Text()) | |
audio, _ := cardFront.Find(".btn-audio").Attr("data-src") | |
backHTML, _ := goquery.OuterHtml(s.Find(".flip-card-back > .card-content > .fill-parent > div")) | |
back := backParser.ReplaceAllString(backHTML, "") | |
name := "classcard-" + uuid.NewV4().String() | |
Download(name, audio) | |
audioText := fmt.Sprintf("[sound:%s.mp3]", name) | |
fmt.Printf("Add: %s (%s) %s \n", front, back, audio) | |
wr.Write([]string{front, back, audioText, title}) | |
}) | |
wr.Flush() | |
} | |
func Download(name, url string) { | |
resp, err := http.Get(url) | |
if err != nil { | |
fmt.Println("Err:", err.Error()) | |
return | |
} | |
defer resp.Body.Close() | |
out, err := os.Create(fmt.Sprintf("audio/%s.mp3", name)) | |
if err != nil { | |
fmt.Println("Err:", err.Error()) | |
return | |
} | |
defer out.Close() | |
if _, err = io.Copy(out, resp.Body); err != nil { | |
fmt.Println("Err:", err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment