forked from bormaxi8080/osint-repos-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
executable file
·298 lines (237 loc) · 12.1 KB
/
builder.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import requests
import json
from datetime import datetime
GITHUB_API_TOKEN = os.environ['GITHUB_API_TOKEN']
# GITHUB_API_HEADER_ACCEPT = "Accept: application/vnd.github.v3+json"
GITHUB_API_HEADER_ACCEPT = "Accept: application/vnd.github+json"
GITHUB_API_URL = "https://api.github.com"
GITHUB_API_STARRED = "/user/starred"
GITHUB_API_USERS = "/users/"
GITHUB_REPO_URL = "https://github.com/bormaxi8080/osint-repos-list"
HEADERS = {
"Accept": GITHUB_API_HEADER_ACCEPT,
"Authorization": "token {0}".format(GITHUB_API_TOKEN)
}
MD_DOCUMENT_HEADER = "## List of GitHub Starred Repositories and Users"
MD_DOCUMENT_GENERATION = "This document generated automatically, see {0} for details".format(GITHUB_REPO_URL)
MD_DOCUMENT_LINE_SEPARATOR = "\r\n"
MD_DOCUMENT_GROUP_SEPARATOR = "----"
MD_DOCUMENT_WARNING = "WARNING! All tools, programs and techniques published in this repository are used for informational, educational purposes or for information security purposes. The authors are not responsible for the activities that users of these tools and techniques may carry out, and urge them not to use them to carry out harmful or destructive activities directed against other users or groups on the Internet."
def fetch_user(user):
not_found = "{'message': 'Not Found', 'documentation_url': 'https://docs.github.com/rest/users/users#get-a-user'}"
params = None
res = requests.get(url="{0}{1}{2}".format(GITHUB_API_URL, GITHUB_API_USERS, user),
params=params,
headers=HEADERS)
result = res.json()
if str(result) == not_found:
return None
else:
return result
def fetch_starred_repos(page=""):
params = None
if page != '':
params = {"page": page}
res = requests.get(url="{0}{1}".format(GITHUB_API_URL, GITHUB_API_STARRED),
params=params,
headers=HEADERS)
last_page = True
if 'rel="next"' in res.headers["Link"]:
last_page = False
return {
"last_page": last_page,
"repos": res.json()
}
def _separate(document):
return document + "{0}{1}".format(MD_DOCUMENT_LINE_SEPARATOR, MD_DOCUMENT_LINE_SEPARATOR)
def _save_document(path, document_data):
with open(path, "w") as f:
f.write(document_data)
print("Document saved: {0}".format(path))
return
def _save_json(path, json_data):
with open(path, "w") as f:
json.dump(json_data, f, indent=2)
print("Document saved: {0}".format(path))
return
if __name__ == '__main__':
print("Welcome to GitHub starred repos builder!")
print("See {0} for details".format(GITHUB_REPO_URL))
STARRED_REPOS = []
fetched_page = 1
STARRED_OWNERS_NAMES = []
STARRED_OWNERS = []
print("Fetching GitHub starred repos...")
# Fetch first page
print("Pages fetched: {0}".format(fetched_page))
fetched_result = fetch_starred_repos()
STARRED_REPOS.extend(fetched_result["repos"])
# Fetch other pages in loop if exists
if not fetched_result["last_page"]:
while not fetched_result["last_page"]:
fetched_page = fetched_page + 1
print("Pages fetched: {0}".format(fetched_page))
fetched_result = fetch_starred_repos(page="{0}".format(fetched_page))
STARRED_REPOS.extend(fetched_result["repos"])
print("Repos fetched: {0}".format(len(STARRED_REPOS)))
# Sort array by repository name
SORTED_REPOS = sorted(STARRED_REPOS, key=lambda x: x['name'])
print("Generating repos data...")
DOCUMENT_DATE = "**Created at:** {0}".format(datetime.now().date().strftime("%Y-%m-%d"))
# Generate Markdown document
MD_DOCUMENT = MD_DOCUMENT_HEADER + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
MD_DOCUMENT_GENERATION + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"(c) @bormaxi8080, 2023" + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
DOCUMENT_DATE + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
MD_DOCUMENT_WARNING + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"**Starred repositories count:** {0}".format(len(STARRED_REPOS)) + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"See also: " + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"- [Starred Users](starred_users.md)" + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"# Starred Repositories:" + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR
# Save repos to document structure
for repo in SORTED_REPOS:
# NOTE: Simplification of the output: image badges have been removed, since with a large number of repositories,
# a document is generated that is too large and the page freezes.
# Check repository owner
owner = str(repo["owner"]["login"])
if owner in STARRED_OWNERS_NAMES:
pass
else:
STARRED_OWNERS_NAMES.append(owner)
# Repository link
MD_DOCUMENT += "### [{0}]({1}) from [{2}]({3})".format(
str(repo["name"]), str(repo["html_url"]),
str(repo["owner"]["login"]), str(repo["owner"]["html_url"])
)
MD_DOCUMENT = _separate(MD_DOCUMENT)
# Author
# MD_DOCUMENT += "[![GitHub](https://img.shields.io/badge/GitHub-Profile-brightgreen?logo=github)](https://github.com/{0})".format(
# str(repo["owner"]["login"]))
# Description
if repo["description"] is None:
MD_DOCUMENT += "No project description"
else:
MD_DOCUMENT += str(repo["description"])
MD_DOCUMENT = _separate(MD_DOCUMENT)
# Stars
MD_DOCUMENT += "**Stars:** {0}".format(str(repo["stargazers_count"]))
# MD_DOCUMENT += " [![GitHub stars](https://img.shields.io/github/stars/{0}/{1}.svg?style=social&label=Stars)]({2})".format(
# str(repo["owner"]["login"]), str(repo["name"]), str(repo["html_url"])
# )
# First commit (created_at)
# MD_DOCUMENT += " [![GitHub creation date](https://img.shields.io/badge/Created%20on-{0}-brightgreen.svg)]({1})".format(
# datetime.strptime(str(repo["created_at"]), '%Y-%m-%dT%H:%M:%SZ').strftime("%Y--%m--%d"),
# str(repo["html_url"])
# )
MD_DOCUMENT += " / **Created on:** {0}".format(
datetime.strptime(str(repo["created_at"]), '%Y-%m-%dT%H:%M:%SZ').strftime("%Y-%m-%d"))
# Last commit
# MD_DOCUMENT += " [![GitHub last commit](https://img.shields.io/github/last-commit/{0}/{1}.svg?color=blue)]({2})".format(
# str(repo["owner"]["login"]), str(repo["name"]), str(repo["html_url"])
# )
MD_DOCUMENT += " / **Last commit:** {0}".format(
datetime.strptime(str(repo["updated_at"]), '%Y-%m-%dT%H:%M:%SZ').strftime("%Y-%m-%d"))
# Tag
# MD_DOCUMENT += " [![GitHub tags](https://img.shields.io/github/v/tag/{0}/{1}.svg)]({2})".format(
# str(repo["owner"]["login"]), str(repo["name"]), str(repo["html_url"])
# )
MD_DOCUMENT = _separate(MD_DOCUMENT)
# Topics
if not repo["topics"] is None:
topics = repo["topics"]
if len(topics) > 0:
topics_ = ["#" + item for item in topics]
str_topics = " ".join(topics_)
# str_topics = "%2C%20".join(topics_).replace("-", "\u2013")
# MD_DOCUMENT += "[![GitHub topics](https://img.shields.io/badge/Topics-{0}-brightgreen.svg)]({1})".format(
# str_topics,
# str(repo["html_url"])
# )
MD_DOCUMENT += "**Topics:** {0}".format(str_topics)
MD_DOCUMENT = _separate(MD_DOCUMENT)
# Repository Url
MD_DOCUMENT += "**Repository Url:** {0}".format(str(repo["html_url"]))
MD_DOCUMENT = _separate(MD_DOCUMENT)
# Clone
# MD_DOCUMENT += "[![GitHub clone](https://img.shields.io/badge/GitHub-Clone-green?logo=github)]({0})".format(str(repo["clone_url"]))
MD_DOCUMENT += "**Clone Url:** {0}".format(str(repo["clone_url"]))
MD_DOCUMENT = _separate(MD_DOCUMENT)
MD_DOCUMENT += MD_DOCUMENT_GROUP_SEPARATOR
MD_DOCUMENT = _separate(MD_DOCUMENT)
print("Saving document data...")
# Save Markdown document to file
_save_document("starred_repos.md", MD_DOCUMENT)
# Save json backup to file
_save_json("starred_repos.json", STARRED_REPOS)
print("Done")
print("Generating users data...")
# Generate Markdown document
MD_DOCUMENT = MD_DOCUMENT_HEADER + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
MD_DOCUMENT_GENERATION + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"(c) @bormaxi8080, 2023" + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
DOCUMENT_DATE + MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"See also: " + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"- [Starred Repositories](starred_repos.md)" + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR + \
"# Starred Users:" + \
MD_DOCUMENT_LINE_SEPARATOR + MD_DOCUMENT_LINE_SEPARATOR
# Sort users
STARRED_OWNERS_NAMES.sort()
# Fetch owners data
for owner in STARRED_OWNERS_NAMES:
owner_data = fetch_user(owner)
if owner_data is not None:
STARRED_OWNERS.append(owner_data)
for owner in STARRED_OWNERS:
if "login" in owner and "html_url" in owner:
if owner["login"] is not None and owner["html_url"] is not None:
MD_DOCUMENT += "### [{0}]({1})".format(str(owner["login"]), str(owner["html_url"]))
if "name" in owner and owner["name"] is not None:
MD_DOCUMENT += " ({0})".format(str(owner["name"]))
if "location" in owner and owner["location"] is not None:
MD_DOCUMENT += ", {0}".format(str(owner["location"]))
MD_DOCUMENT = _separate(MD_DOCUMENT)
if "bio" in owner and owner["bio"] is not None:
MD_DOCUMENT += str(owner["bio"])
MD_DOCUMENT = _separate(MD_DOCUMENT)
if "blog" in owner and owner["blog"] is not None:
if str(owner["blog"]) != "":
MD_DOCUMENT += "Site/Blog: {0}".format(str(owner["blog"]))
MD_DOCUMENT = _separate(MD_DOCUMENT)
if "public_repos" in owner and owner["public_repos"] is not None\
and "html_url" in owner and owner["html_url"] is not None\
and "followers" in owner and owner["followers"] is not None\
and "followers_url" in owner and owner["followers_url"] is not None:
MD_DOCUMENT += "Public repos: [{0}]({1}?tab=repositories) / Followers: [{2}]({3})".format(
str(owner["public_repos"]),
str(owner["html_url"]),
str(owner["followers"]),
str(owner["followers_url"]))
MD_DOCUMENT = _separate(MD_DOCUMENT)
flag = False
if "twitter_username" in owner and owner["twitter_username"] is not None:
flag = True
MD_DOCUMENT += "Twitter: [@{0}](https://twitter.com/{1})".format(
str(owner["twitter_username"]), str(owner["twitter_username"]))
if "email" in owner and owner["email"] is not None:
MD_DOCUMENT += " / "
if "email" in owner and owner["email"] is not None:
flag = True
MD_DOCUMENT += "Email: [{0}](mailto:{1})".format(
str(owner["email"]), str(owner["email"]))
if flag:
MD_DOCUMENT = _separate(MD_DOCUMENT)
MD_DOCUMENT += MD_DOCUMENT_GROUP_SEPARATOR
MD_DOCUMENT = _separate(MD_DOCUMENT)
print("Saving document data...")
# Save Markdown document to file
_save_document("starred_users.md", MD_DOCUMENT)
# Save json backup to file
_save_json("starred_users.json", STARRED_OWNERS)
print("Done")