Last active
March 13, 2025 13:39
-
-
Save blachniet/6a659f347219b6ca961262fdf22bb689 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"encoding/csv" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
func usage() { | |
mt.Fprintf(flag.CommandLine.Output(), "stars2drops - convert GitHub stars to a Raindrop.io CSV file\n\n") | |
mt.Fprintf(flag.CommandLine.Output(), "You can upload the resulting CSV file to your Raindrop.io account here: https://app.raindrop.io/settings/import. See https://help.raindrop.io/import/#csv for more information on the CSV format.") | |
mt.Fprintf(flag.CommandLine.Output(), "\n\n") | |
fmt.Fprintf(flag.CommandLine.Output(), "environment variables:\n") | |
fmt.Fprintf(flag.CommandLine.Output(), " GH_PAT\n") | |
fmt.Fprintf(flag.CommandLine.Output(), " \trequired - GitHub fine-grained token with read-only 'starring' permissions: https://github.com/settings/tokens?type=beta\n") | |
fmt.Fprintf(flag.CommandLine.Output(), "\nargs:\n") | |
flag.PrintDefaults() | |
} | |
func main() { | |
var folder string | |
var tags string | |
flag.StringVar(&folder, "folder", "Unsorted", "Raindrop.io folder to put bookmarks in") | |
flag.StringVar(&tags, "tags", "", "comma-separated list of tags to apply to all bookmarks") | |
flag.Usage = usage | |
flag.Parse() | |
pat := os.Getenv("GH_PAT") | |
if pat == "" { | |
log.Fatalln("err: GH_PAT environment varable not set") | |
} | |
stars, err := getStars(pat) | |
if err != nil { | |
log.Fatalln("err: retrieving stars: ", err) | |
} | |
writer := csv.NewWriter(os.Stdout) | |
err = writer.Write([]string{"url", "folder", "title", "description", "tags", "created"}) | |
if err != nil { | |
log.Fatal("err: write csv: ", err) | |
} | |
for _, star := range stars { | |
err = writer.Write([]string{star.Repo.HTMLURL, folder, star.Repo.FullName, star.Repo.Description, tags, star.StarredAt}) | |
if err != nil { | |
log.Fatalln("err: write csv: ", err) | |
} | |
} | |
writer.Flush() | |
if err = writer.Error(); err != nil { | |
log.Fatalln("err: write csv: ", err) | |
} | |
} | |
func getStars(pat string) ([]ghStar, error) { | |
var stars []ghStar | |
req, err := http.NewRequest(http.MethodGet, "https://api.github.com/user/starred", nil) | |
if err != nil { | |
return stars, fmt.Errorf("build request: %w", err) | |
} | |
req.Header.Set("Accept", "application/vnd.github.star+json") | |
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", pat)) | |
req.Header.Set("X-GitHub-Api-Version", "2022-11-28") | |
client := &http.Client{} | |
res, err := client.Do(req) | |
if err != nil { | |
return stars, fmt.Errorf("do request: %w", err) | |
} | |
err = json.NewDecoder(res.Body).Decode(&stars) | |
if err != nil { | |
return stars, fmt.Errorf("decode json: %w", err) | |
} | |
res.Body.Close() | |
return stars, nil | |
} | |
type ghStar struct { | |
StarredAt string `json:"starred_at"` | |
Repo ghStarRepo `json:"repo"` | |
} | |
type ghStarRepo struct { | |
HTMLURL string `json:"html_url"` | |
FullName string `json:"full_name"` | |
Description string `json:"description"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment