Skip to content

Instantly share code, notes, and snippets.

@Hellowlol
Last active August 29, 2015 14:07
Show Gist options
  • Save Hellowlol/d12cd37ec3e0735914b6 to your computer and use it in GitHub Desktop.
Save Hellowlol/d12cd37ec3e0735914b6 to your computer and use it in GitHub Desktop.
Whatthefork2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from requests.auth import HTTPBasicAuth
import json
import webbrowser
from math import ceil
import time
class Whatthefork():
### Holders
apicalls_reset = 0
apicalls = 0
apicalls_left = 0
apilocked = False
ahead_list = []
br_ahead = 0
###
def __init__(self, username, password, owner, repo, openbrwser=False):
self.username = username
self.password = password
self.usr = owner
self.rpo = repo
def fetch(self, url):
''' Used to connect to github api'''
if not self.apilocked:
try:
#Github api req user-agent
headers = {'User-Agent': 'Mozilla/5.0'}
data = requests.get(url, auth=HTTPBasicAuth(self.username, self.password), timeout=15, headers=headers)
self.apicalls += 1
self.apicalls_left = int(data.headers["X-RateLimit-Remaining"])
self.apicalls_reset = data.headers["X-RateLimit-Reset"]
print self.apicalls_left
if int(data.headers["X-RateLimit-Remaining"]) <= 300:
self.apilocked = True
return json.loads(data.text)
except Exception as e:
print e, url
else:
print "No more apicalls left %s, will sleep until %s" % (self.apicalls_left,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(self.apicalls_reset)))))
time.sleep(time.time() - int(self.apicalls_reset))
def branches_ahead(self):
st = time.time()
mrepo = "%s:master" % self.usr
repos = self.getbranches2()
for branch in repos:
try:
result = self.fetch('https://api.github.com/repos/%s/compare/%s:%s...%s' % (branch["user"], self.usr, branch["name"], branch["name"]))
if result["message"] == "Not Found":
result = self.fetch('https://api.github.com/repos/%s/compare/%s...%s' % (branch["user"], mrepo, branch["name"]))
except Exception as e:
pass #print branch["name"], branch["user"], e
try:
if result["total_commits"] > 0 and result["ahead_by"] > 0:
self.br_ahead += 1
curl = "https://github.com/%s/commits/%s" % (branch["user"], branch["name"])
print "Ahead", result["total_commits"], branch["user"], branch["name"], curl
d = {
"fork": branch["user"],
"branch": branch["name"],
"ahead": result["ahead_by"],
"url": curl
}
self.ahead_list.append(d)
if self.openbrwser:
webbrowser.open(curl, new=2, autoraise=False)
except:
pass
print 'The "Scan" took ', time.time() - st
print "\n"
print "There was %s forks in total" % self.forks_count
print "There was %s branches ahead" % self.br_ahead
print "%i apicalls left" % int(self.apicalls_left)
print "Api limit will reset in %s" % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(self.apicalls_reset)))
#print self.ahead_list
return self.ahead_list
def getbranches2(self):
forks = self.getforks()
l = []
lappend = l.append
for f in forks:
branches = self.fetch("https://api.github.com/repos/%s/branches" % f)
# Loop the list of branches, add a name of the forker
for f2 in branches:
f2["user"] = f
lappend(f2)
return l
def getbranches(self):
forks = self.getforks()
nwl = []
#dots are bad
nwlappend = nwl.append
for fork in forks:
branches = self.fetch("https://api.github.com/repos/%s/branches" % fork)
nwlappend(branches)
print nwl
return nwl
def getforks(self):
''' Returns all the forks of a repo as a list '''
r = self.getrepo()
nwl = []
#Dots are bad
nwlappend = nwl.append
if r["forks_count"] <= 100:
forks = self.fetch("https://api.github.com/repositories/%s/forks?per_page=100" % r["id"])
for fork in forks:
nwlappend(fork["full_name"])
return nwl
else:
pages = float(r["forks_count"]) / 100
pages_round = ceil(pages)
list_of_forks = []
for i in range(1, int(pages_round) + 1):
forks = self.fetch("https://api.github.com/repositories/%s/forks?page=%s&per_page=100" % (r["id"], i))
for f in forks:
list_of_forks.append(f["full_name"])
return list_of_forks
def getrepo(self):
''' fetch a users repos '''
result = self.fetch("https://api.github.com/users/%s/repos" % self.usr)
for r in result:
if r["name"] == self.rpo:
self.id = r["id"]
self.forks_count = r["forks_count"]
return r
if __name__ == "__main__":
pass
gh = Whatthefork(username="githubusername", password="githubpassword", owner="repo_owner", repo="Awesome_repo_name")
gh.branches_ahead()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment