-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_stars_simple.sh
More file actions
executable file
·226 lines (182 loc) · 7.87 KB
/
export_stars_simple.sh
File metadata and controls
executable file
·226 lines (182 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# Super simple GitHub stars export - just runs with defaults
# Configuration - EDIT THESE TWO LINES with your information
USERNAME="helloprkr" # Your GitHub username
TOKEN="" # Your GitHub token (optional but recommended)
# iCloud Path
ICLOUD_DIR="/Users/maximvs/Library/Mobile Documents/com~apple~CloudDocs/00X_000_JORDAN-PARKER-PLANS/Memory-🧿/00_GitHub/STARRED_github🌟"
# Set up workspace
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
VENV_DIR="${SCRIPT_DIR}/venv"
# Create virtual environment
python3 -m venv "$VENV_DIR" 2>/dev/null || python -m venv "$VENV_DIR" 2>/dev/null
# Activate and install
source "${VENV_DIR}/bin/activate"
pip install -q PyGithub
# Create export script
TMP_FILE=$(mktemp)
cat > $TMP_FILE << 'PYTHONSCRIPT'
import os, sys, json, csv, datetime
from github import Github, GithubException
# Configuration (will be replaced)
GITHUB_USERNAME = "__USERNAME__"
GITHUB_TOKEN = "__TOKEN__"
OUTPUT_DIR = "__OUTDIR__"
# File paths
CSV_FILE = os.path.join(OUTPUT_DIR, "github_stars.csv")
JSON_FILE = os.path.join(OUTPUT_DIR, "github_stars.json")
MARKDOWN_FILE = os.path.join(OUTPUT_DIR, "github_stars.md")
def get_github_stars(username, token=None):
"""Fetch starred repositories for a given GitHub username."""
try:
if token:
g = Github(token)
else:
g = Github()
print(f"Fetching starred repositories for {username}...")
user = g.get_user(username)
stars = user.get_starred()
total_stars = stars.totalCount
print(f"Found {total_stars} starred repositories")
repos = []
count = 0
for repo in stars:
count += 1
repo_data = {
"name": repo.name,
"full_name": repo.full_name,
"owner": repo.owner.login,
"description": repo.description or "",
"html_url": repo.html_url,
"clone_url": repo.clone_url,
"stars": repo.stargazers_count,
"forks": repo.forks_count,
"language": repo.language or "Not specified",
"topics": repo.get_topics(),
"created_at": repo.created_at.isoformat() if repo.created_at else None,
"updated_at": repo.updated_at.isoformat() if repo.updated_at else None,
"license": repo.license.name if repo.license else "No license",
"is_archived": repo.archived,
"is_fork": repo.fork
}
repos.append(repo_data)
if count % 10 == 0:
print(f"Processed {count}/{total_stars} repositories...")
print(f"Completed fetching {count} repositories")
return repos
except GithubException as e:
print(f"GitHub API error: {e}")
return []
except Exception as e:
print(f"An error occurred: {e}")
return []
def save_to_csv(repos, filename):
"""Save repositories to CSV file."""
try:
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = ["name", "full_name", "owner", "description", "html_url",
"clone_url", "stars", "forks", "language", "topics"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for repo in repos:
repo_copy = repo.copy()
repo_copy["topics"] = ", ".join(repo_copy["topics"])
writer.writerow({field: repo_copy.get(field, "") for field in fieldnames})
print(f"Saved CSV to {filename}")
except Exception as e:
print(f"Error saving to CSV: {e}")
def save_to_json(repos, filename):
"""Save repositories to JSON file."""
try:
with open(filename, 'w', encoding='utf-8') as jsonfile:
json.dump(repos, jsonfile, indent=2)
print(f"Saved JSON to {filename}")
except Exception as e:
print(f"Error saving to JSON: {e}")
def save_to_markdown(repos, filename):
"""Save repositories to Markdown file."""
try:
with open(filename, 'w', encoding='utf-8') as mdfile:
mdfile.write(f"# Starred GitHub Repositories\n\n")
mdfile.write(f"_Exported on {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}_\n\n")
mdfile.write(f"Total repositories: **{len(repos)}**\n\n")
# Group repositories by language
languages = {}
for repo in repos:
lang = repo["language"] or "Not specified"
if lang not in languages:
languages[lang] = []
languages[lang].append(repo)
# Sort languages by number of repositories
sorted_languages = sorted(languages.items(), key=lambda x: len(x[1]), reverse=True)
# Table of contents
mdfile.write("## Table of Contents\n\n")
for lang, lang_repos in sorted_languages:
lang_anchor = lang.lower().replace(' ', '-').replace('#', 'sharp')
mdfile.write(f"- [{lang} ({len(lang_repos)})](#user-content-{lang_anchor})\n")
mdfile.write("\n")
# Write repositories grouped by language
for lang, lang_repos in sorted_languages:
mdfile.write(f"## {lang}\n\n")
# Sort repositories by stars
sorted_repos = sorted(lang_repos, key=lambda x: x["stars"], reverse=True)
for repo in sorted_repos:
mdfile.write(f"### [{repo['full_name']}]({repo['html_url']})\n\n")
if repo["description"]:
mdfile.write(f"{repo['description']}\n\n")
mdfile.write(f"- **Stars:** {repo['stars']}\n")
mdfile.write(f"- **Language:** {repo['language']}\n")
if repo["topics"]:
mdfile.write(f"- **Topics:** {', '.join(repo['topics'])}\n")
mdfile.write("\n---\n\n")
print(f"Saved Markdown to {filename}")
except Exception as e:
print(f"Error saving to Markdown: {e}")
def main():
"""Main function to run the script."""
# Get username and token from substituted values
username = GITHUB_USERNAME
token = GITHUB_TOKEN
if not username or username == "__USERNAME__":
print("No GitHub username provided. Cannot proceed.")
return
if token == "__TOKEN__":
token = ""
if not token:
print("No GitHub token provided. This may result in rate limiting.")
# Get starred repositories
repos = get_github_stars(username, token)
if not repos:
print("No repositories found or an error occurred.")
return
# Save to different formats
save_to_csv(repos, CSV_FILE)
save_to_json(repos, JSON_FILE)
save_to_markdown(repos, MARKDOWN_FILE)
print(f"Export complete! Files saved to: {OUTPUT_DIR}")
if __name__ == "__main__":
# Verify username is set correctly
print(f"Username: {GITHUB_USERNAME}")
if GITHUB_USERNAME and GITHUB_USERNAME != "__USERNAME__":
main()
else:
print("ERROR: GitHub username not set properly!")
PYTHONSCRIPT
# Replace variables in the script
sed -i "" "s|__USERNAME__|$USERNAME|g" $TMP_FILE
sed -i "" "s|__TOKEN__|$TOKEN|g" $TMP_FILE
sed -i "" "s|__OUTDIR__|$SCRIPT_DIR|g" $TMP_FILE
# Run the script
echo "Running GitHub Stars export..."
python $TMP_FILE
# Clean up
rm $TMP_FILE
# Copy to iCloud
if [ -d "$ICLOUD_DIR" ]; then
echo "Copying files to iCloud..."
mkdir -p "$ICLOUD_DIR"
cp github_stars.* "$ICLOUD_DIR/"
echo "Files copied to: $ICLOUD_DIR"
fi
deactivate
echo "Done! Your GitHub stars have been exported."