Skip to content

Commit

Permalink
file architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
AwesomeYelim committed Jul 5, 2024
1 parent 0abb7b7 commit d7daa2d
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 160 deletions.
28 changes: 28 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"easyPreparation_1.0/internal/lyrics"
"easyPreparation_1.0/internal/presentation"
"fmt"

"github.com/unidoc/unioffice/common/license"
)

func init() {
// UniOffice 라이센스 설정
err := license.SetMeteredKey("468eb71b0f562ed29385b487b55d413ad506b3c48950ead1de75bd736c7c17c4")
if err != nil {
panic(fmt.Sprintf("Failed to set UniOffice license key: %v", err))
}
}

func main() {
// 가사 검색
song := &lyrics.SlideData{}
song.SearchLyricsList("https://music.bugs.co.kr/search/lyrics?q=%s", "하나님은 너를 지키시는자", false)

// 프레젠테이션 생성 및 슬라이드 추가
presentation.CreatePresentation(song, "output.pptx")

fmt.Println("Presentation saved to output.pptx")
}
49 changes: 49 additions & 0 deletions internal/lyrics/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lyrics

import (
"easyPreparation_1.0/pkg"
"log"
"strconv"
"strings"

"github.com/PuerkitoBio/goquery"
)

// SlideData 구조체는 슬라이드에 포함될 데이터를 나타냅니다.
type SlideData struct {
Title string
Content []string
TrackID int
}

// parseTrackList 함수는 트랙 리스트를 파싱합니다.
func (si *SlideData) parseTrackList(doc *goquery.Document) {
doc.Find("table.trackList tbody tr[rowtype='lyrics']").Each(func(i int, s *goquery.Selection) {
albumID, exists := s.Attr("trackid")
if exists {
tempNo, err := strconv.ParseInt(albumID, 10, 64)
if err != nil {
log.Fatalf("Failed to parse track ID: %v", err)
}
si.TrackID = int(tempNo)
si.SearchLyricsList("https://music.bugs.co.kr/track/%s", albumID, true)
}
})
}

// parseLyrics 함수는 가사를 파싱합니다.
func (si *SlideData) parseLyrics(doc *goquery.Document) {
doc.Find(".lyricsContainer xmp").Each(func(i int, s *goquery.Selection) {
// 공백 제거
trimmedText := pkg.RemoveEmptyLines(s.Text())
// 두 줄씩 자르기
lines := strings.Split(trimmedText, "\n")
for i := 0; i < len(lines); i += 2 {
if i+1 < len(lines) {
si.Content = append(si.Content, lines[i]+"\n"+lines[i+1])
} else {
si.Content = append(si.Content, lines[i]) // 마지막줄 홀수 인경우에만
}
}
})
}
50 changes: 50 additions & 0 deletions internal/lyrics/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package lyrics

import (
"fmt"
"log"
"net/http"
"net/url"

"github.com/PuerkitoBio/goquery"
)

// searchLyricsList 함수는 가사 목록을 검색합니다.
func (si *SlideData) SearchLyricsList(baseUrl, query string, isDirect bool) {
if len(si.Content) > 0 {
return
}
searchUrl := formatSearchURL(baseUrl, query, isDirect)

// HTTP 요청 보내기
resp, err := http.Get(searchUrl)
if err != nil {
log.Fatalf("Failed to make HTTP request: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Fatalf("Status code error: %d %s", resp.StatusCode, resp.Status)
}

// HTML 파싱
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatalf("Failed to parse HTML: %v", err)
}

if isDirect {
si.parseLyrics(doc)
} else {
si.parseTrackList(doc)
}
}

// formatSearchURL 함수는 검색 URL을 생성합니다.
func formatSearchURL(baseUrl, query string, isDirect bool) string {
if isDirect {
return fmt.Sprintf(baseUrl, query)
}
searchQuery := url.QueryEscape(query)
return fmt.Sprintf(baseUrl, searchQuery)
}
36 changes: 36 additions & 0 deletions internal/presentation/presentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package presentation

import (
"easyPreparation_1.0/internal/lyrics"
"github.com/unidoc/unioffice/presentation"
"log"
)

// CreatePresentation 함수는 프레젠테이션을 생성하고 슬라이드를 추가합니다.
func CreatePresentation(slidesData *lyrics.SlideData, filePath string) {
ppt := presentation.New()
defer ppt.Close()

for _, content := range slidesData.Content {
slide := ppt.AddSlide()
// 제목 설정
titleBox := slide.AddTextBox()
titlePara := titleBox.AddParagraph()
titleRun := titlePara.AddRun()
titleRun.SetText(slidesData.Title)
titleBox.Properties().SetPosition(50, 50)
titleBox.Properties().SetSize(50, 50)

// 내용 설정
contentBox := slide.AddTextBox()
contentPara := contentBox.AddParagraph()
contentRun := contentPara.AddRun()
contentRun.SetText(content)
contentBox.Properties().SetPosition(50, 50)
contentBox.Properties().SetSize(600, 400)
}

if err := ppt.SaveToFile(filePath); err != nil {
log.Fatalf("Error saving presentation: %v", err)
}
}
160 changes: 0 additions & 160 deletions main.go

This file was deleted.

Binary file modified output.pptx
Binary file not shown.
15 changes: 15 additions & 0 deletions pkg/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pkg

import "strings"

// RemoveEmptyLines 함수는 중간 공백을 제거합니다.
func RemoveEmptyLines(text string) string {
lines := strings.Split(text, "\n")
var result []string
for _, line := range lines {
if strings.TrimSpace(line) != "" {
result = append(result, strings.TrimSpace(line))
}
}
return strings.Join(result, "\n")
}

0 comments on commit d7daa2d

Please sign in to comment.