Skip to content

Commit 207eb23

Browse files
committed
the whole layout at once -initial
0 parents  commit 207eb23

File tree

10 files changed

+783
-0
lines changed

10 files changed

+783
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# setup.py output
2+
build/
3+
dist/
4+
gists.egg-info/
5+
6+
# OS Files
7+
.DS_Store
8+
.DS_Store?
9+
*.pyc
10+
11+
#pypi settings file
12+
.pyirc

LICENSE.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Varun Malhotra
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include HISTORY.rst README.rst

README.rst

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
GistApi-Wrapper-python
2+
======================
3+
4+
Python wrapper for ``GitHub's Gist``.
5+
6+
Features
7+
--------
8+
9+
* Creating gists returning the url, script and clone link for copy-paste purpose
10+
* Checkout one's gists - Name(s), Description and Content
11+
* Edit and Delete a gist
12+
* Search GitHub user's gist - fork, star and unstar them
13+
* List-all comments of any gist, make/edit a comment on a gist, delete a comment
14+
15+
Installation
16+
-------------
17+
.. code-block:: bash
18+
19+
$ pip install gists
20+
21+
Example Usage
22+
-------------
23+
24+
.. code-block:: python
25+
26+
from gist import Gist
27+
28+
GHgist = Gist(username='USERNAME',api_token='API_TOKEN')
29+
# or provide USERNAME and API_TOKEN in config.py file, so just, GHgist = Gist()
30+
31+
# creating gist and returning url, script, clone link
32+
GHgist.create(name='Test.py', description='just testing it', public=1, content='print "Yay! Test Passed" ')
33+
34+
# Lists all the names of authenticated user's gists
35+
GHgist.profile().listall()
36+
37+
# Lists only the names of recent two gists of user 'softvar'
38+
GHgist.search('geojeff').list(2)
39+
40+
# Lists all the comments on gist named 'bootstrap-min.css' of user 'softvar'
41+
GHgist.comments().listall(user='softvar',name='bootstrap-min.css')
42+
43+
# ...and many more...
44+
45+
Patches and suggestions are welcomed
46+
------------------------------------
47+
48+
.. code-block:: bash
49+
$ git clone

gists/__init__.py

Whitespace-only changes.

