Last active
June 13, 2022 20:03
-
-
Save Phoenix616/3af6d7acafe819dc51dba4bccc941e67 to your computer and use it in GitHub Desktop.
A python script to compare repositories by the amount of stars while removing people who stared on of the other repos before. See an example with IRC clients here: https://gist.github.com/Phoenix616/4a9a75d425b7913c6580c01265e465b6
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
# Copyright (c) 2018 Phoenix616 (Max Lee) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import sys | |
import os | |
import time | |
from github import Github | |
if len(sys.argv) < 3: | |
print "Invalid syntax, you have to at least specify two repositories with " + sys.argv[0] + " <user/repo>...\n" | |
quit() | |
g = Github("API-KEY", per_page=100) | |
useMarkdown = False | |
titleName = "Repository" | |
titleLatest = "latest" | |
titleTotal = "total" | |
titleDifference = "difference" | |
titleInfo = "stared latest" | |
repos = dict() | |
stargazer = dict() | |
longestRepo = 0; | |
for i in range(1, len(sys.argv)): | |
if i == 1 and sys.argv[i] == "markdown=true": | |
if len(sys.argv) < 4: | |
print "Invalid syntax, you have to at least specify two repositories with " + sys.argv[0] + " markdown=true <user/repo>...\n" | |
quit() | |
useMarkdown = True | |
print "Using markdown for table!\n" | |
continue | |
repoPath = sys.argv[i] | |
if "/" not in repoPath: | |
repoPath = repoPath + "/" + repoPath | |
tablename = "[" + repoPath.split("/")[1] + "](https://github.com/" + repoPath + ")" if useMarkdown else repoPath | |
if len(tablename) > longestRepo: | |
longestRepo = len(tablename) | |
repos[repoPath] = dict() | |
repos[repoPath]["gazers"] = [] | |
repos[repoPath]["latestgazers"] = [] | |
repos[repoPath]["latest"] = dict() | |
repos[repoPath]["tablename"] = tablename | |
repo = g.get_repo(repoPath) | |
path = "/tmp/star-date-" + repoPath.replace("/", "-", 1) + ".tmp" | |
if os.path.isfile(path) and os.path.getmtime(path) + 60 * 60 * 1000 > time.time() and os.path.getsize(path) > 0: | |
print "\nLoading", repoPath, "data from tmp file:" | |
f = open(path,"r") | |
l = 0 | |
for line in f.readlines(): | |
l += 1 | |
sys.stdout.write(unichr(9734)) | |
if l % 10 == 0: | |
sys.stdout.flush() | |
if l % 100 == 0: | |
print " " if l < 1000 else "",l | |
parts = line.split(" ") | |
if parts[0] not in stargazer: | |
stargazer[parts[0]] = dict() | |
stargazer[parts[0]][repoPath] = parts[1] | |
repos[repoPath]["gazers"].append(parts[0]) | |
sys.stdout.flush() | |
print " " if l < 1000 else "",l | |
else: | |
print "\nQuerying", repoPath, "data from GitHub API:" | |
f = open(path,"w+") | |
l = 0 | |
for user in repo.get_stargazers_with_dates(): | |
l += 1 | |
sys.stdout.write(unichr(9734)) | |
if l % 10 == 0: | |
sys.stdout.flush() | |
if l % 100 == 0: | |
print " " if l < 1000 else "",l | |
print >>f, user.user.login + " " + user.starred_at.strftime("%s") | |
if user.user.login not in stargazer: | |
stargazer[user.user.login] = dict() | |
stargazer[user.user.login][repoPath] = user.starred_at.strftime("%s") | |
repos[repoPath]["gazers"].append(user.user.login) | |
f.close() | |
sys.stdout.flush() | |
print " " if l < 1000 else "",l | |
for user, repoDict in stargazer.iteritems(): | |
latestRepo = max(repoDict, key=repoDict.get) | |
repos[latestRepo]["latestgazers"].append(user) | |
for repo in repoDict: | |
if repo != latestRepo: | |
if latestRepo not in repos[repo]["latest"]: | |
repos[repo]["latest"][latestRepo] = 1 | |
else: | |
repos[repo]["latest"][latestRepo] = repos[repo]["latest"][latestRepo] + 1 | |
print "\ntable:\n" | |
nameLength = longestRepo if longestRepo > len(titleName) else len(titleName) | |
print titleName.ljust(nameLength),"|",titleLatest,"|",titleTotal,"|",titleDifference,"|",titleInfo | |
print "-" * nameLength + "-|-" + "-" * len(titleLatest) + "-|-" + "-" * len(titleTotal) + "-|-" + "-" * len(titleDifference) + "-|-" + "-" * len(titleInfo) | |
for repo, info in sorted(repos.iteritems(), key=lambda x: len(x[1]["latestgazers"]), reverse=True): | |
latestStaredRepos = [] | |
latestStaredReposOthers = [] | |
for repoName, stars in sorted(info["latest"].iteritems(), key=lambda x: x[1], reverse=True): | |
if len(latestStaredRepos) < 4 or stars > (len(info["gazers"])-len(info["latestgazers"])) / 10: | |
latestStaredRepos.append(("[" + repoName.split("/")[1] + "](https://github.com/" + repoName + ")" if useMarkdown else repoName) + "(" + str(stars) + ")") | |
else: | |
latestStaredReposOthers.append(("[" + repoName.split("/")[1] + "](https://github.com/" + repoName + ")" if useMarkdown else repoName) + "(" + str(stars) + ")") | |
if len(latestStaredReposOthers) > 1: | |
latestStaredRepos.append(str(len(latestStaredReposOthers)) + " others") | |
elif len(latestStaredReposOthers) == 1: | |
latestStaredRepos.append(latestStaredReposOthers[0]) | |
print info["tablename"].ljust(nameLength), "|", str(len(info["latestgazers"])).ljust(len(titleLatest)), "|", str(len(info["gazers"])).ljust(len(titleTotal)), "|", str((len(info["gazers"])-len(info["latestgazers"]))).ljust(len(titleDifference)), "|", ", ".join(latestStaredRepos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment