def get_user_list_by_department_id(department_id=1, fetch_child=1): """ 通过部门ID获取成员列表 :param department_id: 默认1为玩在一起 :param fetch_child: 默认1为获取子部门 :return: 部门成员列表 """ access_token = get_access_token_by_secret(secret_contacts) url = api_contacts % (department_id, fetch_child, access_token) try: # access token每两小时变一次,所以以URL为key的缓存最多时效也就是两小时 downloader = Downloader(cache=DiskCache(expires=timedelta( seconds=7199))) result = json.loads(downloader(url)) if result.get('errcode') != 0: logging.error('Get wx_work access token failed: ' + result.get('errmsg')) return [] else: logging.info('Get wx_work access token : ' + result.get('errmsg')) return result.get('userlist') except Exception, e: logging.error('Get wx_work access token request failed: ' + e.message) return []
def get_access_token_by_secret(secret): """ 根据secret获取access token,两小时缓存 :param secret: 应用的secret :return: 应用的access token """ if not secret: logging.error('Secret is empty.') return url = api_access_token % (crop_id, secret) try: downloader = Downloader(cache=DiskCache(expires=timedelta( seconds=7199))) result = json.loads(downloader(url)) if result.get('errcode') != 0: logging.error('Get wx_work access token failed: ' + result.get('errmsg')) return '' else: logging.info('Get wx_work access token : ' + result.get('errmsg')) return result.get('access_token') except Exception, e: logging.error('Get wx_work access token request failed: ' + e.message) return ''
def get_stats(self, game_realm, game_id, game_hash, json_decode=True): url = 'https://acs.leagueoflegends.com/v1/stats/game/%s/%s?gameHash=%s' % (game_realm, game_id, game_hash) d = Downloader(self.cache) stats = d(url) if not stats: return False if json_decode: stats = json.loads(stats) return stats
def get_schedule_items(self, cp_event_id, json_decode=True): url = 'http://api.lolesports.com/api/v1/scheduleItems?leagueId=%s' % cp_event_id d = Downloader(self.cache) league_schedule = d(url) if not league_schedule: return False if json_decode: league_schedule = json.loads(league_schedule) return league_schedule
def get_timeline(self, game_realm, game_id, game_hash, json_decode=True): url = 'https://acs.leagueoflegends.com/v1/stats/game/%s/%s/timeline?gameHash=%s' \ % (game_realm, game_id, game_hash) d = Downloader(self.cache) timelime = d(url) if not timelime: return False if json_decode: timelime = json.loads(timelime) return timelime
def get_match_detail(self, tournament_id, match_id, json_decode=True): url = 'http://api.lolesports.com/api/v2/highlanderMatchDetails?tournamentId=%s&matchId=%s' \ % (tournament_id, match_id) logging.info('match detail: %s' % url) d = Downloader(cache=self.cache) match_detail = d(url) if not match_detail: return False if json_decode: match_detail = json.loads(match_detail) return match_detail
def download_img(img_url, save_file): download = Downloader(delay=5, num_retries=2) img_data = download(img_url) # 保持原始图片 folder = os.path.dirname(save_file) if not os.path.exists(folder): os.makedirs(folder) img_file = open(save_file, "wb") img_file.write(img_data) img_file.flush() img_file.close()
def request(url): d = Downloader() response = d(url) if not response: return False return json.loads(response)