def get_user(self, user_id): try: hc = HttpClient(ret_type="json") url = "%s?user=%s&key=%s" % (settings.OPS_USER_INFO_URL, user_id, settings.OPS_KEY) ret = hc.get(url) if ret["statusCode"] == 200 and ret["data"] and ret["data"][ "status"] == "00": ret = ret["data"]["data"] user = User(backend=self, username=ret["loginname"]) user.last_name = ret["usernme"] user.email = "*****@*****.**" % ret["loginname"] user.is_active = True if ret["privilege"] == 1: user.is_super = True user.role = "root" user.web_role = "SU" else: user.is_super = False user.role = "www" user.web_role = "U" user.mobile = ret["mobile"] user.group = ret["usergroup"] user._is_authenticated = True return user return None except User.DoesNotExist: return None
def is_super(self): try: if not self._is_superuser: infourl = settings.OPS_USER_INFO_URL data = "user=%s&key=%s" % (self.username, settings.OPS_KEY) url = "%s?%s" % (infourl, data) hc = HttpClient(ret_type="json") ret = hc.get(url) if ret and ret["statusCode"] == 200 and ret["data"][ "status"] == "00" and ret["data"]["data"][ "privilege"] == 1: self._is_superuser = True return self._is_superuser except Exception, e: logger.exception(e) self._is_superuser = False return self._is_superuser
class CodeBaseAPI: """Class to access Codebase API This warper for to access the CodebaseHQ API Attributes: __auth: Dict to hold codebaseHQ authentication credentials __config: Dict to hold codebase configuration data __endpoints: Dict to hold the codebase api endpoint/path data __http: Contains instance of the HttpClient class """ def __init__(self, account_name, username, api_key): """ Constructor for CodeBaseAPI class. * This class requires account_name, username, api_key parameters to be passed in the constructor. * To get these credentials, click the Settings icon in the top-right of your Codebase HQ page (the one that looks like a wrench) and then click 'My Profile'. Go to the 'API Credentials' section at the bottom of the page Args: account_name: Account name of the CodebaseHQ user username: Username of the CodebaseHQ user api_key: API Key of the CodebaseHQ user Raises: CredentialError: when any on of the input params are empty Exception: when there is an un-expected error """ if account_name and username and api_key: self.__auth = self.__get_auth_data(account_name, username, api_key) else: raise CredentialError() self.__config = self.__get_config() self.__endpoints = self.__config['endpoints'] self.__http = HttpClient( base_url=self.__config['base_url'], auth=self.__auth ) def __get_config(self): """ Gets the configuration data If there any changes in the CodebaseHQ API URL or endpoints, then these change the value in this dict Args: None Returns: dict containing the CodebaseHQ configuration data Raises: None """ return { 'base_url': 'https://api3.codebasehq.com', 'endpoints': { 'all_projects': '/projects', 'project': '/project', 'tickets': '/tickets' } } def get_all_projects(self): """Gets all the projects details * Makes an HTTP GET request to the /projects endpoint to get all project data. * API URL: https://api3.codebasehq.com/projects * [Documentation Link](https://support.codebasehq.com/kb/projects) Sample Output: ``` <projects type="array"> <project> <group-id type="integer" nil="true"></group-id> <icon type="integer">1</icon> <name>Codebase</name> <account-name>aTech Media</account-name> <overview></overview> <permalink>codebase</permalink> <start-page>overview</start-page> <status>active</status> <total-tickets>100</total-tickets> <open-tickets>36</open-tickets> <closed-tickets>64</closed-tickets> </project> <project> ... </project> </projects> ``` """ response = self.__http.get( endpoint=self.__endpoints['all_projects'], headers=self.__get_common_headers(), ) return response def __get_common_headers(self): """Gets common header to be sent with the HTTP request object Args: None Returns: Dict containing custom header data Raises: None """ return { 'Content-type': "application/xml" } def __get_auth_data(self, account_name, username, api_key): """ Gets data for HTTP Basic Authentication Args: account_name: Account name of the CodebaseHQ user username: Username of the CodebaseHQ user api_key: API Key of the CodebaseHQ user Returns: dict containing username and password Raises: Exception: when there is an un-expected error """ return { 'username': account_name + '/' + username, 'password': api_key }
class TwitterSpider(): def __init__(self): self.httpClient = HttpClient() def _getUserHomeSiteUrl(self, screen_name): return "https://twitter.com/" + screen_name def _getFollowersUsersParams(self, cursor): return { "max_position": cursor, "include_available_features": 1, "include_entities": 1 } def _getFollowersUsersUrl(self, screen_name): return 'https://twitter.com/' + screen_name + '/followers/users' def _getFollowingUsersParams(self, cursor): return { "max_position": cursor, "include_available_features": 1, "include_entities": 1 } def _getFollowingUsersUrl(self, screen_name): return 'https://twitter.com/' + screen_name + '/following/users' def _getTimelineParams(self, max_id=0): # params = {"contextual_tweet_id": 261095810454917120,"include_available_features": 1,"include_entities": 1,"last_note_ts": 110,"max_id": 0} # if max_id!=0: # params['max_id']=max_id # return params params = { "contextual_tweet_id": 261095810454917120, "include_available_features": 1, "include_entities": 1, "last_note_ts": 110, "max_position": 0 } if max_id != 0: params['max_position'] = max_id return params def _getTimelineUrl(self, screen_name): return "https://twitter.com/i/profiles/show/" + screen_name + "/timeline" def _getUserFromSearchUrl(self, name): return "https://twitter.com/search?q=" + name + "&mode=users" def _getUserFromSearchParam(self, name): return {} def _parseNumStr2Int(self, numStr): if not numStr: return 0 numStr = numStr.replace(',', '') if numStr.find('K') > -1: numStr = numStr.replace('K', '') return int(float(numStr) * 1000) elif numStr.find('M') > -1: numStr = numStr.replace('M', '') return int(float(numStr) * 1000000) elif numStr.find('千'.decode("utf8")) > -1: numStr = numStr.replace('千'.decode("utf8"), '') return int(float(numStr) * 1000) elif numStr.find('万'.decode("utf8")) > -1: numStr = numStr.replace('万'.decode("utf8"), '') return int(float(numStr) * 10000) else: return int(numStr) def getFollowersUsers(self, screen_name, cursor=-1): cursor = cursor while True: print 'getFollowersUsers cursor : ', str(cursor) try: res = self.httpClient.get( self._getFollowersUsersUrl(screen_name), self._getFollowersUsersParams(cursor)) json = res.json() html = json['items_html'] except ValueError, e: print sys.stderr.write( 'getFollowers ValueError ,break... cursor ' + str(cursor) + ' screen_name : ' + screen_name + " " + str(e)) break except Exception, e: print sys.stderr.write('getFollowers Error ,retry... cursor ' + str(cursor) + ' screen_name : ' + screen_name + " " + str(e)) time.sleep(300) continue for user in followParser(html): yield user if json['has_more_items'] == False or json['min_position'] == '0': break else: cursor = json['min_position']