<s> import json <EOL> import requests <EOL> BASE_URL = '<STR_LIT>' <EOL> Link_URL = '<STR_LIT>' <EOL> class Mygist : <EOL> def __init__ ( self , gist , ** args ) : <EOL> self . gist = gist <EOL> if '<STR_LIT:user>' in args : <EOL> self . user = args [ '<STR_LIT:user>' ] <EOL> else : <EOL> self . user = self . gist . username <EOL> def listall ( self ) : <EOL> '''<STR_LIT>''' <EOL> file_name = [ ] <EOL> r = requests . get ( <EOL> '<STR_LIT>' % ( BASE_URL , self . user ) , <EOL> headers = self . gist . header <EOL> ) <EOL> r_text = json . loads ( r . text ) <EOL> limit = len ( r . json ( ) ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> for g , no in zip ( r_text , range ( <NUM_LIT:0> , limit ) ) : <EOL> for key , value in r . json ( ) [ no ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> file_name . append ( value [ '<STR_LIT:filename>' ] ) <EOL> return file_name <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def list ( self , offset ) : <EOL> '''<STR_LIT>''' <EOL> file_name = [ ] <EOL> r = requests . get ( <EOL> '<STR_LIT>' % ( BASE_URL , self . user ) , <EOL> headers = self . gist . header <EOL> ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> r_text = json . loads ( r . text ) <EOL> limit = offset if ( offset <= len ( r . json ( ) ) ) else len ( r . json ( ) ) <EOL> for g , no in zip ( r_text , range ( <NUM_LIT:0> , limit ) ) : <EOL> for key , value in r . json ( ) [ no ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> file_name . append ( value [ '<STR_LIT:filename>' ] ) <EOL> return file_name <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def getMyID ( self , gist_name ) : <EOL> '''<STR_LIT>''' <EOL> r = requests . get ( <EOL> '<STR_LIT:%s>' % BASE_URL + '<STR_LIT>' % self . user , <EOL> headers = self . gist . header <EOL> ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> r_text = json . loads ( r . text ) <EOL> limit = len ( r . json ( ) ) <EOL> for g , no in zip ( r_text , range ( <NUM_LIT:0> , limit ) ) : <EOL> for ka , va in r . json ( ) [ no ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if str ( va [ '<STR_LIT:filename>' ] ) == str ( gist_name ) : <EOL> return r . json ( ) [ no ] [ '<STR_LIT:id>' ] <EOL> return <NUM_LIT:0> <EOL> def content ( self , ** args ) : <EOL> '''<STR_LIT>''' <EOL> self . gist_name = '<STR_LIT>' <EOL> if '<STR_LIT:name>' in args : <EOL> self . gist_name = args [ '<STR_LIT:name>' ] <EOL> self . gist_id = self . getMyID ( self . gist_name ) <EOL> elif '<STR_LIT:id>' in args : <EOL> self . gist_id = args [ '<STR_LIT:id>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if self . gist_id : <EOL> r = requests . get ( <EOL> '<STR_LIT:%s>' % BASE_URL + '<STR_LIT>' % self . gist_id , <EOL> headers = self . gist . header <EOL> ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> r_text = json . loads ( r . text ) <EOL> if self . gist_name != '<STR_LIT>' : <EOL> content = r . json ( ) [ '<STR_LIT>' ] [ self . gist_name ] [ '<STR_LIT:content>' ] <EOL> else : <EOL> for key , value in r . json ( ) [ '<STR_LIT>' ] . iteritems ( ) : <EOL> content = r . json ( ) [ '<STR_LIT>' ] [ value [ '<STR_LIT:filename>' ] ] [ '<STR_LIT:content>' ] <EOL> return content <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def getgist ( self , ** args ) : <EOL> if '<STR_LIT:id>' in args : <EOL> self . gist_id = args [ '<STR_LIT:id>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if self . gist_id : <EOL> r = requests . get ( <EOL> '<STR_LIT>' % ( BASE_URL , self . gist_id ) , <EOL> headers = self . gist . header , <EOL> ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> for key , value in r . json ( ) [ '<STR_LIT>' ] . iteritems ( ) : <EOL> content = value [ '<STR_LIT:filename>' ] <EOL> return content <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def edit ( self , ** args ) : <EOL> '''<STR_LIT>''' <EOL> self . gist_name = '<STR_LIT>' <EOL> if '<STR_LIT:description>' in args : <EOL> self . description = args [ '<STR_LIT:description>' ] <EOL> else : <EOL> self . description = '<STR_LIT>' <EOL> if '<STR_LIT:name>' in args and '<STR_LIT:id>' in args : <EOL> self . gist_name = args [ '<STR_LIT:name>' ] <EOL> self . gist_id = args [ '<STR_LIT:id>' ] <EOL> elif '<STR_LIT:name>' in args : <EOL> self . gist_name = args [ '<STR_LIT:name>' ] <EOL> self . gist_id = self . getMyID ( self . gist_name ) <EOL> elif '<STR_LIT:id>' in args : <EOL> self . gist_id = args [ '<STR_LIT:id>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if '<STR_LIT:content>' in args : <EOL> self . content = args [ '<STR_LIT:content>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if ( self . gist_name == '<STR_LIT>' ) : <EOL> self . gist_name = self . getgist ( id = self . gist_id ) <EOL> data = { "<STR_LIT:description>" : self . description , <EOL> "<STR_LIT>" : { <EOL> self . gist_name : { <EOL> "<STR_LIT:content>" : self . content <EOL> } <EOL> } <EOL> } <EOL> else : <EOL> data = { "<STR_LIT:description>" : self . description , <EOL> "<STR_LIT>" : { <EOL> self . gist_name : { <EOL> "<STR_LIT:content>" : self . content <EOL> } <EOL> } <EOL> } <EOL> if self . gist_id : <EOL> r = requests . patch ( <EOL> '<STR_LIT>' % ( BASE_URL , self . gist_id ) , <EOL> headers = self . gist . header , <EOL> data = json . dumps ( data ) , <EOL> ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> r_text = json . loads ( r . text ) <EOL> response = { <EOL> '<STR_LIT>' : self . content , <EOL> '<STR_LIT>' : r . json ( ) [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : r . json ( ) [ '<STR_LIT>' ] <EOL> } <EOL> return response <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def delete ( self , ** args ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:name>' in args : <EOL> self . gist_name = args [ '<STR_LIT:name>' ] <EOL> self . gist_id = self . getMyID ( self . gist_name ) <EOL> elif '<STR_LIT:id>' in args : <EOL> self . gist_id = args [ '<STR_LIT:id>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> if self . gist_id : <EOL> r = requests . delete ( <EOL> '<STR_LIT>' % ( BASE_URL , url , self . gist_id ) , <EOL> headers = self . gist . header <EOL> ) <EOL> if ( r . status_code == <NUM_LIT> ) : <EOL> response = { <EOL> '<STR_LIT:id>' : self . gist_id , <EOL> } <EOL> return response <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def starred ( self , ** args ) : <EOL> '''<STR_LIT>''' <EOL> ids = [ ] <EOL> r = requests . get ( <EOL> '<STR_LIT>' % BASE_URL , <EOL> headers = self . gist . header <EOL> ) <EOL> if '<STR_LIT>' in args : <EOL> limit = args [ '<STR_LIT>' ] <EOL> else : <EOL> limit = len ( r . json ( ) ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> for g in range ( <NUM_LIT:0> , limit ) : <EOL> ids . append ( '<STR_LIT>' % ( GIST_URL , r . json ( ) [ g ] [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] , r . json ( ) [ g ] [ '<STR_LIT:id>' ] ) ) <EOL> return ids <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def links ( self , ** args ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:name>' in args : <EOL> self . gist_name = args [ '<STR_LIT:name>' ] <EOL> self . gist_id = self . getMyID ( self . gist_name ) <EOL> elif '<STR_LIT:id>' in args : <EOL> self . gist_id = args [ '<STR_LIT:id>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if self . gist_id : <EOL> r = requests . get ( <EOL> '<STR_LIT>' % ( BASE_URL , self . gist_id ) , <EOL> headers = self . gist . header , <EOL> ) <EOL> if ( r . status_code == <NUM_LIT:200> ) : <EOL> content = { <EOL> '<STR_LIT>' : r . json ( ) [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : r . json ( ) [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( Link_URL , self . gist . username , r . json ( ) [ '<STR_LIT:id>' ] ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( Link_URL , r . json ( ) [ '<STR_LIT:id>' ] ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( Link_URL , self . gist . username , r . json ( ) [ '<STR_LIT:id>' ] ) <EOL> } <EOL> return content <EOL> raise Exception ( '<STR_LIT>' ) </s>
