-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirect_export.py
More file actions
executable file
·197 lines (163 loc) · 7.28 KB
/
direct_export.py
File metadata and controls
executable file
·197 lines (163 loc) · 7.28 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
#!/usr/bin/env python3
"""
Direct GitHub Stars Exporter
"""
import os
import sys
import json
import csv
import datetime
try:
from github import Github, GithubException
except ImportError:
print("Installing required packages...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "PyGithub"])
from github import Github, GithubException
# Configuration
GITHUB_USERNAME = "helloprkr" # Your GitHub username
GITHUB_TOKEN = "" # Your GitHub personal access token (optional)
OUTPUT_DIR = os.getcwd() # Current directory for output files
# 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 for @{GITHUB_USERNAME}\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 copy_to_icloud():
"""Copy files to iCloud directory."""
try:
icloud_dir = "/Users/maximvs/Library/Mobile Documents/com~apple~CloudDocs/00X_000_JORDAN-PARKER-PLANS/Memory-🧿/00_GitHub/STARRED_github🌟"
if not os.path.exists(icloud_dir):
os.makedirs(icloud_dir)
print(f"Created iCloud directory: {icloud_dir}")
import shutil
for file in [CSV_FILE, JSON_FILE, MARKDOWN_FILE]:
if os.path.exists(file):
shutil.copy2(file, icloud_dir)
print(f"Copied {file} to iCloud")
print(f"All files copied to iCloud directory: {icloud_dir}")
except Exception as e:
print(f"Error copying to iCloud: {e}")
print("Files remain in the current directory.")
def main():
"""Main function to run the script."""
print(f"Starting GitHub Stars export for user: {GITHUB_USERNAME}")
# Get starred repositories
repos = get_github_stars(GITHUB_USERNAME, GITHUB_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)
# Copy to iCloud
copy_to_icloud()
print(f"Export complete! Files saved to:\n- {OUTPUT_DIR}\n- iCloud")
if __name__ == "__main__":
main()