def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT): self.__requester = Requester(login_or_token, password, base_url, timeout)
def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=DEFAULT_PER_PAGE): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int """ assert login_or_token is None or isinstance( login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance( client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page)
def __init__(self): self.logger = logging.getLogger() self.logger.setLevel(logging.INFO) self.cash = None self.date = None self.holdings = {} self.r = Requester()
class Github: def __init__( self, login=None, password=None ): self.__requester = Requester( login=login, password=password ) def _dataRequest( self, verb, url, parameters, data ): return self.__requester.dataRequest( verb, url, parameters, data ) def _statusRequest( self, verb, url, parameters, data ): return self.__requester.statusRequest( verb, url, parameters, data ) def get_user( self, login = None ): if login is None: return AuthenticatedUser( self, {}, lazy = True ) else: return NamedUser( self, { "login": login }, lazy = False ) def get_organization( self, login ): return Organization( self, { "login": login }, lazy = False ) def get_gist( self, id ): return Gist( self, { "id": id }, lazy = False ) def get_gists( self ): return [ Gist( self, attributes, lazy = True ) for attributes in self._dataRequest( "GET", "/gists/public", None, None ) ]
def __init__(self, authId, authPass): Requester.__init__(self, "https://mtgox.com/api/1") self._authKey = authId self._authSecret = base64.b64decode(authPass.encode()) # for now, we will manage only EUR. #self._availableCurrencies = {"USD", "EUR", "JPY", "CAD", "GBP", "CHF", "RUB", "AUD", "SEK", "DKK", "HKD", "PLN", "CNY", "SGD", "THB", "NZD", "NOK"} self._availableCurrencies = {"EUR"}
def __init__(self, login_or_token=None, password=None, jwt=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=DEFAULT_PER_PAGE, api_preview=False, verify=True, retry=None): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int :param verify: boolean or string :param retry: int or urllib3.util.retry.Retry object """ assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert jwt is None or isinstance(jwt, (str, unicode)), jwt assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent assert isinstance(api_preview, (bool)) assert retry is None or isinstance(retry, (int)) or isinstance(retry, (urllib3.util.Retry)) self.__requester = Requester(login_or_token, password, jwt, base_url, timeout, client_id, client_secret, user_agent, per_page, api_preview, verify, retry)
class Github(object): def __init__(self, login_or_token=None, password=None): self.__requester = Requester(login_or_token, password) @property def rate_limiting(self): return self.__requester.rate_limiting def get_user(self, login=None): if login is None: return AuthenticatedUser.AuthenticatedUser( self.__requester, {"url": "https://api.github.com/user"}, completed=False) else: headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/users/" + login, None, None) return NamedUser.NamedUser(self.__requester, data, completed=True) def get_organization(self, login): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/orgs/" + login, None, None) return Organization.Organization(self.__requester, data, completed=True) def get_gist(self, id): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/gists/" + str(id), None, None) return Gist.Gist(self.__requester, data, completed=True) def get_gists(self): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/gists/public", None, None) return PaginatedList.PaginatedList(Gist.Gist, self.__requester, headers, data)
def run_requester_threads(thread_number): for i in range(thread_number): thread = Requester(i, wordpress_url, plugins_directory, sleep_between_req_in_milis, proxies, basic_auth_user, basic_auth_password) thread.start() threads.append(thread) Printer.p(NAME, 'Requester threads started')
def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None): self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent)
def __init__(self) -> None: # get all the api keys load_dotenv() self.rapidAPIKey = os.getenv('RAPID_API_KEY') # dictionaries containing api urls and host urls from rapid api self.apiURLs = { # each key maps to a list with the first idex containing the api url and # the second containing the host url "edamam": [os.getenv('EDAMAM_URL'), os.getenv('EDAMAM_HOST_URL')], # ... More to add } self.requester = Requester()
def __init__( self, api_key=None, api_secret=None, oauth_token=None, oauth_token_secret=None): self.__requester = Requester( api_key, api_secret, oauth_token, oauth_token_secret)
def __init__( self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent="PyGithub/Python", per_page=DEFAULT_PER_PAGE, ): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int """ assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent self.__requester = Requester( login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page )
class Github( object ): def __init__( self, login_or_token = None, password = None ): self.__requester = Requester( login_or_token, password ) @property def rate_limiting( self ): return self.__requester.rate_limiting def get_user( self, login = None ): if login is None: return AuthenticatedUser.AuthenticatedUser( self.__requester, { "url": "https://api.github.com/user" }, completed = False ) else: headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/users/" + login, None, None ) return NamedUser.NamedUser( self.__requester, data, completed = True ) def get_organization( self, login ): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/orgs/" + login, None, None ) return Organization.Organization( self.__requester, data, completed = True ) def get_gist( self, id ): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/gists/" + str( id ), None, None ) return Gist.Gist( self.__requester, data, completed = True ) def get_gists( self ): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/gists/public", None, None ) return PaginatedList.PaginatedList( Gist.Gist, self.__requester, headers, data )
class Resy(object): def __init__(self, api_key=None): self.base_requester = Requester(api_key) def location(self, params=None, lat=None, long=None, radius=None): """Returns venues wthin a given radius""" if params is None: params = self._process_params(locals()) return self.base_requester.GET('location/config', 3, params) def venues(self, params=None, location_id=None): """Returns venues from within a location_id""" if params is None: params = self._process_params(locals()) return self.base_requester.GET('venues', 2, params) def venue(self, params=None, location_id=None, url_slug=None): """returns a specific venue given location_id and url_slug (venue name)""" if params is None: params = self._process_params(locals()) return self.base_requester.GET('venue', 2, params) def calendar(self, params=None, id=None): """ returns the dates open for a specific venue""" if params is None: params = self._process_params(locals()) return self.base_requester.GET('venue/calendar', 2, params) def top(self, params=None, day=None, location_id=None, num_seats=None): """returns the top venues from a given day, location, and seat number""" if params is None: params = self._process_params(locals()) return self.base_requester.GET('venues/top', 2, params) # def reservation(self, params=None, x=None, y=None, day=None, num_seats=None, # auth_token=None, time_slot=None, venue_id=None, location_id=None): # if params is None: # params = self._process_params(locals()) # return self.base_requester.GET('reservation/find', 2, params) def get(self, path, version, params): """returns json for a specific path, version, and paramater list""" return self.base_requester.GET(path, version, params) def set_key(self, api_key): """set the api_key""" self.base_requester.set_key(api_key) def _process_params(self, params={}): """read in the locals from a function, and return the processed params dictionary""" for key, value in params.copy().iteritems(): if 'self' == key or None == value: del params[key] return params
def __init__(self, config, eventSched, httpRequester, ownAddrFunc, peerId, persister, pInMeasure, pOutMeasure, peerPool, connBuilder, connListener, connHandler, choker, torrent, torrentIdent, torrentDataPath, version): ##global stuff self.config = config self.version = version self.peerPool = peerPool self.connBuilder = connBuilder self.connListener = connListener self.connHandler = connHandler self.choker = choker ##own stuff self.log = Logger('Bt', '%-6s - ', torrentIdent) self.torrent = torrent self.torrentIdent = torrentIdent self.log.debug("Creating object persister") self.btPersister = BtObjectPersister(persister, torrentIdent) self.log.debug("Creating measure classes") self.inRate = Measure(eventSched, 60, [pInMeasure]) self.outRate = Measure(eventSched, 60, [pOutMeasure]) self.inRate.stop() self.outRate.stop() self.log.debug("Creating storage class") self.storage = Storage(self.config, self.btPersister, torrentIdent, self.torrent, torrentDataPath) self.log.debug("Creating global status class") self.pieceStatus = PieceStatus(self.torrent.getTotalAmountOfPieces()) self.log.debug("Creating file priority class") self.filePrio = FilePriority(self.btPersister, self.version, self.pieceStatus, self.storage.getStatus(), self.torrent, torrentIdent) self.log.debug("Creating requester class") self.requester = Requester(self.config, self.torrentIdent, self.pieceStatus, self.storage, self.torrent) self.log.debug("Creating tracker requester class") self.trackerRequester = TrackerRequester(self.config, self.btPersister, eventSched, peerId, self.peerPool, ownAddrFunc, httpRequester, self.inRate, self.outRate, self.storage, self.torrent, self.torrentIdent, self.version) self.log.debug("Creating superseeding handler class") self.superSeedingHandler = SuperSeedingHandler(self.torrentIdent, self.btPersister, self.storage.getStatus(), self.pieceStatus) ##callbacks self.log.debug("Adding callbacks") self._addCallbacks() ##status self.state = 'stopped' self.started = False self.paused = True ##lock self.lock = threading.Lock()
def setUp( self ): unittest.TestCase.setUp( self ) self.r = Requester( "login", "password" ) self.b64_userpass = base64.b64encode( "login:password" ) self.b64_userpass = self.b64_userpass.replace( '\n', '' ) self.connectionFactory = MockMockMock.Mock( "httplib.HTTPSConnection" ) self.connection = MockMockMock.Mock( "connection", self.connectionFactory ) self.response = MockMockMock.Mock( "response", self.connectionFactory ) httplib.HTTPSConnection = self.connectionFactory.object
class TestCase( unittest.TestCase ): def setUp( self ): unittest.TestCase.setUp( self ) self.r = Requester( "login", "password" ) self.b64_userpass = base64.b64encode( "login:password" ) self.b64_userpass = self.b64_userpass.replace( '\n', '' ) self.connectionFactory = MockMockMock.Mock( "httplib.HTTPSConnection" ) self.connection = MockMockMock.Mock( "connection", self.connectionFactory ) self.response = MockMockMock.Mock( "response", self.connectionFactory ) httplib.HTTPSConnection = self.connectionFactory.object def tearDown( self ): self.connectionFactory.tearDown() unittest.TestCase.tearDown( self ) def expect( self, verb, url, input, status, responseHeaders, output ): self.connectionFactory.expect( "api.github.com", strict = True ).andReturn( self.connection.object ) self.connection.expect.request( verb, url, input, { "Authorization" : "Basic " + self.b64_userpass } ) self.connection.expect.getresponse().andReturn( self.response.object ) self.response.expect.status.andReturn( status ) self.response.expect.getheaders().andReturn( responseHeaders ) self.response.expect.read().andReturn( output ) self.connection.expect.close() def testSimpleStatus( self ): self.expect( "GET", "/test", "null", 200, [], "" ) self.assertEqual( self.r.statusRequest( "GET", "/test", None, None ), 200 ) def testSimpleData( self ): self.expect( "GET", "/test", "null", 200, [], '{ "foo": "bar" }' ) self.assertEqual( self.r.dataRequest( "GET", "/test", None, None ), { "foo" : "bar" } ) def testDataOnBadStatus( self ): self.expect( "GET", "/test", "null", 404, [], '{ "foo": "bar" }' ) with self.assertRaises( UnknownGithubObject ): self.r.dataRequest( "GET", "/test", None, None ) def testDataWithParametersAndData( self ): self.expect( "GET", "/test?tata=tutu&toto=titi", '{"xxx": 42}', 200, [], '{ "foo": "bar" }' ) self.assertEqual( self.r.dataRequest( "GET", "/test", { "toto" : "titi", "tata" : "tutu" }, { "xxx" : 42 } ), { "foo" : "bar" } ) def testPagination( self ): self.expect( "GET", "/test", 'null', 200, [ ( "link", "<xxx?page=2>; next, xxx; last" ) ], '[ 1, 2 ]' ) self.expect( "GET", "/test?page=2", 'null', 200, [ ( "link", "xxx; prev, xxx; first, <xxx?page=3>; next, xxx; last" ) ], '[ 3, 4 ]' ) self.expect( "GET", "/test?page=3", 'null', 200, [ ( "link", "xxx; prev, xxx; first" ) ], '[ 5, 6 ]' ) self.assertEqual( self.r.dataRequest( "GET", "/test", None, None ), [ 1, 2, 3, 4, 5, 6 ] ) def testPaginationObviouslyFinished( self ): self.expect( "GET", "/test", 'null', 200, [ ( "link", "<xxx?page=2>; next, xxx; last" ) ], '[ 1, 2 ]' ) self.expect( "GET", "/test?page=2", 'null', 200, [ ( "link", "xxx; prev, xxx; first, <xxx?page=3>; next, xxx; last" ) ], '[ 3, 4 ]' ) self.expect( "GET", "/test?page=3", 'null', 200, [ ( "link", "xxx; prev, xxx; first" ) ], '[]' ) self.assertEqual( self.r.dataRequest( "GET", "/test", None, None ), [ 1, 2, 3, 4 ] )
def __init__(self, dictionary, logger, perm_dict): # email, password, name, server_id, server_name threading.Thread.__init__(self) self.email = dictionary["email"] self.password = dictionary["password"] self.server = { "id": dictionary["server_id"], "status": None, "name": dictionary["server_name"], "last_login": None, "url": { "region": None, "map": None, "main": None } } self.data = { "player": { "-1": { "nick": dictionary["name"] } }, # player with index -1 is used when player_id is None so we can have name for logs before # logging in "player_rank": {}, "alliance": {}, "habitat": {}, "habitat_unit": [], "report": {}, "transit": [], "diplomacy": { "red": [], "green": [], "blue": [], "orange": [] } } self.player_id = "-1" self.requester = Requester(self) self.tasks = [] self.scheduled_tasks = {} self.active = True self.last_time_idle = False self.logger = logger self.perm_dict = perm_dict self.add_startup_tasks() print(str(self))
class LinkedIn( object ): def __init__( self, api_key=None, api_secret=None, oauth_token=None, oauth_token_secret=None): self.__requester = Requester( api_key, api_secret, oauth_token, oauth_token_secret) def get_user( self, login = None ): headers, data = self.__requester.requestAndCheck( "GET", 'https://api.linkedin.com/v1/people/~', ['id','first-name'], None ) return LinkedInUser.LinkedInUser( self.__requester, data, completed = True )
class PageScraper: __page_content = None __requester = None __beautify = None def __init__(self, url): self.__requester = Requester(url) def initialize(self): self.__page_content = self.__requester.get_page_content() self.__beautify = Beautify(self.__page_content) def run(self): while True: self.__beautify.parse_content() self.__beautify.filter_main_counters() self.__beautify.log_main_counters() self.__beautify.parse_statistics('belgium') local_data = self.__beautify.get_local_statistics() global_data = self.__beautify.get_global_statistics() FirePost.send_global_data_to_firebase( total_dead=global_data['total_dead'], total_infected=global_data['total_infected'], total_recovered=global_data['total_recovered']) FirePost.send_local_data_to_firebase( country=local_data['country'], total_cases=local_data['total_cases'], new_cases=local_data['new_cases'], total_deaths=local_data['total_deaths'], new_deaths=local_data['new_deaths'], total_recovered=local_data['total_recovered'], active_cases=local_data['active_cases'], critical=local_data['serious_critical'], total_case_per_million=local_data['total_cases_per_1_million']) # sleep 24 hours sleep(60 * 60 * 24)
def __init__(self, authId, authPass): Requester.__init__(self, 'https://bitcoin-central.net/api/v1/') self.authId = authId self.authPass = authPass concat = authId + ':' + authPass self.authHttpBase64 = base64.b64encode(concat.encode())
def main(): if len(sys.argv) > 1: config_path = sys.argv[1] else: config_path = './configs/config_default.txt' if not Path(config_path).is_file(): logging.error("Could not find config file!") sys.exit(1) # exiting with error code # load config config = configparser.ConfigParser() config.read(config_path) log_dir = config['PATHS']['log_dir'] log_file_name = config['PATHS']['log_file_name'] # check if config dir is present if not Path(log_dir).is_dir(): logging.error("Logging directory is not present!") sys.exit(1) # exiting with error code file_handler = TimedRotatingFileHandler(os.path.join( os.path.dirname(__file__), log_dir, log_file_name), when='midnight', interval=1) console_handler = logging.StreamHandler() logging.basicConfig( level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[file_handler, console_handler]) logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logging.getLogger("requests").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING) logging.getLogger("apscheduler.scheduler").setLevel(logging.WARNING) logging.getLogger("apscheduler.executors.default").setLevel( logging.WARNING) logging.getLogger("chardet.charsetprober").setLevel(logging.WARNING) logger.info("=======Starting=Crawler=========") # store config preferences in variables article_download_pattern = ([ (int(config['ARTICLE_DOWNLOAD_PATTERN']['number']), int(config['ARTICLE_DOWNLOAD_PATTERN']['delay'])), ]) # [(application number, period in seconds) ... ] number_download_worker = int(config['CRAWLING']['number_download_worker']) website_request_timeout = int( config['REQUESTS']['website_request_timeout']) rss_feed_crawl_period = int(config['CRAWLING']['rss_feed_crawl_period']) rss_feed_request_timeout = int( config['REQUESTS']['rss_feed_request_timeout']) warmup_iterations = int(config['CRAWLING']['warmup_iterations']) throttle_velocity = float(config['CRAWLING']['throttle_velocity']) max_offset = int(config['CRAWLING']['max_offset']) downloads_path = config['PATHS']['downloads'] crawled_rss_articles_path = config['PATHS']['rss_articles'] feed_path = config['PATHS']['feeds_list'] requests_path = config['PATHS']['requests'] # partly validating the config if not Path(feed_path).is_file(): logging.error("Could not find RSS feeds list file!") sys.exit(1) # exiting with error code parent_dir = os.path.dirname(requests_path) if not Path(parent_dir).is_dir(): logging.error("Could not find requests directory!") sys.exit(1) # exiting with error code writer = Writer() writer.start() throttle = Throttle(request_velocity=throttle_velocity) rss_requester = Requester(tag="RSS Requester", path=requests_path, throttle=throttle) website_requester = Requester(tag="Website Requester", path=requests_path, throttle=throttle) scheduler = Scheduler(patterns=article_download_pattern) crawler = Crawler(requester=rss_requester, scheduler=scheduler, feed_path=feed_path, crawled_rss_articles_path=crawled_rss_articles_path, rss_feed_crawl_period=rss_feed_crawl_period, rss_feed_request_timeout=rss_feed_request_timeout, warmup_iterations=warmup_iterations, max_offset=max_offset) crawler.start() for i in range(number_download_worker): logger.info("Starting download worker #%d", i) DownloadWorker(requester=website_requester, timeout=website_request_timeout, path=downloads_path).start() while True: time.sleep(60) logger.debug("Number of threads running: %d", threading.active_count()) process = psutil.Process(os.getpid()) ram_usage = process.memory_full_info() # percent = absolute/mem.total logger.info("RAM usage: %s%%, %s", process.memory_percent(), ram_usage)
LINKERFINAL = Graphline(LINKRESOLVE=LinkResolver(bitlyusername, bitlyapikey), LINKREQUESTER=HTTPGetter(proxy, "BBC R&D Grabber", 10), linkages={ ("self", "inbox"): ("LINKRESOLVE", "inbox"), ("LINKRESOLVE", "outbox"): ("self", "outbox"), ("LINKRESOLVE", "urlrequests"): ("LINKREQUESTER", "inbox"), ("LINKREQUESTER", "outbox"): ("LINKRESOLVE", "responses") }).activate() system = Graphline( CURRENTPROG=WhatsOn(proxy), REQUESTER=Requester( "all", dbuser, dbpass ), # Can set this for specific channels to limit Twitter requests whilst doing dev FIREHOSE=TwitterStream( username, password, proxy, True, 40 ), # Twitter API sends blank lines every 30 secs so timeout of 40 should be fine SEARCH=PeopleSearch(consumerkeypair, keypair, proxy), COLLECTOR=DataCollector(dbuser, dbpass), RAWCOLLECTOR=RawDataCollector(dbuser, dbpass), HTTPGETTER=HTTPGetter(proxy, "BBC R&D Grabber", 10), HTTPGETTERRDF=HTTPGetter(proxy, "BBC R&D Grabber", 10), TWOWAY=TwoWaySplitter(), ANALYSIS=LiveAnalysis(dbuser, dbpass), NLTKANALYSIS=LiveAnalysisNLTK(dbuser, dbpass), TWEETCLEANER=Pipeline( LINKER, RetweetFixer(), RetweetCorrector(dbuser, dbpass), TweetCleaner(['user_mentions', 'urls', 'hashtags'])),
class Bt: def __init__(self, config, eventSched, httpRequester, ownAddrFunc, peerId, persister, pInMeasure, pOutMeasure, peerPool, connBuilder, connListener, connHandler, choker, torrent, torrentIdent, torrentDataPath, version): ##global stuff self.config = config self.version = version self.peerPool = peerPool self.connBuilder = connBuilder self.connListener = connListener self.connHandler = connHandler self.choker = choker ##own stuff self.log = Logger('Bt', '%-6s - ', torrentIdent) self.torrent = torrent self.torrentIdent = torrentIdent self.log.debug("Creating object persister") self.btPersister = BtObjectPersister(persister, torrentIdent) self.log.debug("Creating measure classes") self.inRate = Measure(eventSched, 60, [pInMeasure]) self.outRate = Measure(eventSched, 60, [pOutMeasure]) self.inRate.stop() self.outRate.stop() self.log.debug("Creating storage class") self.storage = Storage(self.config, self.btPersister, torrentIdent, self.torrent, torrentDataPath) self.log.debug("Creating global status class") self.pieceStatus = PieceStatus(self.torrent.getTotalAmountOfPieces()) self.log.debug("Creating file priority class") self.filePrio = FilePriority(self.btPersister, self.version, self.pieceStatus, self.storage.getStatus(), self.torrent, torrentIdent) self.log.debug("Creating requester class") self.requester = Requester(self.config, self.torrentIdent, self.pieceStatus, self.storage, self.torrent) self.log.debug("Creating tracker requester class") self.trackerRequester = TrackerRequester(self.config, self.btPersister, eventSched, peerId, self.peerPool, ownAddrFunc, httpRequester, self.inRate, self.outRate, self.storage, self.torrent, self.torrentIdent, self.version) self.log.debug("Creating superseeding handler class") self.superSeedingHandler = SuperSeedingHandler(self.torrentIdent, self.btPersister, self.storage.getStatus(), self.pieceStatus) ##callbacks self.log.debug("Adding callbacks") self._addCallbacks() ##status self.state = 'stopped' self.started = False self.paused = True ##lock self.lock = threading.Lock() ##internal functions - callbacks def _addCallbacks(self): ownStatus = self.storage.getStatus() self.persistentStatusCallback = self.config.addCallback((('storage', 'persistPieceStatus'),), ownStatus.enablePersisting) def _removeCallbacks(self): self.config.removeCallback(self.persistentStatusCallback) ##internal functions - start/pause/stop - common def _halt(self, targetState): if self.paused and targetState in ('shutdown', 'remove'): #stopping and already paused, only need to stop the tracker requester and the callbacks self.log.debug("Removing callbacks") self._removeCallbacks() self.log.debug("Stopping tracker requester") self.trackerRequester.stop() else: #either stopping, removing or shutdown and still running or loading self.log.debug("Aborting storage loading just in case") self.storage.abortLoad() if self.started: #were already running self.started = False if targetState == 'stop': self.log.debug("Pausing tracker requester") self.trackerRequester.pause() else: self.log.debug("Removing callbacks") self._removeCallbacks() self.log.debug("Stopping tracker requester") self.trackerRequester.stop() self.log.debug("Removing us from choker") self.choker.removeTorrent(self.torrentIdent) self.log.debug("Removing us from connection builder") self.connBuilder.removeTorrent(self.torrentIdent) self.log.debug("Removing us from connection listener") self.connListener.removeTorrent(self.torrent.getTorrentHash()) self.log.debug("Removing us from connection handler") self.connHandler.removeTorrent(self.torrentIdent) self.log.debug("Stopping transfer measurement") self.inRate.stop() self.outRate.stop() #shutdown/removal specific tasks which need to be done regardless of current status if targetState in ('shutdown', 'remove'): self.log.debug("Removing all infos related to us from connection pool") self.peerPool.clear(self.torrentIdent) if targetState == 'remove': self.log.debug('Removing all persisted objects of this torrent') self.btPersister.removeAll() ##internal functions - start/pause/stop - specific def _start(self, loadSuccess): try: if loadSuccess: #loading was successful, add to handlers self.log.debug("Reseting requester") self.requester.reset() self.log.debug("Starting transfer measurement") self.inRate.start() self.outRate.start() self.log.debug("Adding us to connection handler") self.connHandler.addTorrent(self.torrentIdent, self.torrent, self.pieceStatus, self.inRate, self.outRate, self.storage, self.filePrio, self.requester, self.superSeedingHandler) self.log.debug("Adding us to connection listener") self.connListener.addTorrent(self.torrentIdent, self.torrent.getTorrentHash()) self.log.debug("Adding us to connection builder") self.connBuilder.addTorrent(self.torrentIdent, self.torrent.getTorrentHash()) self.log.debug("Adding us to choker") self.choker.addTorrent(self.torrentIdent, self.storage.getStatus(), self.superSeedingHandler) self.log.debug("Starting tracker requester") self.trackerRequester.start() self.started = True self.state = 'running' except: #something failed - hard self.log.error("Error in load function:\n%s", logTraceback()) ##external functions - state def start(self): #called when torrent is started self.lock.acquire() if self.paused: self.paused = False if self.storage.isLoaded(): self.log.debug("Storage already loaded, skipping hashing") self._start(True) else: self.storage.load(self._start) self.state = 'loading' self.lock.release() def stop(self): #called when torrent is stopped self.lock.acquire() if not self.paused: self._halt('stop') self.paused = True self.state = 'stopped' self.lock.release() def shutdown(self): #called on shutdown self.lock.acquire() self._halt('shutdown') self.paused = False self.state = 'stopped' self.lock.release() def remove(self): #called when torrent is removed self.lock.acquire() self._halt('remove') self.paused = False self.state = 'stopped' self.lock.release() ##external functions - stats def getStats(self, wantedStats): self.lock.acquire() stats = {} if wantedStats.get('state', False): stats['state'] = self.state #connections if wantedStats.get('connections', False): stats.update(self.connHandler.getStats(self.torrentIdent, connDetails=True)) #files if wantedStats.get('files', False): stats['files'] = self.filePrio.getStats() #peers if wantedStats.get('peers', False) or wantedStats.get('connectionAverages', False): #get peer stats connAverages = wantedStats.get('connectionAverages', False) stats.update(self.peerPool.getStats(self.torrentIdent)) stats.update(self.connHandler.getStats(self.torrentIdent, connSummary=True, connAverages=connAverages)) stats.update(self.trackerRequester.getStats(trackerSummary=True)) #normalise peer stats if stats['connectedLeeches'] > stats['knownLeeches']: stats['knownLeeches'] = stats['connectedLeeches'] if stats['connectedSeeds'] > stats['knownSeeds']: stats['knownSeeds'] = stats['connectedSeeds'] if stats['knownLeeches'] + stats['knownSeeds'] > stats['knownPeers']: stats['knownPeers'] = stats['knownLeeches'] + stats['knownSeeds'] elif stats['knownLeeches'] + stats['knownSeeds'] < stats['knownPeers']: stats['knownLeeches'] += stats['knownPeers'] - stats['knownSeeds'] #generate additional conn stats if necessary if connAverages: if stats['knownSeeds'] == 0: stats['knownLeechesPerSeed'] = 0 else: stats['knownLeechesPerSeed'] = (stats['knownLeeches'] * 1.0) / stats['knownSeeds'] #pieces if wantedStats.get('pieceAverages', False): stats.update(self.pieceStatus.getStats(pieceAverages=True)) #progress stats if wantedStats.get('progress', False): stats.update(self.storage.getStats()) #requests if wantedStats.get('requests', False) or wantedStats.get('pieceAverages', False): reqDetails = wantedStats.get('requests', False) pieceAverages = wantedStats.get('pieceAverages', False) stats.update(self.connHandler.getRequesterStats(self.torrentIdent, requestDetails=reqDetails, pieceAverages=pieceAverages)) #tracker if wantedStats.get('tracker', False): stats.update(self.trackerRequester.getStats(trackerDetails=True)) if wantedStats.get('trackerStatus', False): stats.update(self.trackerRequester.getStats(trackerStatus=True)) #transfer stats if wantedStats.get('transfer', False): stats['inRawBytes'] = self.inRate.getTotalTransferedBytes() stats['outRawBytes'] = self.outRate.getTotalTransferedBytes() stats['inPayloadBytes'] = self.inRate.getTotalTransferedPayloadBytes() stats['outPayloadBytes'] = self.outRate.getTotalTransferedPayloadBytes() stats['inRawSpeed'] = self.inRate.getCurrentRate() stats['outRawSpeed'] = self.outRate.getCurrentRate() stats['protocolOverhead'] = (100.0 * (stats['inRawBytes'] + stats['outRawBytes'] - stats['inPayloadBytes'] - stats['outPayloadBytes'])) / max(stats['inPayloadBytes'] + stats['outPayloadBytes'], 1.0) if wantedStats.get('transferAverages', False): stats['avgInRawSpeed'] = self.inRate.getAverageRate() * 1024 stats['avgOutRawSpeed'] = self.outRate.getAverageRate() * 1024 stats['avgInPayloadSpeed'] = self.inRate.getAveragePayloadRate() * 1024 stats['avgOutPayloadSpeed'] = self.outRate.getAveragePayloadRate() * 1024 #torrent stats if wantedStats.get('torrent', False): stats.update(self.torrent.getStats()) stats['superSeeding'] = self.superSeedingHandler.isEnabled() self.lock.release() return stats ##external funcs - actions def setFilePriority(self, fileIds, priority): self.lock.acquire() for fileId in fileIds: self.filePrio.setFilePriority(fileId, priority) self.lock.release() def setFileWantedFlag(self, fileIds, wanted): self.lock.acquire() if self.started: #already running, need to go through the connection handler because of syncing issues self.connHandler.setFileWantedFlag(self.torrentIdent, fileIds, wanted) else: #not running for fileId in fileIds: self.filePrio.setFileWantedFlag(fileId, wanted) self.lock.release() def setSuperSeeding(self, enabled): self.lock.acquire() if not enabled == self.superSeedingHandler.isEnabled(): if self.started: self.connHandler.setSuperSeeding(self.torrentIdent, enabled) else: self.superSeedingHandler.setEnabled(enabled) self.lock.release() ##external funcs - tracker actions def getTrackerInfo(self): self.lock.acquire() trackerInfo = self.trackerRequester.getTrackerInfo() self.lock.release() return trackerInfo def setTrackerInfo(self, newTrackerInfo): self.lock.acquire() self.trackerRequester.setTrackerInfo(newTrackerInfo) self.lock.release() ##external funcs - other def getInfohash(self): self.lock.acquire() infohash = self.torrent.getTorrentHash() self.lock.release() return infohash
class Github( object ): def __init__( self, login_or_token = None, password = None, base_url = DEFAULT_BASE_URL, timeout = DEFAULT_TIMEOUT): self.__requester = Requester( login_or_token, password, base_url, timeout ) @property def rate_limiting( self ): return self.__requester.rate_limiting def get_user( self, login = GithubObject.NotSet ): assert login is GithubObject.NotSet or isinstance( login, ( str, unicode ) ), login if login is GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser( self.__requester, { "url": "/user" }, completed = False ) else: headers, data = self.__requester.requestAndCheck( "GET", "/users/" + login, None, None ) return NamedUser.NamedUser( self.__requester, data, completed = True ) def get_organization( self, login ): assert isinstance( login, ( str, unicode ) ), login headers, data = self.__requester.requestAndCheck( "GET", "/orgs/" + login, None, None ) return Organization.Organization( self.__requester, data, completed = True ) def get_gist( self, id ): assert isinstance( id, ( str, unicode ) ), id headers, data = self.__requester.requestAndCheck( "GET", "/gists/" + id, None, None ) return Gist.Gist( self.__requester, data, completed = True ) def get_gists( self ): headers, data = self.__requester.requestAndCheck( "GET", "/gists/public", None, None ) return PaginatedList.PaginatedList( Gist.Gist, self.__requester, headers, data ) def legacy_search_repos( self, keyword, language = GithubObject.NotSet ): assert isinstance( keyword, ( str, unicode ) ), keyword assert language is GithubObject.NotSet or isinstance( language, ( str, unicode ) ), language args = {} if language is GithubObject.NotSet else { "language": language } return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote( keyword ), args, self.__requester, "repositories", Legacy.convertRepo, Repository.Repository, ) def legacy_search_users( self, keyword ): assert isinstance( keyword, ( str, unicode ) ), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote( keyword ), {}, self.__requester, "users", Legacy.convertUser, NamedUser.NamedUser, ) def legacy_search_user_by_email( self, email ): assert isinstance( email, ( str, unicode ) ), email headers, data = self.__requester.requestAndCheck( "GET", "/legacy/user/email/" + email, None, None ) return NamedUser.NamedUser( self.__requester, Legacy.convertUser( data[ "user" ] ), completed = False ) def render_markdown( self, text, context = GithubObject.NotSet ): assert isinstance( text, ( str, unicode ) ), text assert context is GithubObject.NotSet or isinstance( context, Repository.Repository ), context post_parameters = { "text": text } if context is not GithubObject.NotSet: post_parameters[ "mode" ] = "gfm" post_parameters[ "context" ] = context._identity status, headers, data = self.__requester.requestRaw( "POST", "/markdown", None, post_parameters ) return data def get_hooks( self ): headers, data = self.__requester.requestAndCheck( "GET", "/hooks", None, None ) return [ HookDescription.HookDescription( self.__requester, attributes, completed = True ) for attributes in data ]
class Github(object): """ This is the main class you instantiate to access the Github API v3. Optional parameters allow different authentication methods. """ def __init__(self, login_or_token=None, password=None, jwt=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=DEFAULT_PER_PAGE, api_preview=False, verify=True, retry=None): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int :param verify: boolean or string :param retry: int or urllib3.util.retry.Retry object """ assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert jwt is None or isinstance(jwt, (str, unicode)), jwt assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent assert isinstance(api_preview, (bool)) assert retry is None or isinstance(retry, (int)) or isinstance(retry, (urllib3.util.Retry)) self.__requester = Requester(login_or_token, password, jwt, base_url, timeout, client_id, client_secret, user_agent, per_page, api_preview, verify, retry) def __get_FIX_REPO_GET_GIT_REF(self): """ :type: bool """ return self.__requester.FIX_REPO_GET_GIT_REF def __set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF) def __get_per_page(self): """ :type: int """ return self.__requester.per_page def __set_per_page(self, value): self.__requester.per_page = value # v2: Remove this property? Why should it be necessary to read/modify it after construction per_page = property(__get_per_page, __set_per_page) # v2: Provide a unified way to access values of headers of last response # v2: (and add/keep ad hoc properties for specific useful headers like rate limiting, oauth scopes, etc.) # v2: Return an instance of a class: using a tuple did not allow to add a field "resettime" @property def rate_limiting(self): """ First value is requests remaining, second value is request limit. :type: (int, int) """ remaining, limit = self.__requester.rate_limiting if limit < 0: self.get_rate_limit() return self.__requester.rate_limiting @property def rate_limiting_resettime(self): """ Unix timestamp indicating when rate limiting will reset. :type: int """ if self.__requester.rate_limiting_resettime == 0: self.get_rate_limit() return self.__requester.rate_limiting_resettime def get_rate_limit(self): """ Rate limit status for different resources (core/search/graphql). :calls: `GET /rate_limit <http://developer.github.com/v3/rate_limit>`_ :rtype: :class:`github.RateLimit.RateLimit` """ headers, data = self.__requester.requestJsonAndCheck( 'GET', '/rate_limit' ) return RateLimit.RateLimit(self.__requester, headers, data["resources"], True) @property def oauth_scopes(self): """ :type: list of string """ return self.__requester.oauth_scopes def get_license(self, key=github.GithubObject.NotSet): """ :calls: `GET /license/:license <https://developer.github.com/v3/licenses/#get-an-individual-license>`_ :param key: string :rtype: :class:`github.License.License` """ assert isinstance(key, (str, unicode)), key headers, data = self.__requester.requestJsonAndCheck( "GET", "/licenses/" + key ) return github.License.License(self.__requester, headers, data, completed=True) def get_licenses(self): """ :calls: `GET /licenses <https://developer.github.com/v3/licenses/#list-all-licenses>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.License.License` """ url_parameters = dict() return github.PaginatedList.PaginatedList( github.License.License, self.__requester, "/licenses", url_parameters ) def get_user(self, login=github.GithubObject.NotSet): """ :calls: `GET /users/:user <http://developer.github.com/v3/users>`_ or `GET /user <http://developer.github.com/v3/users>`_ :param login: string :rtype: :class:`github.NamedUser.NamedUser` """ assert login is github.GithubObject.NotSet or isinstance(login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {}, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestJsonAndCheck( "GET", "/users/" + login ) return github.NamedUser.NamedUser(self.__requester, headers, data, completed=True) def get_users(self, since=github.GithubObject.NotSet): """ :calls: `GET /users <http://developer.github.com/v3/users>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self.__requester, "/users", url_parameters ) def get_organization(self, login): """ :calls: `GET /orgs/:org <http://developer.github.com/v3/orgs>`_ :param login: string :rtype: :class:`github.Organization.Organization` """ assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestJsonAndCheck( "GET", "/orgs/" + login ) return github.Organization.Organization(self.__requester, headers, data, completed=True) def get_organizations(self, since=github.GithubObject.NotSet): """ :calls: `GET /organizations <http://developer.github.com/v3/orgs#list-all-organizations>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Organization.Organization` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList( github.Organization.Organization, self.__requester, "/organizations", url_parameters ) def get_repo(self, full_name_or_id, lazy=False): """ :calls: `GET /repos/:owner/:repo <http://developer.github.com/v3/repos>`_ or `GET /repositories/:id <http://developer.github.com/v3/repos>`_ :rtype: :class:`github.Repository.Repository` """ assert isinstance(full_name_or_id, (str, unicode, int, long)), full_name_or_id url_base = "/repositories/" if isinstance(full_name_or_id, int) or isinstance(full_name_or_id, long) else "/repos/" url = "%s%s" % (url_base, full_name_or_id) if lazy: return Repository.Repository(self.__requester, {}, {"url": url}, completed=False) headers, data = self.__requester.requestJsonAndCheck( "GET", "%s%s" % (url_base, full_name_or_id) ) return Repository.Repository(self.__requester, headers, data, completed=True) def get_repos(self, since=github.GithubObject.NotSet, visibility=github.GithubObject.NotSet): """ :calls: `GET /repositories <http://developer.github.com/v3/repos/#list-all-public-repositories>`_ :param since: integer :param visibility: string ('all','public') :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since if visibility is not github.GithubObject.NotSet: assert visibility in ('public', 'all'), visibility url_parameters["visibility"] = visibility return github.PaginatedList.PaginatedList( github.Repository.Repository, self.__requester, "/repositories", url_parameters ) def get_project(self, id): """ :calls: `GET /projects/:project_id <https://developer.github.com/v3/projects/#get-a-project>`_ :rtype: :class:`github.Project.Project` :param id: integer """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/projects/%d" % (id), headers={"Accept": Consts.mediaTypeProjectsPreview} ) return github.Project.Project(self.__requester, headers, data, completed=True) def get_gist(self, id): """ :calls: `GET /gists/:id <http://developer.github.com/v3/gists>`_ :param id: string :rtype: :class:`github.Gist.Gist` """ assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestJsonAndCheck( "GET", "/gists/" + id ) return github.Gist.Gist(self.__requester, headers, data, completed=True) def get_gists(self, since=github.GithubObject.NotSet): """ :calls: `GET /gists/public <http://developer.github.com/v3/gists>`_ :param since: datetime.datetime format YYYY-MM-DDTHH:MM:SSZ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Gist.Gist` """ assert since is github.GithubObject.NotSet or isinstance(since, datetime.datetime), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since.strftime("%Y-%m-%dT%H:%M:%SZ") return github.PaginatedList.PaginatedList( github.Gist.Gist, self.__requester, "/gists/public", url_parameters ) def search_repositories(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/repositories <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('stars', 'forks', 'updated') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert sort in ('stars', 'forks', 'updated'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Repository.Repository, self.__requester, "/search/repositories", url_parameters ) def search_users(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/users <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('followers', 'repositories', 'joined') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: assert sort in ('followers', 'repositories', 'joined'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self.__requester, "/search/users", url_parameters ) def search_issues(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/issues <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('comments', 'created', 'updated') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Issue.Issue` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: assert sort in ('comments', 'created', 'updated'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Issue.Issue, self.__requester, "/search/issues", url_parameters ) def search_code(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, highlight=False, **qualifiers): """ :calls: `GET /search/code <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('indexed') :param order: string ('asc', 'desc') :param highlight: boolean (True, False) :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.ContentFile.ContentFile` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert sort in ('indexed',), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" headers = {"Accept": Consts.highLightSearchPreview} if highlight else None return github.PaginatedList.PaginatedList( github.ContentFile.ContentFile, self.__requester, "/search/code", url_parameters, headers=headers ) def search_commits(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/commits <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('author-date', 'committer-date') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Commit.Commit` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert sort in ('author-date', 'committer-date'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Commit.Commit, self.__requester, "/search/commits", url_parameters, headers={ "Accept": Consts.mediaTypeCommitSearchPreview } ) def search_topics(self, query, **qualifiers): """ :calls: `GET /search/topics <http://developer.github.com/v3/search>`_ :param query: string :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Topic.Topic` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Topic.Topic, self.__requester, "/search/topics", url_parameters, headers={ "Accept": Consts.mediaTypeTopicsPreview } ) def render_markdown(self, text, context=github.GithubObject.NotSet): """ :calls: `POST /markdown <http://developer.github.com/v3/markdown>`_ :param text: string :param context: :class:`github.Repository.Repository` :rtype: string """ assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance(context, github.Repository.Repository), context post_parameters = { "text": text } if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestJson( "POST", "/markdown", input=post_parameters ) return data def get_hook(self, name): """ :calls: `GET /hooks/:name <http://developer.github.com/v3/repos/hooks/>`_ :param name: string :rtype: :class:`github.HookDescription.HookDescription` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/hooks/" + name ) return HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) def get_hooks(self): """ :calls: `GET /hooks <http://developer.github.com/v3/repos/hooks/>`_ :rtype: list of :class:`github.HookDescription.HookDescription` """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/hooks" ) return [HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) for attributes in data] def get_gitignore_templates(self): """ :calls: `GET /gitignore/templates <http://developer.github.com/v3/gitignore>`_ :rtype: list of string """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates" ) return data def get_gitignore_template(self, name): """ :calls: `GET /gitignore/templates/:name <http://developer.github.com/v3/gitignore>`_ :rtype: :class:`github.GitignoreTemplate.GitignoreTemplate` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates/" + name ) return GitignoreTemplate.GitignoreTemplate(self.__requester, headers, attributes, completed=True) def get_emojis(self): """ :calls: `GET /emojis <http://developer.github.com/v3/emojis/>`_ :rtype: dictionary of type => url for emoji` """ headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/emojis" ) return attributes def create_from_raw_data(self, klass, raw_data, headers={}): """ Creates an object from raw_data previously obtained by :attr:`github.GithubObject.GithubObject.raw_data`, and optionaly headers previously obtained by :attr:`github.GithubObject.GithubObject.raw_headers`. :param klass: the class of the object to create :param raw_data: dict :param headers: dict :rtype: instance of class ``klass`` """ return klass(self.__requester, headers, raw_data, completed=True) def dump(self, obj, file, protocol=0): """ Dumps (pickles) a PyGithub object to a file-like object. Some effort is made to not pickle sensitive informations like the Github credentials used in the :class:`Github` instance. But NO EFFORT is made to remove sensitive information from the object's attributes. :param obj: the object to pickle :param file: the file-like object to pickle to :param protocol: the `pickling protocol <http://docs.python.org/2.7/library/pickle.html#data-stream-format>`_ """ pickle.dump((obj.__class__, obj.raw_data, obj.raw_headers), file, protocol) def load(self, f): """ Loads (unpickles) a PyGithub object from a file-like object. :param f: the file-like object to unpickle from :return: the unpickled object """ return self.create_from_raw_data(*pickle.load(f)) def get_installation(self, id): """ :param id: :return: """ return Installation.Installation(self.__requester, headers={}, attributes={"id": id}, completed=True)
def __init__( self, login=None, password=None ): self.__requester = Requester( login=login, password=password )
def Perform(self, url, args, withAuth): res = Requester.Perform(self, url, args, withAuth) if res["result"] != "success": raise Exception("Request failed.") return res
class Github(object): """ This is the main class you instanciate to access the Github API v3. Optional parameters allow different authentication methods. """ def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=DEFAULT_PER_PAGE): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int """ assert login_or_token is None or isinstance( login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance( client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page) def __get_FIX_REPO_GET_GIT_REF(self): """ :type: bool """ return self.__requester.FIX_REPO_GET_GIT_REF def __set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF) def __get_per_page(self): """ :type: int """ return self.__requester.per_page def __set_per_page(self, value): self.__requester.per_page = value per_page = property(__get_per_page, __set_per_page) @property def rate_limiting(self): """ :type: (int, int) """ return self.__requester.rate_limiting @property def oauth_scopes(self): """ :type: list of string """ return self.__requester.oauth_scopes def get_user(self, login=github.GithubObject.NotSet): """ :calls: `GET /users/:user <http://developer.github.com/v3/todo>`_ :param login: string :rtype: :class:`github.NamedUser.NamedUser` """ assert login is github.GithubObject.NotSet or isinstance( login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestJsonAndCheck( "GET", "/users/" + login, None, None) return github.NamedUser.NamedUser(self.__requester, data, completed=True) def get_organization(self, login): """ :calls: `GET /orgs/:org <http://developer.github.com/v3/todo>`_ :param login: string :rtype: :class:`github.Organization.Organization` """ assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestJsonAndCheck( "GET", "/orgs/" + login, None, None) return github.Organization.Organization(self.__requester, data, completed=True) def get_repo(self, full_name): """ :calls: `GET /repos/:user/:repo <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.Repository.Repository` """ assert isinstance(full_name, (str, unicode)), full_name headers, data = self.__requester.requestJsonAndCheck( "GET", "/repos/" + full_name, None, None) return Repository.Repository(self.__requester, data, completed=True) def get_gist(self, id): """ :calls: `GET /gists/:id <http://developer.github.com/v3/todo>`_ :param id: string :rtype: :class:`github.Gist.Gist` """ assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestJsonAndCheck( "GET", "/gists/" + id, None, None) return github.Gist.Gist(self.__requester, data, completed=True) def get_gists(self): """ :calls: `GET /gists/public <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Gist.Gist` """ return github.PaginatedList.PaginatedList(github.Gist.Gist, self.__requester, "/gists/public", None) def legacy_search_repos(self, keyword, language=github.GithubObject.NotSet): """ :calls: `GET /legacy/repos/search/:keyword <http://developer.github.com/v3/todo>`_ :param keyword: string :param language: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(keyword, (str, unicode)), keyword assert language is github.GithubObject.NotSet or isinstance( language, (str, unicode)), language args = {} if language is github.GithubObject.NotSet else { "language": language } return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote(keyword), args, self.__requester, "repositories", Legacy.convertRepo, github.Repository.Repository, ) def legacy_search_users(self, keyword): """ :calls: `GET /legacy/user/search/:keyword <http://developer.github.com/v3/todo>`_ :param keyword: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(keyword, (str, unicode)), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote(keyword), {}, self.__requester, "users", Legacy.convertUser, github.NamedUser.NamedUser, ) def legacy_search_user_by_email(self, email): """ :calls: `GET /legacy/user/email/:email <http://developer.github.com/v3/todo>`_ :param email: string :rtype: :class:`github.NamedUser.NamedUser` """ assert isinstance(email, (str, unicode)), email headers, data = self.__requester.requestJsonAndCheck( "GET", "/legacy/user/email/" + email, None, None) return github.NamedUser.NamedUser(self.__requester, Legacy.convertUser(data["user"]), completed=False) def render_markdown(self, text, context=github.GithubObject.NotSet): """ :calls: `POST /markdown <http://developer.github.com/v3/todo>`_ :param text: string :param context: :class:`github.Repository.Repository` :rtype: string """ assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance( context, github.Repository.Repository), context post_parameters = {"text": text} if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestJson( "POST", "/markdown", None, post_parameters) return data def get_hooks(self): """ :calls: `GET /hooks <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.HookDescription.HookDescription` """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/hooks", None, None) return [ HookDescription.HookDescription(self.__requester, attributes, completed=True) for attributes in data ] def get_gitignore_templates(self): """ :calls: `GET /gitignore/templates <http://developer.github.com/v3/todo>`_ :rtype: list of string """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates", None, None) return data def get_gitignore_template(self, name): """ :calls: `GET /gitignore/templates/:name <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.GitignoreTemplate.GitignoreTemplate` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates/" + name, None, None) return GitignoreTemplate.GitignoreTemplate(self.__requester, attributes, completed=True) def create_from_raw_data(self, klass, raw_data): """ Creates an object from raw_data previously obtained by :attr:`github.GithubObject.GithubObject.raw_data` :param klass: the class of the object to create :param raw_data: dict :rtype: instance of class ``klass`` """ return klass(self.__requester, raw_data, completed=True)
class foodRequester(): def __init__(self) -> None: # get all the api keys load_dotenv() self.rapidAPIKey = os.getenv('RAPID_API_KEY') # dictionaries containing api urls and host urls from rapid api self.apiURLs = { # each key maps to a list with the first idex containing the api url and # the second containing the host url "edamam": [os.getenv('EDAMAM_URL'), os.getenv('EDAMAM_HOST_URL')], # ... More to add } self.requester = Requester() def getAPIURLs(self, key: str): """ Get the host url and the api url from the class for making requests through rapid api Make sure the api and host urls have the same keys for the same api """ hostURL = self.apiURLs[key][1] apiURL = self.apiURLs[key][0] return hostURL, apiURL # get recipes from edamam specifically # TODO: understand search query parameters to make a get request # there are alot # issue #7 Github def requestEdamam(self, queryOrId, startIndex=0, endIndex=20, numIngreds=0, dietLabel='', healthLabel='', cuisineType='', mealType='', dishType='', calories=0, time=30, excluded=''): """ queryOrId: the search term to define the request (required) all of other parameters are optional: startIndex and endIndex: parameters to define where to pull recipes (prevents pulling entire database each time) numIngreds: number of ingredients to make recipe the following params are enums with string labels defined on edamam docs dietLabel, healthLabel, cuisineType, mealType, dishType The following params have a range type which comes in three forms: MIN+, MIN-MAX, or MAX calories time excluded: param that takes strings of ingredients to exclude from search """ params = { "q": queryOrId, "from": startIndex, "to": endIndex, "ingr": numIngreds, # ... below will be the other params for when we know how to use each } edamamHURL, edamamAURL = self.getAPIURLs("edamam") response = self.requester.makeGETRequest(edamamAURL, edamamHURL, params=params)
class Github(object): """ This is the main class you instanciate to access the Github API v3. Optional parameters allow different authentication methods. """ def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None, per_page=DEFAULT_PER_PAGE): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int """ assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page) def __get_FIX_REPO_GET_GIT_REF(self): """ :type: bool """ return self.__requester.FIX_REPO_GET_GIT_REF def __set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF) def __get_per_page(self): """ :type: int """ return self.__requester.per_page def __set_per_page(self, value): self.__requester.per_page = value per_page = property(__get_per_page, __set_per_page) @property def rate_limiting(self): """ :type: (int, int) """ return self.__requester.rate_limiting @property def oauth_scopes(self): """ :type: list of string """ return self.__requester.oauth_scopes def get_user(self, login=github.GithubObject.NotSet): """ :calls: `GET /users/:user <http://developer.github.com/v3/todo>`_ :param login: string :rtype: :class:`github.NamedUser.NamedUser` """ assert login is github.GithubObject.NotSet or isinstance(login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestJsonAndCheck( "GET", "/users/" + login, None, None ) return github.NamedUser.NamedUser(self.__requester, data, completed=True) def get_organization(self, login): """ :calls: `GET /orgs/:org <http://developer.github.com/v3/todo>`_ :param login: string :rtype: :class:`github.Organization.Organization` """ assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestJsonAndCheck( "GET", "/orgs/" + login, None, None ) return github.Organization.Organization(self.__requester, data, completed=True) def get_repo(self, full_name): """ :calls: `GET /repos/:user/:repo <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.Repository.Repository` """ assert isinstance(full_name, (str, unicode)), full_name headers, data = self.__requester.requestJsonAndCheck( "GET", "/repos/" + full_name, None, None ) return Repository.Repository(self.__requester, data, completed=True) def get_gist(self, id): """ :calls: `GET /gists/:id <http://developer.github.com/v3/todo>`_ :param id: string :rtype: :class:`github.Gist.Gist` """ assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestJsonAndCheck( "GET", "/gists/" + id, None, None ) return github.Gist.Gist(self.__requester, data, completed=True) def get_gists(self): """ :calls: `GET /gists/public <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Gist.Gist` """ return github.PaginatedList.PaginatedList( github.Gist.Gist, self.__requester, "/gists/public", None ) def legacy_search_repos(self, keyword, language=github.GithubObject.NotSet): """ :calls: `GET /legacy/repos/search/:keyword <http://developer.github.com/v3/todo>`_ :param keyword: string :param language: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(keyword, (str, unicode)), keyword assert language is github.GithubObject.NotSet or isinstance(language, (str, unicode)), language args = {} if language is github.GithubObject.NotSet else {"language": language} return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote(keyword), args, self.__requester, "repositories", Legacy.convertRepo, github.Repository.Repository, ) def legacy_search_users(self, keyword): """ :calls: `GET /legacy/user/search/:keyword <http://developer.github.com/v3/todo>`_ :param keyword: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(keyword, (str, unicode)), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote(keyword), {}, self.__requester, "users", Legacy.convertUser, github.NamedUser.NamedUser, ) def legacy_search_user_by_email(self, email): """ :calls: `GET /legacy/user/email/:email <http://developer.github.com/v3/todo>`_ :param email: string :rtype: :class:`github.NamedUser.NamedUser` """ assert isinstance(email, (str, unicode)), email headers, data = self.__requester.requestJsonAndCheck( "GET", "/legacy/user/email/" + email, None, None ) return github.NamedUser.NamedUser(self.__requester, Legacy.convertUser(data["user"]), completed=False) def render_markdown(self, text, context=github.GithubObject.NotSet): """ :calls: `POST /markdown <http://developer.github.com/v3/todo>`_ :param text: string :param context: :class:`github.Repository.Repository` :rtype: string """ assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance(context, github.Repository.Repository), context post_parameters = { "text": text } if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestJson( "POST", "/markdown", None, post_parameters ) return data def get_hooks(self): """ :calls: `GET /hooks <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.HookDescription.HookDescription` """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/hooks", None, None ) return [HookDescription.HookDescription(self.__requester, attributes, completed=True) for attributes in data] def get_gitignore_templates(self): """ :calls: `GET /gitignore/templates <http://developer.github.com/v3/todo>`_ :rtype: list of string """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates", None, None ) return data def get_gitignore_template(self, name): """ :calls: `GET /gitignore/templates/:name <http://developer.github.com/v3/todo>`_ :rtype: :class:`github.GitignoreTemplate.GitignoreTemplate` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates/" + name, None, None ) return GitignoreTemplate.GitignoreTemplate(self.__requester, attributes, completed=True) def create_from_raw_data(self, klass, raw_data): """ Creates an object from raw_data previously obtained by :attr:`github.GithubObject.GithubObject.raw_data` :param klass: the class of the object to create :param raw_data: dict :rtype: instance of class ``klass`` """ return klass(self.__requester, raw_data, completed=True)
class Github(object): def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None): self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent) def get_FIX_REPO_GET_GIT_REF(self): return self.__requester.FIX_REPO_GET_GIT_REF def set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(get_FIX_REPO_GET_GIT_REF, set_FIX_REPO_GET_GIT_REF) @property def rate_limiting(self): return self.__requester.rate_limiting def get_user(self, login=GithubObject.NotSet): assert login is GithubObject.NotSet or isinstance( login, (str, unicode)), login if login is GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestAndCheck( "GET", "/users/" + login, None, None) return NamedUser.NamedUser(self.__requester, data, completed=True) def get_organization(self, login): assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestAndCheck( "GET", "/orgs/" + login, None, None) return Organization.Organization(self.__requester, data, completed=True) def get_gist(self, id): assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestAndCheck( "GET", "/gists/" + id, None, None) return Gist.Gist(self.__requester, data, completed=True) def get_gists(self): return PaginatedList.PaginatedList(Gist.Gist, self.__requester, "/gists/public", None) def legacy_search_repos(self, keyword, language=GithubObject.NotSet): assert isinstance(keyword, (str, unicode)), keyword assert language is GithubObject.NotSet or isinstance( language, (str, unicode)), language args = {} if language is GithubObject.NotSet else { "language": language } return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote(keyword), args, self.__requester, "repositories", Legacy.convertRepo, Repository.Repository, ) def legacy_search_users(self, keyword): assert isinstance(keyword, (str, unicode)), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote(keyword), {}, self.__requester, "users", Legacy.convertUser, NamedUser.NamedUser, ) def legacy_search_user_by_email(self, email): assert isinstance(email, (str, unicode)), email headers, data = self.__requester.requestAndCheck( "GET", "/legacy/user/email/" + email, None, None) return NamedUser.NamedUser(self.__requester, Legacy.convertUser(data["user"]), completed=False) def render_markdown(self, text, context=GithubObject.NotSet): assert isinstance(text, (str, unicode)), text assert context is GithubObject.NotSet or isinstance( context, Repository.Repository), context post_parameters = {"text": text} if context is not GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestRaw( "POST", "/markdown", None, post_parameters) return data def get_hooks(self): headers, data = self.__requester.requestAndCheck( "GET", "/hooks", None, None) return [ HookDescription.HookDescription(self.__requester, attributes, completed=True) for attributes in data ]
class Github( object ): def __init__( self, login_or_token = None, password = None ): self.__requester = Requester( login_or_token, password ) @property def rate_limiting( self ): return self.__requester.rate_limiting def get_user( self, login = None ): if login is None: return AuthenticatedUser.AuthenticatedUser( self.__requester, { "url": "https://api.github.com/user" }, completed = False ) else: headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/users/" + login, None, None ) return NamedUser.NamedUser( self.__requester, data, completed = True ) def get_organization( self, login ): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/orgs/" + login, None, None ) return Organization.Organization( self.__requester, data, completed = True ) def get_gist( self, id ): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/gists/" + str( id ), None, None ) return Gist.Gist( self.__requester, data, completed = True ) def get_gists( self ): headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/gists/public", None, None ) return PaginatedList.PaginatedList( Gist.Gist, self.__requester, headers, data ) def legacy_search_repos( self, keyword, language = GithubObject.NotSet ): assert isinstance( keyword, ( str, unicode ) ), keyword assert language is GithubObject.NotSet or isinstance( language, ( str, unicode ) ), language args = {} if language is GithubObject.NotSet else { "language": language } return Legacy.PaginatedList( "https://api.github.com/legacy/repos/search/" + urllib.quote( keyword ), args, self.__requester, "repositories", Legacy.convertRepo, Repository.Repository, ) def legacy_search_users( self, keyword ): assert isinstance( keyword, ( str, unicode ) ), keyword return Legacy.PaginatedList( "https://api.github.com/legacy/user/search/" + urllib.quote( keyword ), {}, self.__requester, "users", Legacy.convertUser, NamedUser.NamedUser, ) def legacy_search_user_by_email( self, email ): assert isinstance( email, ( str, unicode ) ), email headers, data = self.__requester.requestAndCheck( "GET", "https://api.github.com/legacy/user/email/" + email, None, None ) return NamedUser.NamedUser( self.__requester, Legacy.convertUser( data[ "user" ] ), completed = False )
class Github(object): """ This is the main class you instanciate to access the Github API v3. Optional parameters allow different authentication methods. """ def __init__( self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent="PyGithub/Python", per_page=DEFAULT_PER_PAGE, ): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int """ assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent self.__requester = Requester( login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page ) def __get_FIX_REPO_GET_GIT_REF(self): """ :type: bool """ return self.__requester.FIX_REPO_GET_GIT_REF def __set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF) def __get_per_page(self): """ :type: int """ return self.__requester.per_page def __set_per_page(self, value): self.__requester.per_page = value # v2: Remove this property? Why should it be necessary to read/modify it after construction per_page = property(__get_per_page, __set_per_page) # v2: Provide a unified way to access values of headers of last response # v2: (and add/keep ad hoc properties for specific useful headers like rate limiting, oauth scopes, etc.) # v2: Return an instance of a class: using a tuple did not allow to add a field "resettime" @property def rate_limiting(self): """ First value is requests remaining, second value is request limit. :type: (int, int) """ remaining, limit = self.__requester.rate_limiting if limit < 0: self.get_rate_limit() return self.__requester.rate_limiting @property def rate_limiting_resettime(self): """ Unix timestamp indicating when rate limiting will reset. :type: int """ if self.__requester.rate_limiting_resettime == 0: self.get_rate_limit() return self.__requester.rate_limiting_resettime def get_rate_limit(self): """ Don't forget you can access the rate limit returned in headers of last Github API v3 response, by :attr:`github.MainClass.Github.rate_limiting` and :attr:`github.MainClass.Github.rate_limiting_resettime`. :calls: `GET /rate_limit <http://developer.github.com/v3/rate_limit>`_ :rtype: :class:`github.RateLimit.RateLimit` """ headers, attributes = self.__requester.requestJsonAndCheck("GET", "/rate_limit") return RateLimit.RateLimit(self.__requester, headers, attributes, True) @property def oauth_scopes(self): """ :type: list of string """ return self.__requester.oauth_scopes def get_user(self, login=github.GithubObject.NotSet): """ :calls: `GET /users/:user <http://developer.github.com/v3/users>`_ or `GET /user <http://developer.github.com/v3/users>`_ :param login: string :rtype: :class:`github.NamedUser.NamedUser` """ assert login is github.GithubObject.NotSet or isinstance(login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {}, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestJsonAndCheck("GET", "/users/" + login) return github.NamedUser.NamedUser(self.__requester, headers, data, completed=True) def get_users(self, since=github.GithubObject.NotSet): """ :calls: `GET /users <http://developer.github.com/v3/users>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self.__requester, "/users", url_parameters ) def get_organization(self, login): """ :calls: `GET /orgs/:org <http://developer.github.com/v3/orgs>`_ :param login: string :rtype: :class:`github.Organization.Organization` """ assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestJsonAndCheck("GET", "/orgs/" + login) return github.Organization.Organization(self.__requester, headers, data, completed=True) def get_repo(self, full_name): """ :calls: `GET /repos/:owner/:repo <http://developer.github.com/v3/repos>`_ :rtype: :class:`github.Repository.Repository` """ assert isinstance(full_name, (str, unicode)), full_name headers, data = self.__requester.requestJsonAndCheck("GET", "/repos/" + full_name) return Repository.Repository(self.__requester, headers, data, completed=True) def get_repos(self, since=github.GithubObject.NotSet): """ :calls: `GET /repositories <http://developer.github.com/v3/repos/#list-all-public-repositories>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList( github.Repository.Repository, self.__requester, "/repositories", url_parameters ) def get_gist(self, id): """ :calls: `GET /gists/:id <http://developer.github.com/v3/gists>`_ :param id: string :rtype: :class:`github.Gist.Gist` """ assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestJsonAndCheck("GET", "/gists/" + id) return github.Gist.Gist(self.__requester, headers, data, completed=True) def get_gists(self): """ :calls: `GET /gists/public <http://developer.github.com/v3/gists>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Gist.Gist` """ return github.PaginatedList.PaginatedList(github.Gist.Gist, self.__requester, "/gists/public", None) def legacy_search_repos(self, keyword, language=github.GithubObject.NotSet): """ :calls: `GET /legacy/repos/search/:keyword <http://developer.github.com/v3/search/legacy>`_ :param keyword: string :param language: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(keyword, (str, unicode)), keyword assert language is github.GithubObject.NotSet or isinstance(language, (str, unicode)), language args = {} if language is github.GithubObject.NotSet else {"language": language} return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote(keyword), args, self.__requester, "repositories", Legacy.convertRepo, github.Repository.Repository, ) def legacy_search_users(self, keyword): """ :calls: `GET /legacy/user/search/:keyword <http://developer.github.com/v3/search/legacy>`_ :param keyword: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(keyword, (str, unicode)), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote(keyword), {}, self.__requester, "users", Legacy.convertUser, github.NamedUser.NamedUser, ) def legacy_search_user_by_email(self, email): """ :calls: `GET /legacy/user/email/:email <http://developer.github.com/v3/search/legacy>`_ :param email: string :rtype: :class:`github.NamedUser.NamedUser` """ assert isinstance(email, (str, unicode)), email headers, data = self.__requester.requestJsonAndCheck("GET", "/legacy/user/email/" + email) return github.NamedUser.NamedUser(self.__requester, headers, Legacy.convertUser(data["user"]), completed=False) def render_markdown(self, text, context=github.GithubObject.NotSet): """ :calls: `POST /markdown <http://developer.github.com/v3/markdown>`_ :param text: string :param context: :class:`github.Repository.Repository` :rtype: string """ assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance(context, github.Repository.Repository), context post_parameters = {"text": text} if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestJson("POST", "/markdown", input=post_parameters) return data def get_hook(self, name): """ :calls: `GET /hooks/:name <http://developer.github.com/v3/repos/hooks/>`_ :param name: string :rtype: :class:`github.HookDescription.HookDescription` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck("GET", "/hooks/" + name) return HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) def get_hooks(self): """ :calls: `GET /hooks <http://developer.github.com/v3/repos/hooks/>`_ :rtype: list of :class:`github.HookDescription.HookDescription` """ headers, data = self.__requester.requestJsonAndCheck("GET", "/hooks") return [ HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) for attributes in data ] def get_gitignore_templates(self): """ :calls: `GET /gitignore/templates <http://developer.github.com/v3/gitignore>`_ :rtype: list of string """ headers, data = self.__requester.requestJsonAndCheck("GET", "/gitignore/templates") return data def get_gitignore_template(self, name): """ :calls: `GET /gitignore/templates/:name <http://developer.github.com/v3/gitignore>`_ :rtype: :class:`github.GitignoreTemplate.GitignoreTemplate` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck("GET", "/gitignore/templates/" + name) return GitignoreTemplate.GitignoreTemplate(self.__requester, headers, attributes, completed=True) def get_emojis(self): """ :calls: `GET /emojis <http://developer.github.com/v3/emojis/>`_ :rtype: dictionary of type => url for emoji` """ headers, attributes = self.__requester.requestJsonAndCheck("GET", "/emojis") return attributes def create_from_raw_data(self, klass, raw_data, headers={}): """ Creates an object from raw_data previously obtained by :attr:`github.GithubObject.GithubObject.raw_data`, and optionaly headers previously obtained by :attr:`github.GithubObject.GithubObject.raw_headers`. :param klass: the class of the object to create :param raw_data: dict :param headers: dict :rtype: instance of class ``klass`` """ return klass(self.__requester, headers, raw_data, completed=True) def dump(self, obj, file, protocol=0): """ Dumps (pickles) a PyGithub object to a file-like object. Some effort is made to not pickle sensitive informations like the Github credentials used in the :class:`Github` instance. But NO EFFORT is made to remove sensitive information from the object's attributes. :param obj: the object to pickle :param file: the file-like object to pickle to :param protocol: the `pickling protocol <http://docs.python.org/2.7/library/pickle.html#data-stream-format>`_ """ pickle.dump((obj.__class__, obj.raw_data, obj.raw_headers), file, protocol) def load(self, f): """ Loads (unpickles) a PyGithub object from a file-like object. :param f: the file-like object to unpickle from :return: the unpickled object """ return self.create_from_raw_data(*pickle.load(f)) def get_api_status(self): """ This doesn't work with a Github Enterprise installation, because it always targets https://status.github.com. :calls: `GET /api/status.json <https://status.github.com/api>`_ :rtype: :class:`github.Status.Status` """ headers, attributes = self.__requester.requestJsonAndCheck("GET", "/api/status.json", cnx="status") return Status.Status(self.__requester, headers, attributes, completed=True) def get_last_api_status_message(self): """ This doesn't work with a Github Enterprise installation, because it always targets https://status.github.com. :calls: `GET /api/last-message.json <https://status.github.com/api>`_ :rtype: :class:`github.StatusMessage.StatusMessage` """ headers, attributes = self.__requester.requestJsonAndCheck("GET", "/api/last-message.json", cnx="status") return StatusMessage.StatusMessage(self.__requester, headers, attributes, completed=True) def get_api_status_messages(self): """ This doesn't work with a Github Enterprise installation, because it always targets https://status.github.com. :calls: `GET /api/messages.json <https://status.github.com/api>`_ :rtype: list of :class:`github.StatusMessage.StatusMessage` """ headers, data = self.__requester.requestJsonAndCheck("GET", "/api/messages.json", cnx="status") return [ StatusMessage.StatusMessage(self.__requester, headers, attributes, completed=True) for attributes in data ]
def __init__(self, url): self.__requester = Requester(url)
def __init__(self, api_key=None): self.base_requester = Requester(api_key)
def __init__( self, login_or_token = None, password = None, base_url = DEFAULT_BASE_URL, timeout = DEFAULT_TIMEOUT): self.__requester = Requester( login_or_token, password, base_url, timeout )
class Github(object): def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None): self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent) def get_FIX_REPO_GET_GIT_REF(self): return self.__requester.FIX_REPO_GET_GIT_REF def set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(get_FIX_REPO_GET_GIT_REF, set_FIX_REPO_GET_GIT_REF) @property def rate_limiting(self): return self.__requester.rate_limiting def get_user(self, login=github.GithubObject.NotSet): assert login is github.GithubObject.NotSet or isinstance(login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestAndCheck( "GET", "/users/" + login, None, None ) return github.NamedUser.NamedUser(self.__requester, data, completed=True) def get_organization(self, login): assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestAndCheck( "GET", "/orgs/" + login, None, None ) return github.Organization.Organization(self.__requester, data, completed=True) def get_repo(self, repo_name, user_name=None): assert isinstance(repo_name, (str, unicode)), repo_name if user_name is not None: url = "/repos/%s/%s" % (user_name, repo_name) else: url = "/repos/%s" % repo_name headers, data = self.__requester.requestAndCheck( "GET", url, None, None ) return Repository.Repository(self.__requester, data, completed=True) def get_gist(self, id): assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestAndCheck( "GET", "/gists/" + id, None, None ) return github.Gist.Gist(self.__requester, data, completed=True) def get_gists(self): return github.PaginatedList.PaginatedList( github.Gist.Gist, self.__requester, "/gists/public", None ) def legacy_search_repos(self, keyword, language=github.GithubObject.NotSet): assert isinstance(keyword, (str, unicode)), keyword assert language is github.GithubObject.NotSet or isinstance(language, (str, unicode)), language args = {} if language is github.GithubObject.NotSet else {"language": language} return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote(keyword), args, self.__requester, "repositories", Legacy.convertRepo, github.Repository.Repository, ) def legacy_search_users(self, keyword): assert isinstance(keyword, (str, unicode)), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote(keyword), {}, self.__requester, "users", Legacy.convertUser, github.NamedUser.NamedUser, ) def legacy_search_user_by_email(self, email): assert isinstance(email, (str, unicode)), email headers, data = self.__requester.requestAndCheck( "GET", "/legacy/user/email/" + email, None, None ) return github.NamedUser.NamedUser(self.__requester, Legacy.convertUser(data["user"]), completed=False) def render_markdown(self, text, context=github.GithubObject.NotSet): assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance(context, github.Repository.Repository), context post_parameters = { "text": text } if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestRaw( "POST", "/markdown", None, post_parameters ) return data def get_hooks(self): headers, data = self.__requester.requestAndCheck( "GET", "/hooks", None, None ) return [HookDescription.HookDescription(self.__requester, attributes, completed=True) for attributes in data] def get_gitignore_templates(self): headers, data = self.__requester.requestAndCheck( "GET", "/gitignore/templates", None, None ) return data def get_gitignore_template(self, name): assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestAndCheck( "GET", "/gitignore/templates/" + name, None, None ) return GitignoreTemplate.GitignoreTemplate(self.__requester, attributes, completed=True)
def test_get_stock(self): r = Requester() print(r.get_stock("INX"))
def __init__( self, login_or_token = None, password = None ): self.__requester = Requester( login_or_token, password )
class Github(object): """ This is the main class you instantiate to access the Github API v3. Optional parameters allow different authentication methods. """ def __init__(self, login_or_token=None, password=None, jwt=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=DEFAULT_PER_PAGE, api_preview=False, verify=True, retry=None): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int :param verify: boolean or string :param retry: int or urllib3.util.retry.Retry object """ assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert jwt is None or isinstance(jwt, (str, unicode)), jwt assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent assert isinstance(api_preview, (bool)) assert retry is None or isinstance(retry, (int)) or isinstance(retry, (urllib3.util.Retry)) self.__requester = Requester(login_or_token, password, jwt, base_url, timeout, client_id, client_secret, user_agent, per_page, api_preview, verify, retry) def __get_FIX_REPO_GET_GIT_REF(self): """ :type: bool """ return self.__requester.FIX_REPO_GET_GIT_REF def __set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF) def __get_per_page(self): """ :type: int """ return self.__requester.per_page def __set_per_page(self, value): self.__requester.per_page = value # v2: Remove this property? Why should it be necessary to read/modify it after construction per_page = property(__get_per_page, __set_per_page) # v2: Provide a unified way to access values of headers of last response # v2: (and add/keep ad hoc properties for specific useful headers like rate limiting, oauth scopes, etc.) # v2: Return an instance of a class: using a tuple did not allow to add a field "resettime" @property def rate_limiting(self): """ First value is requests remaining, second value is request limit. :type: (int, int) """ remaining, limit = self.__requester.rate_limiting if limit < 0: self.get_rate_limit() return self.__requester.rate_limiting @property def rate_limiting_resettime(self): """ Unix timestamp indicating when rate limiting will reset. :type: int """ if self.__requester.rate_limiting_resettime == 0: self.get_rate_limit() return self.__requester.rate_limiting_resettime def get_rate_limit(self): """ Rate limit status for different resources (core/search/graphql). :calls: `GET /rate_limit <http://developer.github.com/v3/rate_limit>`_ :rtype: :class:`github.RateLimit.RateLimit` """ headers, data = self.__requester.requestJsonAndCheck( 'GET', '/rate_limit' ) return RateLimit.RateLimit(self.__requester, headers, data["resources"], True) @property def oauth_scopes(self): """ :type: list of string """ return self.__requester.oauth_scopes def get_license(self, key=github.GithubObject.NotSet): """ :calls: `GET /license/:license <https://developer.github.com/v3/licenses/#get-an-individual-license>`_ :param key: string :rtype: :class:`github.License.License` """ assert isinstance(key, (str, unicode)), key headers, data = self.__requester.requestJsonAndCheck( "GET", "/licenses/" + key ) return github.License.License(self.__requester, headers, data, completed=True) def get_licenses(self): """ :calls: `GET /licenses <https://developer.github.com/v3/licenses/#list-all-licenses>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.License.License` """ url_parameters = dict() return github.PaginatedList.PaginatedList( github.License.License, self.__requester, "/licenses", url_parameters ) def get_user(self, login=github.GithubObject.NotSet): """ :calls: `GET /users/:user <http://developer.github.com/v3/users>`_ or `GET /user <http://developer.github.com/v3/users>`_ :param login: string :rtype: :class:`github.NamedUser.NamedUser` """ assert login is github.GithubObject.NotSet or isinstance(login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {}, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestJsonAndCheck( "GET", "/users/" + login ) return github.NamedUser.NamedUser(self.__requester, headers, data, completed=True) def get_users(self, since=github.GithubObject.NotSet): """ :calls: `GET /users <http://developer.github.com/v3/users>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self.__requester, "/users", url_parameters ) def get_organization(self, login): """ :calls: `GET /orgs/:org <http://developer.github.com/v3/orgs>`_ :param login: string :rtype: :class:`github.Organization.Organization` """ assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestJsonAndCheck( "GET", "/orgs/" + login ) return github.Organization.Organization(self.__requester, headers, data, completed=True) def get_organizations(self, since=github.GithubObject.NotSet): """ :calls: `GET /organizations <http://developer.github.com/v3/orgs#list-all-organizations>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Organization.Organization` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self.__requester, "/organizations", url_parameters ) def get_repo(self, full_name_or_id, lazy=False): """ :calls: `GET /repos/:owner/:repo <http://developer.github.com/v3/repos>`_ or `GET /repositories/:id <http://developer.github.com/v3/repos>`_ :rtype: :class:`github.Repository.Repository` """ assert isinstance(full_name_or_id, (str, unicode, int, long)), full_name_or_id url_base = "/repositories/" if isinstance(full_name_or_id, int) or isinstance(full_name_or_id, long) else "/repos/" url = "%s%s" % (url_base, full_name_or_id) if lazy: return Repository.Repository(self.__requester, {}, {"url": url}, completed=False) headers, data = self.__requester.requestJsonAndCheck( "GET", "%s%s" % (url_base, full_name_or_id) ) return Repository.Repository(self.__requester, headers, data, completed=True) def get_repos(self, since=github.GithubObject.NotSet, visibility=github.GithubObject.NotSet): """ :calls: `GET /repositories <http://developer.github.com/v3/repos/#list-all-public-repositories>`_ :param since: integer :param visibility: string ('all','public') :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert since is github.GithubObject.NotSet or isinstance(since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since if visibility is not github.GithubObject.NotSet: assert visibility in ('public', 'all'), visibility url_parameters["visibility"] = visibility return github.PaginatedList.PaginatedList( github.Repository.Repository, self.__requester, "/repositories", url_parameters ) def get_project(self, id): """ :calls: `GET /projects/:project_id <https://developer.github.com/v3/projects/#get-a-project>`_ :rtype: :class:`github.Project.Project` :param id: integer """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/projects/%d" % (id), headers={"Accept": Consts.mediaTypeProjectsPreview} ) return github.Project.Project(self.__requester, headers, data, completed=True) def get_gist(self, id): """ :calls: `GET /gists/:id <http://developer.github.com/v3/gists>`_ :param id: string :rtype: :class:`github.Gist.Gist` """ assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestJsonAndCheck( "GET", "/gists/" + id ) return github.Gist.Gist(self.__requester, headers, data, completed=True) def get_gists(self, since=github.GithubObject.NotSet): """ :calls: `GET /gists/public <http://developer.github.com/v3/gists>`_ :param since: datetime.datetime format YYYY-MM-DDTHH:MM:SSZ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Gist.Gist` """ assert since is github.GithubObject.NotSet or isinstance(since, datetime.datetime), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since.strftime("%Y-%m-%dT%H:%M:%SZ") return github.PaginatedList.PaginatedList( github.Gist.Gist, self.__requester, "/gists/public", url_parameters ) def search_repositories(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/repositories <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('stars', 'forks', 'updated') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert sort in ('stars', 'forks', 'updated'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Repository.Repository, self.__requester, "/search/repositories", url_parameters ) def search_users(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/users <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('followers', 'repositories', 'joined') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: assert sort in ('followers', 'repositories', 'joined'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.NamedUser.NamedUser, self.__requester, "/search/users", url_parameters ) def search_issues(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/issues <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('comments', 'created', 'updated') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Issue.Issue` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: assert sort in ('comments', 'created', 'updated'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Issue.Issue, self.__requester, "/search/issues", url_parameters ) def search_code(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, highlight=False, **qualifiers): """ :calls: `GET /search/code <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('indexed') :param order: string ('asc', 'desc') :param highlight: boolean (True, False) :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.ContentFile.ContentFile` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert sort in ('indexed',), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" headers = {"Accept": Consts.highLightSearchPreview} if highlight else None return github.PaginatedList.PaginatedList( github.ContentFile.ContentFile, self.__requester, "/search/code", url_parameters, headers=headers ) def search_commits(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/commits <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('author-date', 'committer-date') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Commit.Commit` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert sort in ('author-date', 'committer-date'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: # pragma no branch (Should be covered) assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Commit.Commit, self.__requester, "/search/commits", url_parameters, headers={ "Accept": Consts.mediaTypeCommitSearchPreview } ) def search_topics(self, query, **qualifiers): """ :calls: `GET /search/topics <http://developer.github.com/v3/search>`_ :param query: string :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Topic.Topic` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.Topic.Topic, self.__requester, "/search/topics", url_parameters, headers={ "Accept": Consts.mediaTypeTopicsPreview } ) def render_markdown(self, text, context=github.GithubObject.NotSet): """ :calls: `POST /markdown <http://developer.github.com/v3/markdown>`_ :param text: string :param context: :class:`github.Repository.Repository` :rtype: string """ assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance(context, github.Repository.Repository), context post_parameters = { "text": text } if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestJson( "POST", "/markdown", input=post_parameters ) return data def get_hook(self, name): """ :calls: `GET /hooks/:name <http://developer.github.com/v3/repos/hooks/>`_ :param name: string :rtype: :class:`github.HookDescription.HookDescription` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/hooks/" + name ) return HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) def get_hooks(self): """ :calls: `GET /hooks <http://developer.github.com/v3/repos/hooks/>`_ :rtype: list of :class:`github.HookDescription.HookDescription` """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/hooks" ) return [HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) for attributes in data] def get_gitignore_templates(self): """ :calls: `GET /gitignore/templates <http://developer.github.com/v3/gitignore>`_ :rtype: list of string """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates" ) return data def get_gitignore_template(self, name): """ :calls: `GET /gitignore/templates/:name <http://developer.github.com/v3/gitignore>`_ :rtype: :class:`github.GitignoreTemplate.GitignoreTemplate` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates/" + name ) return GitignoreTemplate.GitignoreTemplate(self.__requester, headers, attributes, completed=True) def get_emojis(self): """ :calls: `GET /emojis <http://developer.github.com/v3/emojis/>`_ :rtype: dictionary of type => url for emoji` """ headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/emojis" ) return attributes def create_from_raw_data(self, klass, raw_data, headers={}): """ Creates an object from raw_data previously obtained by :attr:`github.GithubObject.GithubObject.raw_data`, and optionaly headers previously obtained by :attr:`github.GithubObject.GithubObject.raw_headers`. :param klass: the class of the object to create :param raw_data: dict :param headers: dict :rtype: instance of class ``klass`` """ return klass(self.__requester, headers, raw_data, completed=True) def dump(self, obj, file, protocol=0): """ Dumps (pickles) a PyGithub object to a file-like object. Some effort is made to not pickle sensitive informations like the Github credentials used in the :class:`Github` instance. But NO EFFORT is made to remove sensitive information from the object's attributes. :param obj: the object to pickle :param file: the file-like object to pickle to :param protocol: the `pickling protocol <http://docs.python.org/2.7/library/pickle.html#data-stream-format>`_ """ pickle.dump((obj.__class__, obj.raw_data, obj.raw_headers), file, protocol) def load(self, f): """ Loads (unpickles) a PyGithub object from a file-like object. :param f: the file-like object to unpickle from :return: the unpickled object """ return self.create_from_raw_data(*pickle.load(f)) def get_installation(self, id): """ :param id: :return: """ return Installation.Installation(self.__requester, headers={}, attributes={"id": id}, completed=True)
def __init__(self, _printing=False, _use_cache=False): Requester.__init__(self, _printing, _use_cache) # Table mapping response codes to messages; entries have the # form {code: (shortmessage, longmessage)}. self.responses = { 100: ('Continue', 'Request received, please continue'), 101: ('Switching Protocols', 'Switching to new protocol; obey Upgrade header'), 200: ('OK', 'Request fulfilled, document follows'), 201: ('Created', 'Document created, URL follows'), 202: ('Accepted', 'Request accepted, processing continues off-line'), 203: ('Non-Authoritative Information', 'Request fulfilled from cache'), 204: ('No Content', 'Request fulfilled, nothing follows'), 205: ('Reset Content', 'Clear input form for further input.'), 206: ('Partial Content', 'Partial content follows.'), 300: ('Multiple Choices', 'Object has several resources -- see URI list'), 301: ('Moved Permanently', 'Object moved permanently -- see URI list'), 302: ('Found', 'Object moved temporarily -- see URI list'), 303: ('See Other', 'Object moved -- see Method and URL list'), 304: ('Not Modified', 'Document has not changed since given time'), 305: ('Use Proxy', 'You must use proxy specified in Location to access this ' 'resource.'), 307: ('Temporary Redirect', 'Object moved temporarily -- see URI list'), 400: ('Bad Request', 'Bad request syntax or unsupported method'), 401: ('Unauthorized', 'No permission -- see authorization schemes'), 402: ('Payment Required', 'No payment -- see charging schemes'), 403: ('Forbidden', 'Request forbidden -- authorization will not help'), 404: ('Not Found', 'Nothing matches the given URI'), 405: ('Method Not Allowed', 'Specified method is invalid for this server.'), 406: ('Not Acceptable', 'URI not available in preferred format.'), 407: ('Proxy Authentication Required', 'You must authenticate with ' 'this proxy before proceeding.'), 408: ('Request Timeout', 'Request timed out; try again later.'), 409: ('Conflict', 'Request conflict.'), 410: ('Gone', 'URI no longer exists and has been permanently removed.'), 411: ('Length Required', 'Client must specify Content-Length.'), 412: ('Precondition Failed', 'Precondition in headers is false.'), 413: ('Request Entity Too Large', 'Entity is too large.'), 414: ('Request-URI Too Long', 'URI is too long.'), 415: ('Unsupported Media Type', 'Entity body in unsupported format.'), 416: ('Requested Range Not Satisfiable', 'Cannot satisfy request range.'), 417: ('Expectation Failed', 'Expect condition could not be satisfied.'), 500: ('Internal Server Error', 'Server got itself in trouble'), 501: ('Not Implemented', 'Server does not support this operation'), 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'), 503: ('Service Unavailable', 'The server cannot process the request due to a high load'), 504: ('Gateway Timeout', 'The gateway server did not receive a timely response'), 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'), }
def __init__(self, login_or_token=None, password=None): self.__requester = Requester(login_or_token, password)
class Github(object): """ This is the main class you instanciate to access the Github API v3. Optional parameters allow different authentication methods. """ def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=DEFAULT_PER_PAGE, api_preview=False): """ :param login_or_token: string :param password: string :param base_url: string :param timeout: integer :param client_id: string :param client_secret: string :param user_agent: string :param per_page: int """ assert login_or_token is None or isinstance( login_or_token, (str, unicode)), login_or_token assert password is None or isinstance(password, (str, unicode)), password assert isinstance(base_url, (str, unicode)), base_url assert isinstance(timeout, (int, long)), timeout assert client_id is None or isinstance(client_id, (str, unicode)), client_id assert client_secret is None or isinstance( client_secret, (str, unicode)), client_secret assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent assert isinstance(api_preview, (bool)) self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page, api_preview) def __get_FIX_REPO_GET_GIT_REF(self): """ :type: bool """ return self.__requester.FIX_REPO_GET_GIT_REF def __set_FIX_REPO_GET_GIT_REF(self, value): self.__requester.FIX_REPO_GET_GIT_REF = value FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF) def __get_per_page(self): """ :type: int """ return self.__requester.per_page def __set_per_page(self, value): self.__requester.per_page = value # v2: Remove this property? Why should it be necessary to read/modify it # after construction per_page = property(__get_per_page, __set_per_page) # v2: Provide a unified way to access values of headers of last response # v2: (and add/keep ad hoc properties for specific useful headers like rate limiting, oauth scopes, etc.) # v2: Return an instance of a class: using a tuple did not allow to add a # field "resettime" @property def rate_limiting(self): """ First value is requests remaining, second value is request limit. :type: (int, int) """ remaining, limit = self.__requester.rate_limiting if limit < 0: self.get_rate_limit() return self.__requester.rate_limiting @property def rate_limiting_resettime(self): """ Unix timestamp indicating when rate limiting will reset. :type: int """ if self.__requester.rate_limiting_resettime == 0: self.get_rate_limit() return self.__requester.rate_limiting_resettime def get_rate_limit(self): """ Don't forget you can access the rate limit returned in headers of last Github API v3 response, by :attr:`github.MainClass.Github.rate_limiting` and :attr:`github.MainClass.Github.rate_limiting_resettime`. :calls: `GET /rate_limit <http://developer.github.com/v3/rate_limit>`_ :rtype: :class:`github.RateLimit.RateLimit` """ headers, attributes = self.__requester.requestJsonAndCheck( 'GET', '/rate_limit') return RateLimit.RateLimit(self.__requester, headers, attributes, True) @property def oauth_scopes(self): """ :type: list of string """ return self.__requester.oauth_scopes def get_user(self, login=github.GithubObject.NotSet): """ :calls: `GET /users/:user <http://developer.github.com/v3/users>`_ or `GET /user <http://developer.github.com/v3/users>`_ :param login: string :rtype: :class:`github.NamedUser.NamedUser` """ assert login is github.GithubObject.NotSet or isinstance( login, (str, unicode)), login if login is github.GithubObject.NotSet: return AuthenticatedUser.AuthenticatedUser(self.__requester, {}, {"url": "/user"}, completed=False) else: headers, data = self.__requester.requestJsonAndCheck( "GET", "/users/" + login) return github.NamedUser.NamedUser(self.__requester, headers, data, completed=True) def get_users(self, since=github.GithubObject.NotSet): """ :calls: `GET /users <http://developer.github.com/v3/users>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert since is github.GithubObject.NotSet or isinstance( since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList(github.NamedUser.NamedUser, self.__requester, "/users", url_parameters) def get_organization(self, login): """ :calls: `GET /orgs/:org <http://developer.github.com/v3/orgs>`_ :param login: string :rtype: :class:`github.Organization.Organization` """ assert isinstance(login, (str, unicode)), login headers, data = self.__requester.requestJsonAndCheck( "GET", "/orgs/" + login) return github.Organization.Organization(self.__requester, headers, data, completed=True) def get_repo(self, full_name_or_id, lazy=True): """ :calls: `GET /repos/:owner/:repo <http://developer.github.com/v3/repos>`_ or `GET /repositories/:id <http://developer.github.com/v3/repos>`_ :rtype: :class:`github.Repository.Repository` """ assert isinstance(full_name_or_id, (str, unicode, int, long)), full_name_or_id url_base = "/repositories/" if isinstance( full_name_or_id, int) or isinstance(full_name_or_id, long) else "/repos/" url = "%s%s" % (url_base, full_name_or_id) if lazy: return Repository.Repository(self.__requester, {}, {"url": url}, completed=False) headers, data = self.__requester.requestJsonAndCheck( "GET", "%s%s" % (url_base, full_name_or_id)) return Repository.Repository(self.__requester, headers, data, completed=True) def get_repos(self, since=github.GithubObject.NotSet): """ :calls: `GET /repositories <http://developer.github.com/v3/repos/#list-all-public-repositories>`_ :param since: integer :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert since is github.GithubObject.NotSet or isinstance( since, (int, long)), since url_parameters = dict() if since is not github.GithubObject.NotSet: url_parameters["since"] = since return github.PaginatedList.PaginatedList(github.Repository.Repository, self.__requester, "/repositories", url_parameters) def get_gist(self, id): """ :calls: `GET /gists/:id <http://developer.github.com/v3/gists>`_ :param id: string :rtype: :class:`github.Gist.Gist` """ assert isinstance(id, (str, unicode)), id headers, data = self.__requester.requestJsonAndCheck( "GET", "/gists/" + id) return github.Gist.Gist(self.__requester, headers, data, completed=True) def get_gists(self): """ :calls: `GET /gists/public <http://developer.github.com/v3/gists>`_ :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Gist.Gist` """ return github.PaginatedList.PaginatedList(github.Gist.Gist, self.__requester, "/gists/public", None) def legacy_search_repos(self, keyword, language=github.GithubObject.NotSet): """ :calls: `GET /legacy/repos/search/:keyword <http://developer.github.com/v3/search/legacy>`_ :param keyword: string :param language: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(keyword, (str, unicode)), keyword assert language is github.GithubObject.NotSet or isinstance( language, (str, unicode)), language args = {} if language is github.GithubObject.NotSet else { "language": language } return Legacy.PaginatedList( "/legacy/repos/search/" + urllib.quote_plus(keyword, safe='/%:><'), args, self.__requester, "repositories", Legacy.convertRepo, github.Repository.Repository, ) def legacy_search_users(self, keyword): """ :calls: `GET /legacy/user/search/:keyword <http://developer.github.com/v3/search/legacy>`_ :param keyword: string :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(keyword, (str, unicode)), keyword return Legacy.PaginatedList( "/legacy/user/search/" + urllib.quote_plus(keyword, safe='/%:><'), {}, self.__requester, "users", Legacy.convertUser, github.NamedUser.NamedUser, ) def legacy_search_user_by_email(self, email): """ :calls: `GET /legacy/user/email/:email <http://developer.github.com/v3/search/legacy>`_ :param email: string :rtype: :class:`github.NamedUser.NamedUser` """ assert isinstance(email, (str, unicode)), email headers, data = self.__requester.requestJsonAndCheck( "GET", "/legacy/user/email/" + email) return github.NamedUser.NamedUser(self.__requester, headers, Legacy.convertUser(data["user"]), completed=False) def search_repositories(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/repositories <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('stars', 'forks', 'updated') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Repository.Repository` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() # pragma no branch (Should be covered) if sort is not github.GithubObject.NotSet: assert sort in ('stars', 'forks', 'updated'), sort url_parameters["sort"] = sort # pragma no branch (Should be covered) if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList(github.Repository.Repository, self.__requester, "/search/repositories", url_parameters) def search_users(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/users <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('followers', 'repositories', 'joined') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: assert sort in ('followers', 'repositories', 'joined'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList(github.NamedUser.NamedUser, self.__requester, "/search/users", url_parameters) def search_issues(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/issues <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('comments', 'created', 'updated') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Issue.Issue` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() if sort is not github.GithubObject.NotSet: assert sort in ('comments', 'created', 'updated'), sort url_parameters["sort"] = sort if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList(github.Issue.Issue, self.__requester, "/search/issues", url_parameters) def search_code(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers): """ :calls: `GET /search/code <http://developer.github.com/v3/search>`_ :param query: string :param sort: string ('indexed') :param order: string ('asc', 'desc') :param qualifiers: keyword dict query qualifiers :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.ContentFile.ContentFile` """ assert isinstance(query, (str, unicode)), query url_parameters = dict() # pragma no branch (Should be covered) if sort is not github.GithubObject.NotSet: assert sort in ('indexed', ), sort url_parameters["sort"] = sort # pragma no branch (Should be covered) if order is not github.GithubObject.NotSet: assert order in ('asc', 'desc'), order url_parameters["order"] = order query_chunks = [] if query: # pragma no branch (Should be covered) query_chunks.append(query) for qualifier, value in qualifiers.items(): query_chunks.append("%s:%s" % (qualifier, value)) url_parameters["q"] = ' '.join(query_chunks) assert url_parameters["q"], "need at least one qualifier" return github.PaginatedList.PaginatedList( github.ContentFile.ContentFile, self.__requester, "/search/code", url_parameters) def render_markdown(self, text, context=github.GithubObject.NotSet): """ :calls: `POST /markdown <http://developer.github.com/v3/markdown>`_ :param text: string :param context: :class:`github.Repository.Repository` :rtype: string """ assert isinstance(text, (str, unicode)), text assert context is github.GithubObject.NotSet or isinstance( context, github.Repository.Repository), context post_parameters = {"text": text} if context is not github.GithubObject.NotSet: post_parameters["mode"] = "gfm" post_parameters["context"] = context._identity status, headers, data = self.__requester.requestJson( "POST", "/markdown", input=post_parameters) return data def get_hook(self, name): """ :calls: `GET /hooks/:name <http://developer.github.com/v3/repos/hooks/>`_ :param name: string :rtype: :class:`github.HookDescription.HookDescription` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/hooks/" + name) return HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) def get_hooks(self): """ :calls: `GET /hooks <http://developer.github.com/v3/repos/hooks/>`_ :rtype: list of :class:`github.HookDescription.HookDescription` """ headers, data = self.__requester.requestJsonAndCheck("GET", "/hooks") return [ HookDescription.HookDescription(self.__requester, headers, attributes, completed=True) for attributes in data ] def get_gitignore_templates(self): """ :calls: `GET /gitignore/templates <http://developer.github.com/v3/gitignore>`_ :rtype: list of string """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates") return data def get_gitignore_template(self, name): """ :calls: `GET /gitignore/templates/:name <http://developer.github.com/v3/gitignore>`_ :rtype: :class:`github.GitignoreTemplate.GitignoreTemplate` """ assert isinstance(name, (str, unicode)), name headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/gitignore/templates/" + name) return GitignoreTemplate.GitignoreTemplate(self.__requester, headers, attributes, completed=True) def get_emojis(self): """ :calls: `GET /emojis <http://developer.github.com/v3/emojis/>`_ :rtype: dictionary of type => url for emoji` """ headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/emojis") return attributes def create_from_raw_data(self, klass, raw_data, headers={}): """ Creates an object from raw_data previously obtained by :attr:`github.GithubObject.GithubObject.raw_data`, and optionaly headers previously obtained by :attr:`github.GithubObject.GithubObject.raw_headers`. :param klass: the class of the object to create :param raw_data: dict :param headers: dict :rtype: instance of class ``klass`` """ return klass(self.__requester, headers, raw_data, completed=True) def dump(self, obj, file, protocol=0): """ Dumps (pickles) a PyGithub object to a file-like object. Some effort is made to not pickle sensitive informations like the Github credentials used in the :class:`Github` instance. But NO EFFORT is made to remove sensitive information from the object's attributes. :param obj: the object to pickle :param file: the file-like object to pickle to :param protocol: the `pickling protocol <http://docs.python.org/2.7/library/pickle.html#data-stream-format>`_ """ pickle.dump((obj.__class__, obj.raw_data, obj.raw_headers), file, protocol) def load(self, f): """ Loads (unpickles) a PyGithub object from a file-like object. :param f: the file-like object to unpickle from :return: the unpickled object """ return self.create_from_raw_data(*pickle.load(f)) def get_api_status(self): """ This doesn't work with a Github Enterprise installation, because it always targets https://status.github.com. :calls: `GET /api/status.json <https://status.github.com/api>`_ :rtype: :class:`github.Status.Status` """ headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/api/status.json", cnx="status") return Status.Status(self.__requester, headers, attributes, completed=True) def get_last_api_status_message(self): """ This doesn't work with a Github Enterprise installation, because it always targets https://status.github.com. :calls: `GET /api/last-message.json <https://status.github.com/api>`_ :rtype: :class:`github.StatusMessage.StatusMessage` """ headers, attributes = self.__requester.requestJsonAndCheck( "GET", "/api/last-message.json", cnx="status") return StatusMessage.StatusMessage(self.__requester, headers, attributes, completed=True) def get_api_status_messages(self): """ This doesn't work with a Github Enterprise installation, because it always targets https://status.github.com. :calls: `GET /api/messages.json <https://status.github.com/api>`_ :rtype: list of :class:`github.StatusMessage.StatusMessage` """ headers, data = self.__requester.requestJsonAndCheck( "GET", "/api/messages.json", cnx="status") return [ StatusMessage.StatusMessage(self.__requester, headers, attributes, completed=True) for attributes in data ]
def run(): from Requester import Requester for i in range(10000): Requester('http://www.hello.com', raw_request)
class Portfolio: def __init__(self): self.logger = logging.getLogger() self.logger.setLevel(logging.INFO) self.cash = None self.date = None self.holdings = {} self.r = Requester() def init(self, name): self.name = name def print(self): print("Name:\t", self.name) print("cash:\t", self.cash) print("date:\t", self.date) print("holdings:\t", self.holdings) def save(self): dm = Datamanager() dm.save_portfolio(self) def load(self, name, date): dm = Datamanager() p_data = dm.load_portfolio(name, date) if p_data: self.name = p_data['name'] self.holdings = p_data['holdings'] self.cash = p_data['cash'] self.date = p_data['date'] return True else: logging.warning("Can't load portfolio at - {} - {}".format( name, date)) return False def strategy(self): logging.warning("No extention from base class!") def update(self): self.update_holdings() logging.info("cash".format(self.cash)) self.strategy() self.save() def update_holdings(self): logging.info(self.holdings.keys()) for symbol in self.holdings.keys(): stock = self.r.get_stock_as_json(symbol, self.date) if 'error' not in stock.keys(): self.holdings[symbol] = stock def buy(self, symbol, volume=None): stock = self.r.get_stock_as_json(symbol, self.date) try: if self.cash < stock['adjusted_close']: logging.info( "Can't buy - not enough money, cash:{}, symbol:{}, value: {}" .format(self.cash, symbol, stock['adjusted_close'])) return False if symbol not in self.holdings.keys(): self.holdings[symbol] = stock self.cash -= stock['adjusted_close'] logging.info("Bought: {}, value: {}".format( symbol, stock['adjusted_close'])) return True except Exception as e: logging.error(e) def sell(self, symbol=None): if symbol in self.holdings.keys(): stock = self.holdings[symbol] logging.info("Date: {} - expected date set: {}".format( stock['date'], self.date)) if stock['date'].strftime("%Y-%m-%d") != self.date: logging.error("Can't sell at this date {} - {} - {}".format( symbol, stock['date'], self.date)) return False self.cash += stock['adjusted_close'] self.holdings.pop(symbol) logging.info("Sold {} - for {} ".format(symbol, stock['adjusted_close'])) def get_value(self): holdings_value = sum( [stock['adjusted_close'] for stock in self.holdings.values()]) return holdings_value + self.cash def get_holdings(self): return self.holdings.keys() def set_date(self, date=None): if date: self.date = date else: self.date = datetime.now().strftime("%Y-%m-%d")
def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL): self.__requester = Requester(login_or_token, password, base_url)