Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 69 lines (55 sloc) 2.218 kB
9ed2037 first
Dominik Dabrowski authored
1 import time
2 import requests
3 import util
4 import lxml.html
5 from util import hashabledict, Bunch, Counter
6
7
8 class Event(Bunch):
9 value = 0
10
11 def __init__(self, *args, **kwargs):
12 super(Event, self).__init__(*args, **kwargs)
13 self.commits = kwargs.get('commits')
14
15 @staticmethod
16 def classify(event):
17 '''
18 takes a dictionary and return the proper Event based on its type
19 '''
20 for cls in Event.__subclasses__():
21 if cls.__name__ == event['type']:
22 return cls(**event)
23 return Event(**event)
24
25 def __repr__(self):
26 return '<Event %s:%s>' % (self.actor, self.type)
27
28 def score(self, *args, **kwargs):
29 return self.value
30
31 def get_commits(self):
32 pass
33
34 def load(self, d):
35 self.__dict__.update(d)
36
37 class PushEvent(Event):
38 value = 10
39
40 def get_commits(self):
41 '''
42 determine the number of own commits from the Compare View
43 since the Event doesnt tell us which username did the commit
44 '''
45 if not '/compare/' in self.url:
46 raise ValueError, 'it only makes sense to grab the number of own commits on actions with a compare url'
47 if not self.url.startswith('http'):
48 self.url = 'http://github.com' + self.url
49 h = lxml.html.fromstring(requests.get(self.url).content)
50 time.sleep(1)
51 self.authors = h.xpath("//span[@class='commit-author']/a/text()")
52 self.msgs = h.xpath("//code/a[@class='message']/text()")
53
54 if len(self.authors) != len(self.msgs):
55 print 'WARNING', len(self.authors), len(self.msgs)
56 self.commits = [{'author':author, 'msg':msg} for author, msg in zip(self.authors, self.msgs)]
57 print 'got %s commits' % len(self.commits)
58
59 def score(self):
60 if self.commits is None:
61 self.get_commits()
62 # all unique commits by author
63 commit_counter = Counter(map(hashabledict, self.commits))
64 return self.value * sum(self.actor == commit['author'] and
65 commit_counter[hashabledict(commit)] == 1 for commit in self.commits)
66
67 class GistEvent(Event):
68 value = 5
Something went wrong with that request. Please try again.