def create_image(self): if web.ctx.method == "POST": link_url = web.input().get('link', '').strip() album_id = int(web.input().get('album_id')) file = web.input().get("file") try: f = HttpUploadedFile(file) self.record_image(f=file, uuid=f.uuid(), link=link_url, call_type='create', album_id=album_id) self.private_data["create_success"] = True self.private_data["create_message"] = u"链接创建成功" return web.seeother(self.make_url('images_list')) except Exception as e: log.error('create album failed%s' % traceback.format_exc()) self.private_data["create_success"] = False self.private_data["create_message"] = u"创建失败" albums = Albums.select().where(Albums.status == 0). \ order_by(Albums.id.asc()).execute() self.private_data["albums_list"] = albums return self.display("admin/create_image") if web.ctx.method == "GET": albums = Albums.select().where(Albums.status == 0). \ order_by(Albums.id.asc()).execute() self.private_data["albums_list"] = albums return self.display("admin/create_image")
def record_image(self, f, uuid, link, album_id, call_type="create", image_id=None): im = Imaging(f) origin = im.thumbnail() try: if call_type == "create": Images().create(uuid=uuid, link=link, album=album_id, thumbnail=origin) else: Images.update(link=link, album_id=album_id, thumbnail=origin, uuid=uuid, updateTime=time()). \ where(Images.id == int(image_id)).execute() if not os.path.exists(config.UPLOAD_DIR): os.makedirs(config.UPLOAD_DIR) file_name = str(uuid) + ".jpeg" tmp_file = os.path.join(config.UPLOAD_DIR, file_name) with open(tmp_file, 'wb') as fs: fs.write(f) except Exception as e: print(traceback.format_exc()) log.error(traceback.format_exc()) return uuid
def delete_cert(): log.warning('Deleting mitmproxy certificate...') if error_code := subprocess.call('certutil -delstore -user Root mitmproxy', shell=True): log.error( f'Certificate could not be deleted: {hex(error_code)} - {str(FormatMessage(error_code)).strip()}' )
def create_album(self): inputs = self.get_input() if web.ctx.method == "POST": name = inputs.get('name', '').strip() is_show = inputs.get('is_show', 0) album = Albums.get_or_none(Albums.name == name) if album: self.private_data["create_success"] = False self.private_data["create_message"] = u"相册已存在" return self.display("admin/create_album") try: if int(is_show) == 1: albums = Albums.select().where( Albums.status == 0, Albums.is_show == 1).execute() for album in albums: album.is_show = 0 album.save() album = Albums(name=name, is_show=is_show) album.save() self.private_data["create_success"] = True self.private_data["create_message"] = u"相册创建成功" return web.seeother(self.make_url('albums_list')) except Exception as e: log.error('create album failed%s' % traceback.format_exc()) self.private_data["create_success"] = False self.private_data["create_message"] = u"创建相册失败" return self.display("admin/create_album") if web.ctx.method == "GET": return self.display("admin/create_album")
def images(self): inputs = self.get_input() url = API_URL + '/api/images' try: return get(url, inputs) except Exception as e: log.error('execus images %s' % traceback.format_exc())
def fetch_entitlements_if_necessary(self): log.debug('Fetching origin entitlements') # Get the etag etag_path = get_data_path('origin-entitlements.etag') etag = '' if isfile(self.entitlements_path) and isfile(etag_path): with open(etag_path, mode='r') as file: etag = file.read() # Fetch entitlements if etag does not match url = 'https://raw.githubusercontent.com/acidicoala/origin-entitlements/master/entitlements.json' response = requests.get(url, headers={'If-None-Match': etag}) if response.status_code == 304: log.debug(f'Cached Origin entitlements have not changed') return if response.status_code != 200: log.error( f'Error while fetching entitlements: {response.status_code} - {response.text}' ) return # Cache entitlements with open(self.entitlements_path, 'w') as f: f.write(response.text) # Cache etag with open(etag_path, 'w') as f: f.write(response.headers['etag']) log.info('Origin entitlements were successfully fetched and cached')
def signin(self): try: inputs = self.get_input() log.info('signin:' + str(inputs)) cellphone = inputs['cellphone'] password = md5(inputs['password']).hexdigest() user = Users.get(Users.cellphone == cellphone) if not user or user.password != password: return self.unauthorized() t = int(time.time()) if not user.token or t - time.mktime( user.token_created_time.timetuple()) > 144000: token = gen_token() user.token = token user.token_created_time = datetime.datetime.now() else: token = user.token self.set_login(user.cellphone, token) user.last_login_time = datetime.datetime.now() user.save() return self.success() except Exception as e: log.error('execs signin %s' % traceback.format_exc()) return self.unauthorized()
def update_category(self): inputs = self.get_input() if web.ctx.method == "GET": category_id = inputs.get("category_id") category = Categories.get_or_none(Categories.id == category_id) category_list = Categories.select().where(Categories.status == 0) self.private_data["category"] = category self.private_data["category_list"] = category_list return self.display("admin/update_category") else: category_id = inputs.get('category_id') name = inputs.get('name') desc = inputs.get('desc') parent_id = inputs.get("parent_id") category = Categories.get_or_none(Categories.id == category_id) try: category.update(name=name, description=desc, parent_id=parent_id). \ where(Categories.id == category_id).execute() self.private_data["create_success"] = True return web.seeother(self.make_url('category_list')) except Exception as e: log.error('update category failed %s' % traceback.format_exc()) log.error('input params %s' % inputs) self.private_data["update_success"] = False return self.display("admin/update_category")
def request_sms(self): inputs = self.get_input() url = API_URL + '/api/request_sms' try: return post(url, inputs) except Exception as e: log.error('execus request_sms %s' % traceback.format_exc())
def search_list(self): """ 文章搜索列表 :return: """ inputs = self.get_input() page = int(inputs.get('page', 1)) page_size = int(inputs.get('page_size', 20)) keywords = inputs.get('keywords', None) self.private_data['article_list'] = [] self.private_data['current_page'] = 1 self.private_data['total_page'] = 0 self.private_data['category_list'] = [] self.private_data['keywords'] = keywords try: category_list = Categories.select().where(Categories.status == 0). \ order_by(Categories.id.desc()) if keywords: article_query = Articles.select().where( Articles.name.contains(keywords)) total_count = article_query.count() total_page = (total_count + page_size - 1) / page_size self.private_data['total_page'] = total_page self.private_data['current_page'] = page self.private_data['category_list'] = category_list self.private_data['article_list'] = article_query.\ paginate(page, page_size) return self.display('front/search_list') except Exception as e: log.info('Failed to get search result. Keywords is %s. Error msg is', keywords, e) log.error(traceback.format_exc()) return self.display('front/search_list')
def _insert_update_into_wms(self): """ Inserts or Update the coverage into the wms service """ try: # First check if layer exists or not request = WMSTDescribeLayer(self.coverage.coverage_id) layer_exist = True try: response = ConfigManager.executor.execute(request) except Exception as ex: if not "LayerNotFound" in str(ex): raise ex else: # Layer not found layer_exist = False # WMS layer does not exist, just insert new WMS layer from imported coverage if layer_exist is False: request = WMSTFromWCSInsertRequest(self.coverage.coverage_id, False, ConfigManager.black_listed) else: # WMS layer existed, update WMS layer from updated coverage request = WMSTFromWCSUpdateRequest(self.coverage.coverage_id, False) ConfigManager.executor.execute(request, mock=ConfigManager.mock) except Exception as e: log.error( "Exception thrown when importing in WMS. Please try to reimport in WMS manually.") raise e
def edit_user_info(self): inputs = self.get_input() try: user = Users.get(Users.cellphone == self.is_login()) if inputs.get('name'): user.name = inputs['name'] if inputs.get('description'): user.description = self.htmlunquote(inputs['description']) if inputs.get('gender'): user.gender = int(inputs['gender']) if inputs.get('birthday'): user.birthday = inputs['birthday'] if inputs.get('email'): user.email = inputs['email'] user.save() return self.success() except Users.DoesNotExist: log.error('execus edit_user_info %s' % traceback.format_exc()) return self.forbidden() except Exception as e: log.error('execus edit_user_info %s' % traceback.format_exc()) return self.error()
def add_keywords_to_db(task_list): blacklist = load_keyword_blacklist_from_file() for task in task_list: base_path = task.base_path file_path = task.file_path video_id = task.video_id keywords = get_keywords(base_path, file_path, blacklist) log.info('#Keywords:'.format(keywords)) for key in keywords: try: if KeywordVideoId.objects.filter(keyword=key, video_id=video_id): log.info("Existing keyword {0} for {1}".format( key, video_id)) continue keyword_record = KeywordVideoId() keyword_record.keyword = key keyword_record.video = Video.objects.get(video_id=video_id) keyword_record.save() log.info('#Added keyword:{0} for video_id: {1}'.format( key, video_id)) except Exception as e: log.error( "Error while adding keyword {0} to video {1}: {2}".format( key, video_id, e))
def _insert_update_into_wms(self): """ Inserts or Update the coverage into the wms service """ try: # First check from WMS GetCapabilities if layer name (coverage id) existed request = WMSTGetCapabilities() response = ConfigManager.executor.execute(request) root_element = etree.fromstring(response) namespace = {"wms": "http://www.opengis.net/wms"} exist = root_element.xpath( "//wms:Capability/wms:Layer/wms:Layer/wms:Name/text()='" + self.coverage.coverage_id + "'", namespaces=namespace) # WMS layer does not exist, just insert new WMS layer from imported coverage if exist is False: request = WMSTFromWCSInsertRequest(self.coverage.coverage_id, False) else: # WMS layer existed, update WMS layer from updated coverage request = WMSTFromWCSUpdateRequest(self.coverage.coverage_id, False) ConfigManager.executor.execute(request, mock=ConfigManager.mock) except Exception as e: log.error( "Exception thrown when importing in WMS. Please try to reimport in WMS manually." ) raise e
def search(self): inputs = self.get_input() url = API_URL + '/api/search' try: return get(url, inputs) except Exception as e: log.error('execus search %s' % traceback.format_exc())
def post(url, data): try: req = urllib2.Request(url=url, data=urlencode(data)) result = urllib2.urlopen(req).read() return json.loads(result) except Exception as e: log.error('execu post %s' % traceback.format_exc())
def get(url, data): try: params = urlencode(data) result = urlopen("%s?%s" % (url, params)).read() return result except Exception as e: log.error('execu get %s' % traceback.format_exc())
def articles(self): inputs = self.get_input() url = API_URL + '/api/articles' try: log.info('len articles %s' % len(get(url, inputs))) return get(url, inputs) except Exception as e: log.error('execus articles %s' % traceback.format_exc())
def patch_origin_client(self): origin = Client('Origin', 'Origin.exe', 'libeay32.dll', 'EVP_DigestVerifyFinal') eadesktop = Client('EA Desktop', 'EADesktop.exe', 'libcrypto-1_1-x64.dll', 'EVP_DigestVerifyFinal') client = origin try: client_process = Pymem(client.PROCESS_NAME) except ProcessNotFound: client = eadesktop try: client_process = Pymem(client.PROCESS_NAME) except ProcessNotFound: log.warning('Origin/EA Desktop process not found. Patching aborted') return if client_process.process_id == self.last_client_pid: log.debug(f'{client.NAME} client is already patched') return log.info(f'Patching {client.NAME} client') try: dll_module = next(m for m in client_process.list_modules() if m.name.lower() == client.DLL_NAME) except StopIteration: log.error(f'{client.DLL_NAME} is not loaded. Patching aborted') return # The rest should complete without issues in most cases. # Get the Export Address Table symbols # noinspection PyUnresolvedReferences dll_symbols = PE(dll_module.filename).DIRECTORY_ENTRY_EXPORT.symbols # Get the symbol of the EVP_DigestVerifyFinal function verify_func_symbol = next(s for s in dll_symbols if s.name.decode('ascii') == client.FUNCTION_NAME) # Calculate the final address in memory verify_func_addr = dll_module.lpBaseOfDll + verify_func_symbol.address # Instructions to patch. We return 1 to force successful response validation. patch_instructions = bytes([ 0x66, 0xB8, 0x01, 0, # mov ax, 0x1 0xC3 # ret ]) client_process.write_bytes(verify_func_addr, patch_instructions, len(patch_instructions)) # Validate the written memory read_instructions = client_process.read_bytes(verify_func_addr, len(patch_instructions)) if read_instructions != patch_instructions: log.error('Failed to patch the instruction memory') return # At this point we know that patching was successful self.last_client_pid = client_process.process_id log.info(f'Patching {client.NAME} was successful')
def comment_article(self): inputs = self.get_input() token = Users.get(Users.cellphone == self.is_login()).token inputs['token'] = token url = API_URL + '/api/comment_article' try: return post(url, inputs) except Exception as e: log.error('execus comment_article %s' % traceback.format_exc())
def load_schema(): script_dir = os.path.dirname(os.path.abspath( __file__ )) try: with open(script_dir + "/ingredients/ingredients_schema.json", "r") as ingredients_schema_fd: ingredients_schema = json.load(ingredients_schema_fd) return ingredients_schema except IOError as e: log.error("We could not open the ingredients schema file. Make sure the file exists and is readable.\n" + str(e)) exit_error()
def article_info(self): inputs = self.get_input() url = API_URL + '/api/article_info' try: if self.is_login(): inputs['token'] = Users.get( Users.cellphone == self.is_login()).token return get(url, inputs) except Exception as e: log.error('execus articles %s' % traceback.format_exc())
def update_user_info(self): inputs = self.get_input() token = Users.get(Users.cellphone == self.is_login()).token inputs['token'] = token log.info('inputs %s ' % inputs) url = API_URL + '/api/update_user_info' try: return post(url, inputs) except Exception as e: log.error('execus update_user_info %s' % traceback.format_exc())
def __image_uuid(self, uuid): try: src = None image = Images.get(Images.uuid == uuid) src = ALI_CDNIMAGES_URL + "/%s" % ALI_OSS_DIR + '/%s.jpeg' % uuid log.info('src' + str(src)) return src except Exception as e: log.error('__image_uuid %s' % traceback.format_exc())
def validate(): """ Validates the commandline arguments (i.e. makes sure it's only one) """ if len(sys.argv) == 1: print_usage() exit(1) if len(sys.argv) != 2: log.error("WCST Import expects exactly one argument. See the manual entry for the script below:") print_usage() exit_error()
def __file_write(self, urls, content): try: log.info('into __file_write urls: %s,content: %s' % (urls, content)) file_object = file(urls, 'w') # file_object = open(urls,'w') file_object.truncate() file_object.write(content) file_object.close() except Exception as e: log.error(traceback.format_exc())
def _insert_into_wms(self): """ Inserts the coverage into the wms service """ try: request = WMSTFromWCSInsertRequest(self.coverage.coverage_id, False) ConfigManager.executor.execute(request) except Exception as e: log.error( "Exception thrown when importing in WMS. Please try to reimport in WMS manually.") raise e
def read_ingredients(): """ Reads the ingredient file and returns a string of json :rtype: str """ path = "" try: path = sys.argv[1] return file(path).read() except IOError: log.error("We could not read the ingredients file at " + path + ". Make sure the file exists and is readable.") exit_error()
def resume(self): """标签下文章列表:return:""" self.private_data['category_list'] = [] try: category_list = Categories.select().where(Categories.status == 0).\ order_by(Categories.id.desc()) self.private_data['category_list'] = category_list return self.display('front/resume') except Exception as e: log.info('Failed to get search result.tag is%s.Error msg is', e) log.error(traceback.format_exc()) return self.display('front/resume')
def string_to_list(v): """ Try to parse v into a list of strings. """ astval = ast.literal_eval(v) if type(astval) is list: return [str(s) for s in astval] elif isinstance(astval, basestring): return [astval] else: log.error("Failed converting %s to a list of strings.", v) return []
def decode_ingredients(ingredients_raw): """ Decodes the raw json ingredients string into a python dict :param str ingredients_raw: the raw json string :rtype: dict[str,dict|str|bool|int|float] """ try: return json.loads(ingredients_raw) except Exception as ex: log.error("We could not decode the ingredients file. This is usually due to " \ "a problem with the json format. Please check that you have a valid json file." \ " The JSON decoder error was: " + str(ex)) exit_error()
def load_keyword_blacklist_from_file(): blacklist = set() keyword_file = 'keywords.blacklist' try: with open(keyword_file, 'r') as kfp: for line in kfp: line = line.strip('\n') if line: blacklist.add(line) log.info("Keywords blacklist: {0}".format(blacklist)) except Exception as e: log.error("Error while processing {0}:{1}".format(keyword_file, e)) return blacklist
def wait_up(hostname, port, attempts=10, timeout=2, wait=2): log.debug("Trying {0} times to connect to {1}:{2}".format(attempts, hostname, port)) attempt = 0 while True: attempt += 1 try: _attempt(hostname, port, timeout) log.debug("Connected to {0}:{1}".format(hostname, port)) break except socket.error as e: log.debug("Connection to {0}:{1} failed (attempt {2}/{3}): {4}".format(hostname, port, attempt, attempts, e)) if attempt >= attempts: log.error("Connection attempts exhausted when waiting for {0}:{1}".format(hostname, port)) # Python 2 has wierd exception stuff, just re-raise rather than wrap in AttemptsExhausted exception raise time.sleep(wait)
def main(): """ Main function to put the pieces together and run the recipe """ reg = RecipeRegistry() validate() try: ingredients = decode_ingredients(read_ingredients()) session = Session(ingredients['config'], ingredients['input'], ingredients['recipe'], FileUtil.get_directory_path(sys.argv[1])) reg.run_recipe(session) except RecipeValidationException as re: log.error(str(re)) exit_error() except RuntimeException as re: log.error(str(re)) exit_error() except WCSTException as re: log.error(str(re)) exit_error() except Exception as ex: log.error("An error has occured in the execution of the program. Error Message: " + str( ex) + "\nStack Trace: " + traceback.format_exc()) exit_error()