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