Skip to content

Commit

Permalink
Merge pull request #29 from tomoish/main3
Browse files Browse the repository at this point in the history
mergeAll
  • Loading branch information
tomoish authored Feb 2, 2024
2 parents 8024525 + 84f77a6 commit 7fab1f0
Show file tree
Hide file tree
Showing 28 changed files with 405 additions and 67 deletions.
Binary file added src/NotoSansJP-Black.ttf
Binary file not shown.
Binary file added src/NotoSansJP-Bold.ttf
Binary file not shown.
Binary file added src/NotoSansJP-ExtraLight.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/funcs/create_character_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CreateCharacterImg() {
}

// 画像をファイルに保存
err = SaveImage("images/merged.png", mergedImage)
err = SaveImage("./images/generate_character.png", mergedImage)
if err != nil {
fmt.Println(err)
return
Expand Down
4 changes: 2 additions & 2 deletions src/funcs/create_language_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func CreateLanguageImg() {

fmt.Printf("languages: %v\n", languages)

ImgBytes, _ := GenerateLanguageUsageGraph(languages, 600, 250)
ImgBytes, _ := GenerateLanguageUsageGraph(languages, 600, 400)

// 画像をファイルに保存
err = SaveImage("images/language.png", ImgBytes)
err = SaveImage("./images/language.png", ImgBytes)
if err != nil {
fmt.Println(err)
}
Expand Down
68 changes: 68 additions & 0 deletions src/funcs/draw_background.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package funcs

import (
"fmt"
"image/color"
"log"

"github.com/fogleman/gg"
)

func DrawBackground(level, kind string) {
const (
// Width = 1200
// Height = 1800
Width = 800
Height = 1200
BorderSize = 10 // ボーダーのサイズ
)

// 新しい画像コンテキストを作成
dc := gg.NewContext(Width, Height)

// 画像の背景を白に設定
dc.SetRGB(0.03, 0.03, 0.03)
dc.Clear()

// テキストの描画設定
// フォントの設定

// 関数呼び出しとエラーチェック
if err := dc.LoadFontFace("Roboto-Medium.ttf", 28); err != nil {
fmt.Println("フォントのロードに失敗しました:", err)
}
dc.SetColor(color.White)

// テキストの座標を個別に指定して描画
text1 := "Hi, there! I'm Kota"
x1, y1 := (25*Width)/100, (15*Height)/100 // テキスト1の座標
dc.DrawStringAnchored(text1, float64(x1), float64(y1), 0.5, 0.5)

text2 := level
x2, y2 := (18*Width)/100, (25*Height)/100 // テキスト2の座標(テキストの中央に持つ)
dc.DrawStringAnchored(text2, float64(x2), float64(y2), 0.5, 0.5)

if err := dc.LoadFontFace("NotoSansJP-ExtraLight.ttf", 50); err != nil {
fmt.Println("フォントのロードに失敗しました:", err)
}
text3 := kind
x3, y3 := (33*Width)/100, (25*Height)/100 // テキスト3の座標
dc.DrawStringAnchored(text3, float64(x3), float64(y3), 0.5, 0.5)

// ボーダーを描画する新しいコンテキストを作成
dcBorder := gg.NewContext(Width, Height)
dcBorder.SetRGB(1, 1, 1) // 白いボーダー
dcBorder.Clear()
dcBorder.DrawRectangle(0, 0, float64(Width), float64(Height))
dcBorder.SetLineWidth(BorderSize)
dcBorder.Stroke()

// ボーダーの上に背景画像を描画
dcBorder.DrawImage(dc.Image(), 0, 0)
// 画像をPNG形式で保存
if err := dc.SavePNG("./images/background.png"); err != nil {
log.Fatal(err)
}

log.Println("Image with multiple text has been created and saved.")
}
14 changes: 7 additions & 7 deletions src/funcs/draw_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func GenerateLanguageUsageGraph(languages []LanguageStat, width, height int) ([]
dc.SetRGB(0.2, 0.24, 0.31) // 背景色(暗い青灰色)
drawRoundedRectangle(dc, 0, 0, float64(width), float64(height), cornerRadius)
dc.SetRGB(1, 1, 1)
err := dc.LoadFontFace("Roboto-Medium.ttf", 30) // フォントとサイズの設定が必要
err := dc.LoadFontFace("Roboto-Medium.ttf", 45) // フォントとサイズの設定が必要
if err != nil {
fmt.Println(err)
}
dc.DrawStringAnchored("Most Used Languages", float64(width)/2, 30, 0.5, 0.5)
dc.DrawStringAnchored("Most Used Languages", float64(width)/2, 50, 0.5, 0.5)

// 帯グラフの基準点
barX := 40.0
barY := 60.0
barY := 100.0
barHeight := 10.0
barWidth := float64(width) - 80.0

Expand Down Expand Up @@ -88,8 +88,8 @@ func GenerateLanguageUsageGraph(languages []LanguageStat, width, height int) ([]

// 凡例の描画
legendX := 40.0
legendY := barY + barHeight + 20.0 // 帯グラフの下に余白をとる
err = dc.LoadFontFace("Roboto-Medium.ttf", 25) // 凡例のフォントサイズ
legendY := barY + barHeight + 40.0 // 帯グラフの下に余白をとる
err = dc.LoadFontFace("Roboto-Medium.ttf", 28) // 凡例のフォントサイズ
if err != nil {
fmt.Println(err)
}
Expand All @@ -98,12 +98,12 @@ func GenerateLanguageUsageGraph(languages []LanguageStat, width, height int) ([]
for _, lang := range newLanguages {
// 色のサンプルを描画
dc.SetHexColor(lang.Color)
dc.DrawCircle(legendX+5.0+math.Mod(float64(int(i)), 2.0)*300.0, legendY+6, 5)
dc.DrawCircle(legendX+5.0+math.Mod(float64(int(i)), 2.0)*300.0, legendY+30, 5)
dc.Fill()

// 言語名と割合を描画
dc.SetRGB(1, 1, 1)
dc.DrawString(fmt.Sprintf("%s %.2f%%", lang.Name, lang.Percent), legendX+20.0+math.Mod(float64(int(i)), 2.0)*300.0, legendY+15)
dc.DrawString(fmt.Sprintf("%s %.2f%%", lang.Name, lang.Percent), legendX+20.0+math.Mod(float64(int(i)), 2.0)*300.0, legendY+45)
i += 1.0
legendY = legendY + math.Mod(float64(int(i)+1), 2.0)*40.0

Expand Down
39 changes: 24 additions & 15 deletions src/funcs/draw_user_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,48 @@ import (

// GenerateGitHubStatsImage 関数はユーザー統計情報を受け取り、グラフィカルな表現を画像として生成します。
func GenerateGitHubStatsImage(stats UserStats, width, height int) ([]byte, error) {
const padding = 20.0
const lineHeight = 30.0
// const padding = 20.0
// const lineHeight = 30.0

dc := gg.NewContext(width, height)

// 背景色を設定します。
dc.SetRGB(0.2, 0.24, 0.31)
dc.SetRGB(0.2, 0.2, 0.2)
dc.Clear()

// タイトルを描画します。
dc.SetRGB(1, 1, 1) // 白色
dc.DrawStringAnchored("@hashfx-Github-stats", float64(width)/2, padding, 0.5, 0.5)
dc.SetRGB(0, 0.749, 1)
if err := dc.LoadFontFace("Roboto-Medium.ttf", 50); err != nil {
return nil, err
}
dc.DrawStringAnchored("Github-Stats", float64(width)/2, 10*float64(height)/100, 0.5, 0.5)

// 統計情報をリストとして描画します。
statsList := []struct {
Icon string
Label string
Value int
}{
{"★", "Total Stars Earned:", stats.TotalStars},
{"⧗", "Total Commits:", stats.TotalCommits},
{"⬆️", "Total PRs:", stats.TotalPRs},
{"⬇️", "Total Issues:", stats.TotalIssues},
{"⬈", "Contributed to:", stats.ContributedTo},
{"Total Stars Earned:", stats.TotalStars},
{"Total Commits:", stats.TotalCommits},
{"Total PRs:", stats.TotalPRs},
{"Total Issues:", stats.TotalIssues},
{"Contributed to:", stats.ContributedTo},
}

dc.SetRGB(1, 1, 1) // 白色

// 関数呼び出しとエラーチェック
if err := dc.LoadFontFace("Roboto-Medium.ttf", 30); err != nil {
return nil, err
}

// 各統計情報をリスト形式で描画します。
for i, stat := range statsList {
y := padding*2 + lineHeight*float64(i)
dc.DrawStringAnchored(stat.Icon, padding, y, 0, 0.5)
dc.DrawStringAnchored(stat.Label+" "+strconv.Itoa(stat.Value), padding+40, y, 0, 0.5)
y := 0.3 + 0.15*float64(i) // 垂直位置を計算
valueX := 0.7 * float64(width) // 値を右揃えにするためのX座標
dc.DrawString(stat.Label, 0.1*float64(width), y*float64(height))
dc.DrawString(strconv.Itoa(stat.Value), valueX, y*float64(height))
}

// 画像をバッファにエンコードします。
buf := bytes.Buffer{}
err := dc.EncodePNG(&buf)
Expand Down
1 change: 0 additions & 1 deletion src/funcs/get_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func FetchDataInTimeRange(token string, username string) (int, int, int, int, in
return 0, 0, 0, 0, 0, err
}
req.Header.Set("Authorization", "Bearer "+token)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions src/funcs/get_commit_streak.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"fmt"
"log"
"net/http"
"os"

"github.com/joho/godotenv"
)

const query_frame = `
Expand Down Expand Up @@ -68,10 +65,10 @@ func calculateStreak(weeks []struct {
}

func GetCommitHistory(username string) (int, []int, int, error) {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
// err := godotenv.Load()
// if err != nil {
// log.Fatalf("Error loading .env file")
// }
query := fmt.Sprintf(query_frame, username)

request := GraphQLQuery{Query: query}
Expand All @@ -86,7 +83,9 @@ func GetCommitHistory(username string) (int, []int, int, error) {
log.Fatalf("GitHub GraphQL API request failed: %v", err)
}

req.Header.Set("Authorization", "bearer "+os.Getenv("GITHUB_TOKEN"))
token, _ := GetTokens(0)

req.Header.Set("Authorization", "bearer "+token)

client := &http.Client{}
resp, _ := client.Do(req)
Expand Down
13 changes: 7 additions & 6 deletions src/funcs/get_user_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type UserStats struct {
}

// GetUserInfo は指定されたユーザー名に対する統計情報を取得します。
func GetUserInfo(username, token string) (UserStats, error) {
func GetUserInfo(username, token string) UserStats {
userStats := UserStats{}

// GraphQLクエリをbytes:\xe5\xaebytes:\x9f行し、bytes:\xe7\xb5bytes:\x90果を取得
query := `
{
query :=
`{
user(login: "` + username + `") {
contributionsCollection {
totalCommitContributions
Expand All @@ -36,9 +36,10 @@ func GetUserInfo(username, token string) (UserStats, error) {
}
}
}`

data, err := executeQuery(query, token)
if err != nil {
return userStats, fmt.Errorf("query execution failed: %w", err)
return userStats
}

//bytes: \xe3\x83bytes:\xacスポンスデbytes:\xe3\x83\xbc\xe3\x82bytes:\xbfのbytes:\xe6bytes:\xa7bytes:\x8b造を定bytes:\xe7\xbebytes:\xa9
Expand All @@ -61,7 +62,7 @@ func GetUserInfo(username, token string) (UserStats, error) {
//bytes: \xe3\x83bytes:\xacスポンスデbytes:\xe3\x83\xbc\xe3\x82bytes:\xbfを解析
var response Response
if err := json.Unmarshal(data, &response); err != nil {
return userStats, fmt.Errorf("failed to unmarshal response data: %w", err)
return userStats
}

// UserStatsbytes: \xe6bytes:\xa7bytes:\x8b造体にbytes:\xe7\xb5bytes:\xb1計情報を格bytes:\xe7\xb4bytes:\x8d
Expand All @@ -71,7 +72,7 @@ func GetUserInfo(username, token string) (UserStats, error) {
userStats.TotalPRs = response.Data.User.ContributionsCollection.TotalPullRequestContributions
userStats.ContributedTo = response.Data.User.ContributionsCollection.TotalRepositoryContributions

return userStats, nil
return userStats
}

// executeQuery は指定されたGraphQLクエリをbytes:\xe5\xaebytes:\x9f行し、bytes:\xe7\xb5bytes:\x90果をバイト配列でbytes:\xe8\xbfbytes:\x94します。
Expand Down
Binary file removed src/funcs/images/character.png
Binary file not shown.
Binary file removed src/funcs/images/gauge.png
Binary file not shown.
Binary file removed src/funcs/images/language.png
Binary file not shown.
Binary file removed src/funcs/images/merged.png
Binary file not shown.
Loading

0 comments on commit 7fab1f0

Please sign in to comment.