-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
51 lines (40 loc) · 1.33 KB
/
models.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
from django.db import models
from xml.etree import ElementTree
import requests
class Feed(models.Model):
label = models.CharField(max_length=50)
@property
def feed(self):
raise NotImplementedError()
class GithubFeed(Feed):
username = models.CharField(max_length=100)
token = models.CharField(max_length=100)
api_key = models.CharField(max_length=100)
@property
def headers(self):
return {'Authorization': 'token {}'.format(self.api_key)}
@property
def current_user_request(self):
url = 'https://github.com/{user}.private.atom?token={token}'.format(
user=self.username,
token=self.token
)
r = requests.get(url, headers=self.headers)
if r.status_code != 200:
raise Exception('Request failed ({}): {}'.format(
r.status_code, r))
return r
@property
def userfeed_xml(self):
r = self.current_user_request
return ElementTree.fromstring(r.text)
@property
def feed(self):
root = self.userfeed_xml
entries = []
for entry in root.iter(self.tag('entry')):
content = entry.find(self.tag('content'))
entries.append(content.text)
return entries
def tag(self, tag_text):
return '{http://www.w3.org/2005/Atom}' + tag_text