def _api(self, method, uri, attrs={}): headers = self.API_HEADERS.copy() if self.apikey: headers['apikey'] = self.apikey if self.subkey: headers['subkey'] = self.subkey return API.request(method, self.API_URL + uri, attrs, headers)
class User(object): """ Class to access Bungie.net User API """ def __init__(self): self.public_api = API() def get_membership_data_for_current_user(self, uid): req = API.bungie_api + '/User/GetMembershipsForCurrentUser/' return self.public_api.call_bungie_private_api_automated(uid, req)
class User(object): """ Class to access Bungie.net User API """ def __init__(self): self.public_api = API() def get_bungie_net_user_by_id(self, uid: int): req = API.bungie_api + '/User/GetBungieNetUserById/' + str(uid) + '/' return self.public_api.call_bungie_public_api(req) def search_users(self, display_name: str): req = API.bungie_api + '/User/SearchUsers/?q=' + display_name return self.public_api.call_bungie_public_api(req) def get_membership_data_by_id(self, uid: int, membership_type: int = 254): req = API.bungie_api + '/User/GetMembershipsById/' + str( uid) + '/' + str(membership_type) + '/' return self.public_api.call_bungie_public_api(req) def get_partnerships(self, uid: int): req = API.bungie_api + '/User/' + str(uid) + '/Partnerships/' return self.public_api.call_bungie_public_api(req)
class GroupsV2(object): """ Class to access Bungie.net User API """ def __init__(self): self.public_api = API() def get_group(self, group_id: int) -> str: req = API.bungie_api + '/GroupV2/' + str(group_id) + '/' return self.public_api.call_bungie_public_api(req) def get_group_by_name(self, group_name: str, group_type: int) -> str: """GroupType: 0=General, 1=Clan""" req = API.bungie_api + '/GroupV2/Name/' + group_name + '/' + str( group_type) + '/' return self.public_api.call_bungie_public_api(req) def get_members_of_group(self, group_id: int, current_page: int = 1) -> str: """GroupType: 0=General, 1=Clan""" req = API.bungie_api + '/GroupV2/' + str( group_id) + '/Members/?currentPage=' + str(current_page) return self.public_api.call_bungie_public_api(req) def get_andmins_and_founder_of_group(self, group_id: int) -> str: """GroupType: 0=General, 1=Clan""" req = API.bungie_api + '/GroupV2/' + str( group_id) + '/AdminsAndFounder/' return self.public_api.call_bungie_public_api(req) def get_banned_members_of_group(self, group_id: int) -> str: req = API.bungie_api + '/GroupV2/' + str(group_id) + '/Banned/' return self.public_api.call_bungie_public_api(req) def get_groups_for_member(self, membership_type: int, membership_id: int, search_filter: int, group_type: int) -> str: """filter: 0 all, 1 founded, 2 non-founded""" req = API.bungie_api + '/GroupV2/User/' + str( membership_type) + '/' + str(membership_id) + '/' + str( search_filter) + '/' + str(group_type) + '/' return self.public_api.call_bungie_public_api(req) def get_potential_groups_for_member(self, membership_type: int, membership_id: int, search_filter: int, group_type: int) -> str: req = API.bungie_api + '/GroupV2/User/Potential/' + str( membership_type) + '/' + str(membership_id) + '/' + str( search_filter) + '/' + str(group_type) + '/' return self.public_api.call_bungie_public_api(req)
def __init__(self): self.public_api = API()
def main(): global queue, sep, proc_put_file config = configparser.ConfigParser() config.read(CONFIG_FILE) if (not config) or (not config.sections()): print('No se encontro el archivo de configuracion.') exit(0) ftp_images_folder = config['folders'].get('ftp_images', '') car_images_folder = config['folders'].get('car_images', '.') plates_folder = config['folders'].get('plates', '.') sep = config['folders'].get('folder_sep', '.') car_images_folder = car_images_folder + sep + 'cars_'+dt.now().strftime('%Y-%m-%d') if not os.path.exists(car_images_folder): os.mkdir(car_images_folder) plates_folder = plates_folder + sep + 'plates_' + dt.now().strftime('%Y-%m-%d') if not os.path.exists(plates_folder): os.mkdir(plates_folder) ftp = FTP(url=config['ftp'].get('server_url'), port=int(config['ftp'].get('server_port', '9999')), folder=config['ftp'].get('server_folder', '.'), user=config['ftp'].get('user', 'anonymous'), password=config['ftp'].get('password', '') ) if ftp.connect(): exit(0) if ftp.login(): exit(0) # ftp.change_folder() api = API(api_url=config['api'].get('API_URL', ''), token=config['api'].get('API_TOKEN', ''), ) car_detector = CarDetector(model=os.curdir + sep + config['car_detect'].get('model'), threshold=int(config['car_detect'].get('threshold', '50')), car_percent=float(config['car_detect'].get('car_percent', '4.5')) ) proc_put_file = Process(target=put_file_in_queue, args=(ftp,) ) proc_put_file.start() print('Ready and working..\n') json_result = config['folders'].get('results', '.') + sep + f'plates_result_{dt.now().strftime("%Y-%m-%d")}.json' with open(json_result, 'a') as fp: try: fp.write('[\n') while True: check_folder_day(car_images_folder, 'cars_') check_folder_day(plates_folder, 'plates_') images = get_file_in_queue() if not images: time.sleep(1) continue images_list = [] for image, image_name in images: img = np.frombuffer(image, dtype=np.uint8) img = cv2.imdecode(img, 1) if ftp_images_folder: cv2.imwrite(ftp_images_folder + sep + image_name, img) images_list.append(img) # car_images, dict_unique_cars = car_detector.detect(images_list) dict_unique_cars = car_detector.detect(images_list) for key in dict_unique_cars.keys(): folder = car_images_folder + sep + key + dt.now().strftime('%Y-%m-%d_%H:%M:%S') os.mkdir(folder) for car in dict_unique_cars[key]: car_image_path = folder + sep + 'car_' + str(len(os.listdir(folder))) + '.jpg' if not cv2.imwrite(car_image_path, car): print('No se pudo guardar la imagen en {}'.format(car_image_path)) continue response = api.request(car_image_path) result = response['results'] if 'results' in response else [] if len(result) == 0: print('No results for plate') continue plate, plate_box, right_format = api.improve_plate(result) img_plate = cut_and_save(car_image_path, plate_box, plate, plates_folder) print('Plate detected: {}'.format(plate)) fp.write(str(dict(plate=plate, image_path=car_image_path))) # b64encode(img_plate.tobytes()) fp.flush() if not right_format: continue break except KeyboardInterrupt: fp.write(']\n') raise KeyboardInterrupt
class Destiny2(object): """ Class to access Bungie.net User API All GET requests done """ def __init__(self): self.public_api = API() def get_profile(self, uid: int, membership_type: int, membership_id: int, raw_components: list): components = ','.join(str(e) for e in raw_components) req = API.bungie_api + "/Destiny2/" + str(membership_type) + "/Profile/" + str(membership_id) \ + "/?components=" + str(components) return self.public_api.call_bungie_private_api_automated(uid, req) def get_character(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Profile/" + str( membership_id) + "/Character/" + str(character_id) + "/" return self.public_api.call_bungie_public_api(req) def get_clan_weekly_reward_state(self, clan_id): req = API.bungie_api + "/Destiny2/Clan/" + str( clan_id) + "/WeeklyRewardState/" return self.public_api.call_bungie_public_api(req) def get_item(self, membership_type, membership_id, item_instance_hash): req = API.bungie_api + "/Destiny2/" + str(membership_type) + "/Profile/" + str(membership_id) + "/Item/" + \ str(item_instance_hash) + "/" return self.public_api.call_bungie_public_api(req) def get_vendors(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Profile/" + str( membership_id) + "/Character/" + str( character_id) + "/Vendors/" return self.public_api.call_bungie_public_api(req) def get_vendor(self, membership_type, membership_id, character_id, vendor_hash): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Profile/" + str( membership_id) + "/Character/" + str( character_id) + "/Vendors/" + str(vendor_hash) + "/" return self.public_api.call_bungie_public_api(req) def get_post_game_carnage_report(self, activity_id): req = API.bungie_api + "/Destiny2/Stats/PostGameCarnageReport/" + str( activity_id) + "/" return self.public_api.call_bungie_public_api(req) def get_clan_leaderboards(self, clan_id): req = API.bungie_api + "/Destiny2/Stats/Leaderboards/Clans/" + str( clan_id) + "/" return self.public_api.call_bungie_public_api(req) def get_clan_aggregate_stats(self, clan_id): req = API.bungie_api + "/Destiny2/Stats/AggregateClanStats/" + str( clan_id) + "/" return self.public_api.call_bungie_public_api(req) def get_leaderboards(self, membership_type, membership_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Account/" + str( membership_id) + "/Stats/Leaderboards/" return self.public_api.call_bungie_public_api(req) def get_leaderboards_for_character(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/Stats/Leaderboards/" + str(membership_type) + "/" + str(membership_id) + "/" + \ str(character_id) + "/" return self.public_api.call_bungie_public_api(req) def get_historical_stats(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Profile/" + str( membership_id) + "/Character/" + str(character_id) + "/Stats/" return self.public_api.call_bungie_public_api(req) def get_historical_stats_for_account(self, membership_type, membership_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Account/" + str(membership_id) + "/Stats/" return self.public_api.call_bungie_public_api(req) def get_activity_history(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Account/" + str( membership_id) + "/Character/" + str( character_id) + "/Stats/Activities/" return self.public_api.call_bungie_public_api(req) def get_unique_weapon_history(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Account/" + str( membership_id) + "/Character/" + str( character_id) + "/Stats/UniqueWeapons/" return self.public_api.call_bungie_public_api(req) def get_destiny_aggregate_activity_stats(self, membership_type, membership_id, character_id): req = API.bungie_api + "/Destiny2/" + str( membership_type) + "/Account/" + str( membership_id) + "/Character/" + str( character_id) + "/Stats/AggregateActivityStats/" return self.public_api.call_bungie_public_api(req) def get_public_milestones_content(self, milestone_hash): req = API.bungie_api + "/Destiny2/Milestones/" + str( milestone_hash) + "/Content/" return self.public_api.call_bungie_public_api(req) def get_public_milestones(self, ): req = API.bungie_api + "/Destiny2/Milestones/" return self.public_api.call_bungie_public_api(req) # Milestone Helper @staticmethod def milestone_types(this_type: int) -> str: """ Returns the correct English Language Milestone Type :param this_type: The milestone type from the milestone definition :type this_type: int :return: English Language Milestone Type :rtype: str """ if this_type == 1: return 'Tutorial' elif this_type == 2: return 'OneTime' elif this_type == 3: return 'Weekly' elif this_type == 4: return 'Daily' elif this_type == 5: return 'Special' else: return 'UNKNOWN'
def upload(request): if request.method == 'POST': # 上传到本地目录 try: path = request.POST.get('path') proxy = request.POST.get('proxy') proxy_host = request.POST.getlist('proxy_host') # 上传到本地 f = request.FILES['file'] df = handle_uploaded_file(f) files = {'file': (f.name, open(df, 'rb'))} params = {'action': 'upload'} # 通过proxy处理文件 proxy_obj = Proxy.objects.get(id=proxy) tnow = datetime.datetime.now() # 调用proxy接口,上传文件 api = API('{0}/v1.0/upload'.format(proxy_obj.url), proxy_obj.username, CRYPTOR.decrypt(proxy_obj.password)) result, code = api.req_post(data=params, files=files) if code != 200: file = File(path=path, proxy=proxy_obj, create_time=tnow, status='01', result="上传文件失败") file.save() raise ServerError(result['messege']) # 上传文件成功之后,调用proxy接口,进行文件上传任务 hosts = [] if not proxy_host: hosts = Asset.objects.all().filter(proxy=proxy_obj) if not hosts: # 没有可执行主机 file = File(path=path, proxy=proxy_obj, create_time=tnow, status='01', result="没有可执行主机") file.save() raise RuntimeError("没有可执行主机") else: for host_id in proxy_host: hosts.append(Asset.objects.get(id=host_id)) host_list = [] resource = [] params = {} trigger_kwargs = {} trigger_kwargs['year'] = tnow.year trigger_kwargs['month'] = tnow.month trigger_kwargs['day'] = tnow.day trigger_kwargs['hour'] = tnow.hour trigger_kwargs['minute'] = tnow.minute+1 trigger_kwargs['second'] = tnow.second params['trigger_kwargs'] = trigger_kwargs params['task_name'] = 'ansible' task_kwargs = {} task_kwargs['module_name'] = 'copy' task_kwargs['module_args'] = 'src={0} dest={1}'.format(result.get('fp'), path) # 构建inventory 和 构建主机list for host in hosts: host_list.append(host.networking.all()[0].ip_address) tmp_d = dict() tmp_d['hostname'] = host.networking.all()[0].ip_address tmp_d['port'] = host.port tmp_d['username'] = host.username tmp_d['password'] = CRYPTOR.decrypt(host.password) # 用于前端确定选择的asset tmp_d['id'] = host.id resource.append(tmp_d) task_kwargs['host_list'] = host_list task_kwargs['resource'] = resource params['task_kwargs'] = task_kwargs # 调用proxy接口,创建任务 api = APIRequest('{0}/v1.0/job'.format(proxy_obj.url), proxy_obj.username, CRYPTOR.decrypt(proxy_obj.password)) result, code = api.req_post(json.dumps(params)) if code != 200: file = File(path=path, proxy=proxy_obj, create_time=tnow, status='01', result="上传文件失败") file.save() else: file = File(path=path, proxy=proxy_obj, task_uuid=result['job']['job_id'], create_time=tnow) file.save() except Exception, e: logger.error(traceback.format_exc()) return HttpResponseRedirect(reverse('file_upload'))