def __init__(self, numero, palavra): self.numero = numero self.nivel = 0 self.palavra_original = "" self.palavra_canonica = "" self.tags = [] self.tag_inicial = "" nivelEnd = StringUtil.find(palavra, ".") parentesesInicio = StringUtil.find(palavra, "(") parentesesFim = StringUtil.find(palavra, ")") if (nivelEnd == -1): raise Exception("Erro: Formato da palavra inválido('" + palavra + "'). Palavra.__init__") self.nivel = palavra[:nivelEnd] if (parentesesInicio == -1 or parentesesFim == -1): self.tag_inicial = palavra[nivelEnd + 2:] else: tagsDentroParenteses = palavra[parentesesInicio + 1:(parentesesFim - len(palavra))].split() self.tag_inicial = palavra[nivelEnd + 2:parentesesInicio] self.palavra_original = palavra.split()[-1].replace("*", "") self.palavra_canonica = tagsDentroParenteses[0].replace( "\"", "").replace("*", "") self.tags = tagsDentroParenteses[1:]
def etapa2_removeDadosNaoNecessarios(etapa1_retorno_palavras): """ Transforma o retorno do palavras em uma lista e remove dados/linhas não necessárias. ex: "<ß>"/"<servicos>" :param etapa1_retorno_palavras: Retorno do método "etapa1_getResultadoPalavras" :return: lista com as informações relavantes do retorno do palavras. """ etapa1_retorno_palavras_list = etapa1_retorno_palavras.split( BREAK_LINE) listaFinal = [] dentroA1 = False for i in range(len(etapa1_retorno_palavras_list)): if (etapa1_retorno_palavras_list[i] == 'A1'): dentroA1 = True continue if (dentroA1 and StringUtil.isEmpty(etapa1_retorno_palavras_list[i])): dentroA1 = False if (dentroA1): listaFinal.append( StringUtil.removeStrings([TAB, BREAK_LINE], etapa1_retorno_palavras_list[i], " ")) return listaFinal
def testStringUtil(self): # print(random.randint(1, 10)) # print(random.randint(5000, 5017)) # from utils.StringUtil import is_empty # is_empty("") from utils import StringUtil print( StringUtil.hump_case_underline_lower("oneHTTPRequest12AaureBBBXu")) print(StringUtil.underline2hump("hello_word")) print(StringUtil.hump2underline("URLCamelCaseHello12Word")) json_str = """{"userName":"******"}""" print(StringUtil.json_hump2underline(json_str))
def testSplit(self): strs = StringUtil.split(",,", ",") for i in range(len(strs)): self.assertEqual("", strs[i]) strs = StringUtil.split("1,,", ",") self.assertEqual("1", strs[0]) self.assertEqual("", strs[1]) self.assertEqual("", strs[2]) strs = StringUtil.split("1,,1", ",") self.assertEqual("1", strs[0]) self.assertEqual("", strs[1]) self.assertEqual("1", strs[2])
def sendMail(self, mailTo: list, subject: str, content: str, cc: list = [], _charset: str = "UTF-8", mimeType: MimeType = MimeType.PLAIN ): """ 发送邮件的方法 :param mailTo: toAddr 发送到的邮箱地址 需要是数组 :param subject: 标题 :param content: 内容 :param cc 抄送 :param _charset 编码 :param mimeType 邮件格式 """ if not isinstance(mailTo, list): logging.error("mailTo: TypeError: must be list") return if cc is not None and not isinstance(cc, list): logging.error("cc: TypeError: must be list") return mail = MIMEMultipart() msg = MIMEText(content, _subtype=mimeType.value, _charset=_charset) mail.attach(msg) mail['To'] = self.__formatAddr("<%s>" % StringUtil.arrayToStr(mailTo, ';')) if not CommonUtil.isEmpty(cc): mail['Cc'] = StringUtil.arrayToStr(cc, ';') mailTo = mailTo + cc mail['Subject'] = Header(subject, 'UTF-8').encode() mail['From'] = self.__formatAddr(self.__fromAlias + ' <%s>' % self.__mailFrom) try: server = smtplib.SMTP(self.__smtpServer, self.__smtpPort) server.set_debuglevel(1) server.login(self.__mailFrom, self.__password) server.sendmail(self.__mailFrom, mailTo, mail.as_string()) except smtplib.SMTPException: logging.error("邮件发送失败") finally: server.quit()
def path_join(*path): """ 路径拼接 :param path:路径字符串数组 :return: """ final_path = "" for i in range(len(path)): p = path[i] if StringUtil.is_empty(p): continue if StringUtil.check_startswith(p): p = p[1:] if StringUtil.check_endswith(p): p = p[:-1] if i == 0: final_path = p else: final_path = os.path.join(final_path, p)
def short_time_mail_apply(): """ 随机申请shorttimemail.com邮箱 :return: 邮箱号 """ prefix = StringUtil.random_lowercase_alphanumeric(9) suffix = "@shorttimemail.com" data = {"prefix": prefix, "suffix": suffix} # post续期30分钟:/mail/continue # post销毁:/mail/destory # post删除邮件:/mail/delete ,参数:{ ids: ids.join('|') }以|分割字符串 res = HttpUtil.get_json(url=Constants.SHORT_TIME_MAIL + "/mail/apply", data=data) if res.code != 200: raise MsgException(res.msg) return prefix + suffix
def testIsEmpty(self): self.assertTrue(StringUtil.isEmpty("")) self.assertTrue(StringUtil.isEmpty(None)) self.assertFalse(StringUtil.isEmpty("me"))
def testRegex(self): string = r"1234" self.assertTrue(StringUtil.matchRegex(r"[0-9]*", string))
def testSubString(self): string = "0123456789" self.assertEqual(StringUtil.subString(string, 0, 6), "012345") self.assertEqual(StringUtil.subString(string, 0, 6, 2), "024")
def testArrayToString(self): list = [1, 2, 3] self.assertEqual("1,2,3", StringUtil.arrayToStr(list, ",")) self.assertEqual("[1,2,3]", StringUtil.arrayToStr(list, ",", "[", "]"))
def get_url_dp(product): """ 通过ChromeDP获取下载产品信息 :param product: 产品 :return: """ if product == "" or product is None: raise MsgException("产品不能为空") if netsarang_info is not None and netsarang_info and product in netsarang_info.keys( ): info = netsarang_info[product] # 如果数据不为空,并且日期为今天,这么做是为了避免消耗过多的性能,每天只查询一次 if len(info) > 1 and not info and TimeUtil.date_compare_to( info[0], datetime.now()) == 0: return info[1] prefix = StringUtil.random_lowercase_alphanumeric(9) suffix = Mail.lin_shi_you_xiang_suffix() Mail.lin_shi_you_xiang_apply(prefix) mail = prefix + suffix send_mail_dp(mail, product) time.sleep(10) mail_list = Mail.lin_shi_you_xiang_list(prefix) mail_len = len(mail_list) if mail_len == 0 or not mail_list: raise MsgException("邮件列表为空!") mailbox = mail_list[mail_len - 1]["mailbox"] if mailbox is None or mailbox == "": raise MsgException("邮件列表为空!") mail_id = mail_list[mail_len - 1]["id"] if mail_id is None or mail_id == "": raise MsgException("邮件ID为空!") # 获取最新一封邮件 mail_content = Mail.lin_shi_you_xiang_get_mail(mailbox, mail_id) # 解密,邮件协议Content-Transfer-Encoding指定了base64 html_text = base64.b64decode(mail_content.split("AmazonSES")[1]) # 解析解密后的HTML html = BeautifulSoup(html_text, features="html.parser") # 查找带token的标签的值 href = html.find("a", {"target": "_blank"}).text # 请求token链接地址获取下载链接 # bs = ReptileUtil.selenium_bs(href) # href = bs.find("a", {"target": "download_frame"})["href"] try: driver = ReptileUtil.selenium_driver(href) driver.implicitly_wait(10) href = driver.find_element_by_css_selector( "a[target='download_frame']").get_attribute("href") finally: # 关闭当前窗口。 driver.close() # 关闭浏览器并关闭chreomedriver进程 driver.quit() href = href.replace(".exe", "r.exe") # 把产品信息存储到变量 netsarang_info[product] = [datetime.now(), href] return href
获取邮箱列表 :param prefix: 邮箱前缀 :return: """ url = Constants.LIN_SHI_YOU_XIANG + "/api/v1/mailbox/" + prefix return HttpUtil.get_json(url=url, data=None) def lin_shi_you_xiang_get_mail(prefix, id): url = Constants.LIN_SHI_YOU_XIANG + "/mailbox/" + prefix + "/" + id + "/source" return HttpUtil.get(url=url, data=None).text def lin_shi_you_xiang_delete(prefix, id): """ 删除邮件delete请求 :param id: 邮件编号 :param prefix: 邮箱前缀 :return: """ url = Constants.LIN_SHI_YOU_XIANG + "/api/v1/mailbox/" + prefix + "/" + id res = HttpUtil.delete(url=url, data=None) res_json = json.loads(res.text) if __name__ == '__main__': prefix = StringUtil.random_lowercase_alphanumeric(9) suffix = lin_shi_you_xiang_suffix() lin_shi_you_xiang_apply(prefix) lin_shi_you_xiang_list(prefix)