Last active
April 5, 2023 18:15
-
-
Save msmhrt/2707ee90c13b6da4f6ac91c791f2718a to your computer and use it in GitHub Desktop.
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
// The following code is a result of GPT-4: Use with caution | |
// | |
// prompt: | |
// このソースコードにエラー処理とコメントを適切に追加し、各関数の先頭でその関数のサイクロマティック複雑度をコメントしてください。ソースコードを出力した後にソースコードについて説明する必要はありません。 | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"regexp" | |
"strings" | |
"sync" | |
"time" | |
"github.com/BurntSushi/toml" | |
) | |
type Config struct { | |
UsernameToID map[string]string | |
} | |
var config Config | |
func main() { | |
homeDir, err := os.UserHomeDir() | |
if err != nil { | |
fmt.Println("Error getting home directory:", err) | |
return | |
} | |
_, err = toml.DecodeFile(homeDir+"/.peactivity", &config) | |
if err != nil { | |
fmt.Println("Error decoding config file:", err) | |
return | |
} | |
help := flag.Bool("h", false, "") | |
debug := flag.Bool("d", false, "") | |
flag.Parse() | |
args := flag.Args() | |
var wg sync.WaitGroup | |
sem := make(chan struct{}, 4) | |
results := make([]string, len(args)) | |
for i, input := range args { | |
wg.Add(1) | |
sem <- struct{}{} | |
go func(index int, input string) { | |
defer func() { | |
wg.Done() | |
<-sem | |
}() | |
var userID string | |
if isProfileURL(input) { | |
userID = extractUserIDFromURL(input) | |
} else if isUserID(input) { | |
userID = input | |
} else { | |
userID, err = findUserIDByUsername(input) | |
if err != nil { | |
fmt.Printf("Error finding user ID by username: %v\n", err) | |
return | |
} | |
} | |
profileURL := generateProfileURL(userID) | |
activity, err := fetchUserActivity(profileURL, *debug) | |
if err != nil { | |
fmt.Printf("Error fetching user activity: %v\n", err) | |
return | |
} | |
results[index] = fmt.Sprintf("User %s: Activity %d", userID, activity) | |
}(i, input) | |
} | |
wg.Wait() | |
for _, result := range results { | |
fmt.Println(result) | |
} | |
} | |
// isProfileURL: Cyclomatic complexity = 1 | |
func isProfileURL(input string) bool { | |
return strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") | |
} | |
// isUserID: Cyclomatic complexity = 1 | |
func isUserID(input string) bool { | |
return regexp.MustCompile(`^[1-9][0-9]*$`).MatchString(input) | |
} | |
// findUserIDByUsername: Cyclomatic complexity = 3 | |
func findUserIDByUsername(username string) (string, error) { | |
lowerUsername := strings.ToLower(username) | |
var matchedUsernames []string | |
for key, value := range config.UsernameToID { | |
if strings.HasPrefix(strings.ToLower(key), lowerUsername) { | |
matchedUsernames = append(matchedUsernames, fmt.Sprintf("%s (%s)", key, value)) | |
} | |
} | |
if len(matchedUsernames) == 0 { | |
return "", fmt.Errorf("no matching usernames found") | |
} | |
return config.UsernameToID[matchedUsernames[0]], nil | |
} | |
// extractUserIDFromURL: Cyclomatic complexity = 1 | |
func extractUserIDFromURL(url string) string { | |
re := regexp.MustCompile(`(?:http|https)://example.com/profile/([1-9][0-9]*)`) | |
return re.FindStringSubmatch(url)[1] | |
} | |
// generateProfileURL: Cyclomatic complexity = 1 | |
func generateProfileURL(userID string) string { | |
return fmt.Sprintf("https://example.com/profile/%s", userID) | |
} | |
// fetchUserActivity: Cyclomatic complexity = 3 | |
func fetchUserActivity(profileURL string, debug bool) (int, error) { | |
client := http.Client{ | |
Timeout: time.Duration(10 * time.Second), | |
} | |
resp, err := client.Get(profileURL) | |
if err != nil { | |
return 0, fmt.Errorf("error fetching profile page: %v", err) | |
} | |
defer resp.Body.Close() | |
activity, err := parseActivityFromProfile(resp.Body) | |
if err != nil { | |
return 0, fmt.Errorf("error parsing activity from profile: %v", err) | |
} | |
if debug { | |
fmt.Printf("[DEBUG] Fetched activity %d for profile %s\n", activity, profileURL) | |
} | |
return activity, nil | |
} | |
// parseActivityFromProfile: Cyclomatic complexity = 2 | |
func parseActivityFromProfile(body io.ReadCloser) (int, error) { | |
bodyBytes, err := ioutil.ReadAll(body) | |
if err != nil { | |
return 0, fmt.Errorf("error reading profile body: %v", err) | |
} | |
bodyString := string(bodyBytes) | |
// Implement the parsing logic to extract user activity from the profile page | |
// This function should return the activity as an integer. | |
return 0, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment