Skip to content

Commit

Permalink
Merge pull request #25 from tomoish/main3
Browse files Browse the repository at this point in the history
Main3
  • Loading branch information
tomoish authored Feb 1, 2024
2 parents 211069e + 8ca8e8d commit b119fb7
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
package main
package funcs

import (
"fmt"

"github.com/tomoish/readme/funcs"
)

func CreateCharacterImg() {
// キャラクター画像のパス
characterPath := "funcs/images/character.png"
characterPath := "images/character.png"
// ゲージ画像のパス
gaugePath := "funcs/images/gauge.png"
gaugePath := "images/gauge.png"

// ゲージ画像生成のためのチャネル
gaugeImageChan := make(chan []byte)

// ゲージ画像を非同期で生成
go func() {
GaugeBytes, _ := funcs.DrawGauge(0.5)
GaugeBytes, _ := DrawGauge(0.5)
gaugeImageChan <- GaugeBytes
}()

// ゲージ画像をファイルに保存
gaugeBytes := <-gaugeImageChan
err := funcs.SaveImage(gaugePath, gaugeBytes)
err := SaveImage(gaugePath, gaugeBytes)
if err != nil {
fmt.Println(err)
return
}

// キャラクター画像とゲージ画像を合成
mergedImage, err := funcs.MergeImages(characterPath, gaugePath)
mergedImage, err := MergeImages(characterPath, gaugePath)
if err != nil {
fmt.Println(err)
return
}

// 画像をファイルに保存
err = funcs.SaveImage("funcs/images/merged.png", mergedImage)
err = SaveImage("images/merged.png", mergedImage)
if err != nil {
fmt.Println(err)
return
Expand Down
24 changes: 12 additions & 12 deletions src/create_language_img.go → src/funcs/create_language_img.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package main
package funcs

import (
"fmt"
"time"

"github.com/patrickmn/go-cache"

"github.com/tomoish/readme/funcs"
)

func CreateLanguageImg() {

// 言語ごとの色をここで決める
colordict := map[string]string{
"HTML": "#E34F26",
"CSS": "#ffffff",
Expand All @@ -30,7 +28,7 @@ func CreateLanguageImg() {
// キャッシュにデータが存在しない場合のみデータを保存
if _, found := c.Get(key); !found {
// トークンを取得する
token, newCachedData = funcs.GetTokens(0)
token, newCachedData = GetTokens(0)

c.Set(key, newCachedData, cache.DefaultExpiration)
} else {
Expand All @@ -42,7 +40,7 @@ func CreateLanguageImg() {
fmt.Println("cachedIntData")
fmt.Println(cachedIntData)
// トークンを取得する
token, newCachedData = funcs.GetTokens(cachedIntData)
token, newCachedData = GetTokens(cachedIntData)

c.Set(key, newCachedData, cache.DefaultExpiration)

Expand All @@ -51,7 +49,7 @@ func CreateLanguageImg() {

// ユーザーのリポジトリ情報を取得
username := "kou7306"
repos, err := funcs.GetRepositories(username, token)
repos, err := GetRepositories(username, token)
fmt.Printf("repos: %v\n", repos)
if err != nil {
fmt.Println(err)
Expand All @@ -64,7 +62,7 @@ func CreateLanguageImg() {

// 各リポジトリの言語別のファイルサイズを取得
for _, repo := range repos {
repoDetails, totalSize, err := funcs.GetRepositoryLanguage(repo.Name, repo.Owner, token)
repoDetails, totalSize, err := GetRepositoryLanguage(repo.Name, repo.Owner, token)

if err != nil {
// エラーハンドリング
Expand All @@ -82,7 +80,7 @@ func CreateLanguageImg() {
}
colorCode := ""

languages := []funcs.LanguageStat{}
languages := []LanguageStat{}
// 各言語のファイルサイズをパーセンテージで計算
for language, size := range totalLanguageSize {
percentage := float64(size) / allSize * 100.0
Expand All @@ -96,7 +94,7 @@ func CreateLanguageImg() {
colorCode = colordict["others"]
}

languages = append(languages, funcs.LanguageStat{
languages = append(languages, LanguageStat{
Name: language,
Percent: percentage,
Color: colorCode,
Expand All @@ -105,14 +103,16 @@ func CreateLanguageImg() {

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

ImgBytes, _ := funcs.GenerateLanguageUsageGraph(languages, 600, 250)
ImgBytes, _ := GenerateLanguageUsageGraph(languages, 600, 250)

// 画像をファイルに保存
err = funcs.SaveImage("funcs/images/language.png", ImgBytes)
err = SaveImage("images/language.png", ImgBytes)
if err != nil {
fmt.Println(err)
}

// return languages

}

// func generateSessionKey() string {
Expand Down
115 changes: 115 additions & 0 deletions src/funcs/get_activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package funcs

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

// / GraphQLQuery はGraphQLのクエリを格納するための構造体です
type GraphQLQuery struct {
Query string `json:"query"`
}

// GitHubのGraphQLクエリを定義
type response struct {
Data struct {
User struct {
ContributionsCollection struct {
TotalCommitContributions int `graphql:"totalCommitContributions"`
TotalIssueContributions int `graphql:"totalIssueContributions"`
TotalPullRequestContributions int `graphql:"totalPullRequestContributions"`
TotalRepositoryContributions int `graphql:"totalRepositoryContributions"`
} `graphql:"contributionsCollection"`
StarredRepositories struct {
TotalCount int `graphql:"totalCount"`
} `graphql:"starredRepositories"`
} `graphql:"user(login: $username)"`
}
}

func FetchDataInTimeRange(token string, username string) (int, int, int, int, int, error) {
const query_frame = `
{
user(login: "%s") {
contributionsCollection {
totalCommitContributions
totalIssueContributions
totalPullRequestContributions
totalRepositoryContributions
}
starredRepositories {
totalCount
}
}
}
`

// GraphQLクエリを構築
query := fmt.Sprintf(query_frame, username)

// GraphQLクエリを実行して詳細情報を取得
request := GraphQLQuery{Query: query}
requestBody, err := json.Marshal(request)
if err != nil {
return 0, 0, 0, 0, 0, err
}

url := "https://api.github.com/graphql"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return 0, 0, 0, 0, 0, err
}
req.Header.Set("Authorization", "Bearer "+token)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, 0, 0, 0, 0, err
}
defer resp.Body.Close()

var response response
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return 0, 0, 0, 0, 0, err
}

// 総コントリビューション数
totalCommitContributions := response.Data.User.ContributionsCollection.TotalCommitContributions
// 総スター数
totalStarredRepositories := response.Data.User.StarredRepositories.TotalCount
// 総Issue数
totalIssueContributions := response.Data.User.ContributionsCollection.TotalIssueContributions
// 総PullRequest数
totalPullRequestContributions := response.Data.User.ContributionsCollection.TotalPullRequestContributions
// 総コミット数
totalRepositoryContributions := response.Data.User.ContributionsCollection.TotalRepositoryContributions

return totalCommitContributions, totalStarredRepositories, totalIssueContributions, totalPullRequestContributions, totalRepositoryContributions, nil
}

// func main() {
// // ユーザー名
// username := "kou7306"

// // 期間を指定
// // from := time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC)
// // to := time.Date(2024, 2, 1, 23, 59, 59, 0, time.UTC)

// // GitHubのアクセストークンを設定
// token,_ :=funcs.GetTokens(0)

// // データを取得
// totalCommitContributions, totalStarredRepositories, totalIssueContributions, totalPullRequestContributions, totalRepositoryContributions, err := fetchDataInTimeRange(token, username)
// if err != nil {
// fmt.Println(err)
// return
// }

// fmt.Println("totalCommitContributions: ", totalCommitContributions)
// fmt.Println("totalStarredRepositories: ", totalStarredRepositories)
// fmt.Println("totalIssueContributions: ", totalIssueContributions)
// fmt.Println("totalPullRequestContributions: ", totalPullRequestContributions)
// fmt.Println("totalRepositoryContributions: ", totalRepositoryContributions)
// }
12 changes: 9 additions & 3 deletions src/funcs/get_commit_streak.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"log"
"net/http"
"os"

"github.com/joho/godotenv"
)

const query_frame = `
Expand All @@ -27,7 +29,7 @@ const query_frame = `
}
`

type response struct {
type res struct {
Data struct {
User struct {
ContributionsCollection struct {
Expand Down Expand Up @@ -66,6 +68,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")
}
query := fmt.Sprintf(query_frame, username)

request := GraphQLQuery{Query: query}
Expand All @@ -87,7 +93,7 @@ func GetCommitHistory(username string) (int, []int, int, error) {

// fmt.Println("response1: ", resp)

var res response
var res res
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
log.Fatalf("Decoder failed: %v", err)
}
Expand All @@ -104,7 +110,7 @@ func GetCommitHistory(username string) (int, []int, int, error) {
}
}

// fmt.Println("response: ", res.Data.User.ContributionsCollection.ContributionCalendar.Weeks)
// fmt.Println("res: ", res.Data.User.ContributionsCollection.ContributionCalendar.Weeks)

// fmt.Println("dailyCommits: ", dailyCommits)
// fmt.Println("length of dailyCommits: ", len(dailyCommits))
Expand Down
10 changes: 5 additions & 5 deletions src/funcs/get_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http"
)

// / GraphQLQuery はGraphQLのクエリを格納するための構造体です
type GraphQLQuery struct {
// / GraphQLQueries はGraphQLのクエリを格納するための構造体です
type GraphQLQueries struct {
Query string `json:"query"`
}

Expand All @@ -20,7 +20,7 @@ type Repository struct {
}

// レポジトリ取得の際のGraphQLのレスポンスを格納するための構造体です
type GraphQLResponse struct {
type GraphQLResponses struct {
Data struct {
User struct {
RepositoriesContributedTo struct {
Expand Down Expand Up @@ -76,7 +76,7 @@ func GetRepositories(username, token string) ([]Repository, error) {

// GraphQL APIにリクエストを送信
url := "https://api.github.com/graphql"
reqBody := GraphQLQuery{Query: query}
reqBody := GraphQLQueries{Query: query}
reqBodyJSON, err := json.Marshal(reqBody)
if err != nil {
fmt.Println("JSON Marshal Error:", err)
Expand All @@ -100,7 +100,7 @@ func GetRepositories(username, token string) ([]Repository, error) {
defer resp.Body.Close()

// レスポンスをパース
var response GraphQLResponse
var response GraphQLResponses
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
fmt.Println("JSON Decode Error:", err)
Expand Down
Loading

0 comments on commit b119fb7

Please sign in to comment.