Created
May 21, 2016 22:10
-
-
Save matthewr6/f9ef983a7fbe1dc07692443fb3016f3f to your computer and use it in GitHub Desktop.
Scratch user referencing
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 ( | |
"os" | |
"fmt" | |
"bufio" | |
"regexp" | |
"strings" | |
"net/http" | |
"encoding/json" | |
) | |
type ProfileData struct { | |
Id int `json:"id"` | |
WorkingOn string `json:"status"` | |
About string `json:"bio"` | |
} | |
type UserData struct { | |
Id int `json:"id"` | |
Username string `json:"username"` | |
Profile *ProfileData `json:"profile"` | |
} | |
func getBio(username string) string { | |
res, err := http.Get(fmt.Sprintf("https://api.scratch.mit.edu/users/%v", username)) | |
defer res.Body.Close() | |
if err != nil { | |
return "" | |
} | |
decoder := json.NewDecoder(res.Body) | |
var data UserData | |
decoder.Decode(&data) | |
if data.Profile != nil { | |
return data.Profile.About | |
} | |
return "" | |
} | |
func main() { | |
// data.txt should already exist and be empty | |
f, _ := os.OpenFile("data.txt", os.O_APPEND, 0666) | |
defer f.Close() | |
reader := bufio.NewReader(os.Stdin) | |
fmt.Print("Enter a user to begin with: ") | |
text, _ := reader.ReadString('\n') | |
text = strings.TrimSpace(text) | |
check := []string{text} | |
checked := make(map[string]bool) | |
for true { | |
u := check[len(check)-1] | |
check = check[:len(check)-1] | |
if _, ok := checked[strings.ToLower(u)]; !ok { | |
r := regexp.MustCompile(`@([A-Za-z\d\-_]+)`) | |
matches := r.FindAllStringSubmatch(getBio(u), -1) | |
for _, match := range matches { | |
next := match[1] | |
check = append(check, next) | |
f.WriteString(fmt.Sprintf("%v referenced %v\n", u, next)) | |
} | |
checked[strings.ToLower(u)] = true | |
} | |
if len(check) == 0 { | |
break | |
} | |
} | |
} |
I think it's a script to do the same thing as my Data Blocks project
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This totally wasn't inspired by ANYTHING that happened today