gists/comments.py

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
BASE_URL = 'https://api.github.com'
2+
GIST_URL = 'https://gist.github.com'
3+
4+
import json
5+
import requests
6+
7+
8+
9+
class Comments:
10+
def __init__(self, gist):
11+
self.gist = gist
12+
13+
14+
def getMyID(self,gist_name):
15+
'''
16+
Getting gistID of a gist in order to make the workflow
17+
easy and uninterrupted.
18+
'''
19+
r = requests.get(
20+
'%s'%BASE_URL+'/users/%s/gists' % self.user,
21+
headers=self.gist.header
22+
)
23+
if (r.status_code == 200):
24+
r_text = json.loads(r.text)
25+
limit = len(r.json())
26+
27+
for g,no in zip(r_text, range(0,limit)):
28+
for ka,va in r.json()[no]['files'].iteritems():
29+
if str(va['filename']) == str(gist_name):
30+
return r.json()[no]['id']
31+
return 0
32+
33+
raise Exception('Username not found')
34+
35+
36+
def listall(self, **args):
37+
if 'user' in args:
38+
self.user = args['user']
39+
print self.user
40+
else:
41+
self.user = self.gist.username
42+
43+
self.gist_name = ''
44+
if 'name' in args:
45+
self.gist_name = args['name']
46+
self.gist_id = self.getMyID(self.gist_name)
47+
elif 'id' in args:
48+
self.gist_id = args['id']
49+
else:
50+
raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid')
51+
52+
if self.gist_id:
53+
file_name = []
54+
r = requests.get(
55+
'%s'%BASE_URL+'/gists/%s/comments' % self.gist_id,
56+
headers=self.gist.header
57+
)
58+
r_text = json.loads(r.text)
59+
limit = len(r.json())
60+
if (r.status_code == 200 ):
61+
for g,no in zip(r_text, range(0,limit)):
62+
file_name.append(r.json()[no]['body'])
63+
return r.json()
64+
65+
raise Exception('Gistname not found')
66+
67+
def create(self, **args):
68+
if 'body' in args:
69+
self.body = {'body':args['body']}
70+
else:
71+
raise Exception('Comment Body can\'t be empty')
72+
if 'user' in args:
73+
self.user = args['user']
74+
print self.user
75+
else:
76+
self.user = self.gist.username
77+
78+
self.gist_name = ''
79+
if 'name' in args:
80+
self.gist_name = args['name']
81+
self.gist_id = self.getMyID(self.gist_name)
82+
elif 'id' in args:
83+
self.gist_id = args['id']
84+
else:
85+
raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid')
86+
87+
if self.gist_id:
88+
89+
r = requests.post(
90+
'%s'%BASE_URL+'/gists/%s/comments' % self.gist_id,
91+
headers=self.gist.header,
92+
data=json.dumps(self.body)
93+
)
94+
if (r.status_code == 201):
95+
response ={
96+
'GistID': self.gist_id,
97+
'CommenID': r.json()['id'],
98+
'body': self.body['body'],
99+
'created_at': r.json()['created_at']
100+
}
101+
return response
102+
103+
104+
raise Exception('Comment not created')
105+
106+
107+
def delete(self, **args):
108+
109+
self.user = self.gist.username
110+
111+
self.gist_name = ''
112+
if 'name' in args:
113+
self.gist_name = args['name']
114+
self.gist_id = self.getMyID(self.gist_name)
115+
elif 'id' in args:
116+
self.gist_id = args['id']
117+
else:
118+
raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid')
119+
120+
if 'commentid' in args:
121+
self.commentid = args['commentid']
122+
else:
123+
raise Exception('CommenID not provided')
124+
125+
if self.gist_id:
126+
r = requests.delete(
127+
'%s/gists/%s/comments/%s'%(BASE_URL,self.gist_id, self.commentid),
128+
headers=self.gist.header
129+
)
130+
if (r.status_code == 204):
131+
response ={
132+
'deleted': 'True',
133+
'GistID': self.gist_id,
134+
'CommentID': self.commentid,
135+
}
136+
return response
137+
else:
138+
response ={
139+
'comment' : 'not exists'
140+
}
141+
return response
142+
143+
raise Exception('Gist/Comment not exits')
144+
145+
146+
def get(self, **args):
147+
148+
self.user = self.gist.username
149+
150+
self.gist_name = ''
151+
if 'name' in args:
152+
self.gist_name = args['name']
153+
self.gist_id = self.getMyID(self.gist_name)
154+
elif 'id' in args:
155+
self.gist_id = args['id']
156+
else:
157+
raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid')
158+
159+
if 'commentid' in args:
160+
self.commentid = args['commentid']
161+
else:
162+
raise Exception('CommenID not provided')
163+
164+
if self.gist_id:
165+
r = requests.get(
166+
'%s/gists/%s/comments/%s'%(BASE_URL,self.gist_id, self.commentid),
167+
headers=self.gist.header
168+
)
169+
if (r.status_code == 200):
170+
response ={
171+
'body': r.json()['body'],
172+
'GistID': self.gist_id,
173+
'CommentID': self.commentid,
174+
'created_at': r.json()['created_at']
175+
}
176+
return response
177+
else:
178+
response ={
179+
'comment' : 'not exists'
180+
}
181+
return response
182+
183+
184+
raise Exception('Comment not exits/deleted')
185+
186+
def edit(self, **args):
187+
if 'body' in args:
188+
self.body = {'body':args['body']}
189+
else:
190+
raise Exception('Comment Body can\'t be empty')
191+
if 'user' in args:
192+
self.user = args['user']
193+
print self.user
194+
else:
195+
self.user = self.gist.username
196+
197+
if 'commentid' in args:
198+
self.commentid = args['commentid']
199+
else:
200+
raise Exception('CommenID not provided')
201+
202+
self.gist_name = ''
203+
if 'name' in args:
204+
self.gist_name = args['name']
205+
self.gist_id = self.getMyID(self.gist_name)
206+
elif 'id' in args:
207+
self.gist_id = args['id']
208+
else:
209+
raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid')
210+
211+
if self.gist_id:
212+
213+
r = requests.patch(
214+
'%s/gists/%s/comments/%s'%(BASE_URL,self.gist_id, self.commentid),
215+
headers=self.gist.header,
216+
data=json.dumps(self.body)
217+
)
218+
if (r.status_code == 200):
219+
response ={
220+
'GistID': self.gist_id,
221+
'CommenID': r.json()['id'],
222+
'body': self.body['body'],
223+
'created_at': r.json()['created_at']
224+
}
225+
return response
226+
else:
227+
response = {
228+
'comment' : 'not edited'
229+
}
230+
231+
232+
raise Exception('Comment not created')

0 commit comments

Comments
 (0)