Exemplo n.º 1
0
    def apply_robot_instructions(self, instructions: str):
        """
		Apply robot instructions to rotate and/or move forward on the specified direction
		Check Grid edges and leave a "Robot's scent" of a dangerous position and direction

		Notice:
		1) Mars robots might be very expensive so instead of crashing them with another I've 
		decided to also skip this instruction if its the case.
		2) A Robot scent (x, y) position is not a prohibited coordinate if it is going on a different direction!
		"""
        if not self.robots:
            return

        instructions = StringUtils.normalize(instructions)
        StringUtils.validate(instructions, 100)

        robot = self.robots[-1]

        for instruction in instructions:
            if instruction not in ['L', 'R', 'F']:
                raise CustomExceptions.UnknownInstructionError(instruction)

            if instruction == 'F':
                robot_position = robot.pos()

                if not self.is_safe_position(robot_position):
                    continue

                final_x, final_y = self.calculate_next_positions(
                    robot_position)

                if self.check_collisions(final_x, final_y):
                    # This approach skips the dangerous instruction. This intentionally changes the route
                    print(
                        'ALERT: Robot would have crashed with another. Instruction skipped!'
                    )
                    continue
                """
				if the next position is outside the grid but there was none legacyScent
				the robot should leave one for posterity
				"""
                if self.is_outside_grid(final_x, final_y):
                    self.legacyScent.append(robot_position)
                    print(str(robot_position) + ' LOST')
                    robot.move()
                    return

                robot.move()
            else:
                robot.rotate(instruction)
        print(robot.pos())
Exemplo n.º 2
0
def __locate_col_index(sheet, workbook_sheet):
    cur_column = 0
    for name in workbook_sheet.row_values(1):
        name = StringUtils.strip_blank(name)
        if ["序号"].count(name) == 1:
            sheet.sequence_index = cur_column
        if ["博主", "账号名称"].count(name) == 1:
            sheet.blogger_name_index = cur_column
        if ["链接"].count(name) == 1:
            sheet.link_index = cur_column
        if ["粉丝数/万", "粉丝/万"].count(name) == 1:
            sheet.fans_num_index = cur_column
        if ["平均转发数"].count(name) == 1:
            sheet.forward_num_avg_index = cur_column
        if ["说明"].count(name) == 1:
            sheet.explain_index = cur_column
        if ["转发/元", "转发"].count(name) == 1:
            sheet.forward_price_index = cur_column
        if ["直发/元", "直发"].count(name) == 1:
            sheet.direct_send_price_index = cur_column
        if ["推荐级别"].count(name) == 1:
            sheet.recommend_level_index = cur_column
        if ["状态"].count(name) == 1:
            sheet.status_index = cur_column
        if ["状态码"].count(name) == 1:
            sheet.status_code_index = cur_column
        cur_column += 1

    return sheet
Exemplo n.º 3
0
 def getOrCreateArea(self, name):
     area = self.getAreaForName(name)
     if area is None:
         areaId = StringUtils.normaliseName(name, toLower=True, spacesToUnderscore=True)
         area = Area(id=areaId, name=name, parent=ndb.Key("Country", self.key.id()))
         area.put()
     return area
Exemplo n.º 4
0
def xiaoi_robot(txt,userId='客官'):
    url="http://robot.open.xiaoi.com/ask.do?question=%s&userId=%s&type=0&platform=web"%(txt,userId)

    appKey = "open_oY2gsF6Ueint";
    appSecret = "srAx9cuHDjaFN2N7zw4v";

    str1 = ':'.join( [appKey,'xiaoi.com',appSecret] )
    ha1 = StringUtils.sha1hex(str1)
    ha2 =  StringUtils.sha1hex('GET:/ask.do')
    nonce = StringUtils.buildRandom("",36)

    sig =  StringUtils.sha1hex( ":".join( [ha1,nonce,ha2]) )
    h1={'X-Auth':'app_key="'+appKey+'",nonce="'+nonce+'",signature="'+sig+'"'}

    res = getHtmlText(url,header=h1)
    return res
Exemplo n.º 5
0
def normalize_content(document):
    sentences = sent_tokenize(document)
    sentences = [
        normalize_sentence(sentence) for sentence in sentences
        if StringUtils.is_not_empty(sentence)
    ]
    return ' '.join(sentences)
Exemplo n.º 6
0
 def insert_one(self, table, **kwargs):
     """
     插入一条到数据库(先判断 id 是否存在,存在为更新,不在为插入)
     :param table: 要保存的表
     :param kwargs:
     :return:
     """
     if len(kwargs) > 0:
         keys = ''
         parameters = ''
         params = []
         for key, value in kwargs.iteritems():
             keys = keys + key + ', '
             params.append(value)
             parameters = parameters + '%s' + ', '
         keys = keys[:-2]  # 最后一个逗号不要
         parameters = parameters[:-2]
         # sql_insert = 'insert into ' + table + ' ( ' + keys + ' )' + ' values ' + '( ' + parameters + ' )'
         sql_insert = StringUtils.connect('insert into ', table, ' ( ',
                                          keys, ' )', ' values ', '( ',
                                          parameters, ' )')
         try:
             result = self.execute_rowcount(sql_insert, *params)
             self.commit()
             return result
         except Exception as e:
             print __name__, '插入条目到数据库出错: ', e
             self.rollback()
             return -1
     else:
         return -1
Exemplo n.º 7
0
 def update_one(self, table, id_item, **kwargs):
     """
     对execute_rowcount的一个封装
     :param table:
     :param id_item:
     :param kwargs:
     :return:
     """
     if len(kwargs) > 0:
         try:
             str_key = ''
             params = []
             for key, value in kwargs.iteritems():
                 str_key = str_key + key + '=%s, '
                 params.append(value)
             str_key = str_key[:-2]
             # sql = 'update ' + table + ' set ' + str_key + ' where id=' + id_item
             # 为了安全,如果id_item是字符串,去除 =
             if isinstance(id_item, type('')):
                 id_item = id_item.replace('=', '')
             elif isinstance(id_item, type(u'')):
                 id_item = id_item.replace(u'=', u'')
             sql = StringUtils.connect('update ', table, ' set ', str_key,
                                       ' where id=', id_item)
             result = self.execute_rowcount(sql, *params)
             self.commit()
             return result
         except Exception as e:
             print __name__, '更新条目出错:', e
             self.rollback()
             return -1
     else:
         return -1
Exemplo n.º 8
0
def normalize_sentence(sentence):
    sentence = sentence.lower().replace('/', ' ')
    words = word_tokenize(sentence)
    words = [
        normalize_word(word) for word in words
        if StringUtils.is_not_empty(word)
    ]
    sentence = ' '.join(words)
    sentence = add_dot_at_end_of_line(sentence.strip(' \t\n\r'))
    return sentence
Exemplo n.º 9
0
def get_pre_login_status(username_):
    """
        Perform pre login action, get pre login status, including server time, nonce, rsa kv, etc.
    """
    pre_login_url_ = PRE_LOGIN_URL % get_user(username_)
    data_ = StringUtils.convert_to_str(HttpClient.get(pre_login_url_))
    p_ = re.compile('\((.*)\)')
    json_data = p_.search(data_).group(1)
    data_ = json.loads(json_data)
    server_time_ = str(data_['servertime'])
    nonce_ = data_['nonce']
    rsa_kv_ = data_['rsakv']
    return server_time_, nonce_, rsa_kv_
Exemplo n.º 10
0
def baidu_record_to_txt(msg):
    try:
        rndstr = StringUtils.buildRandom()
        bsio = BytesIO(msg.get_file())
        audio = AudioSegment.from_mp3(bsio)
        audio2 = audio+6
        export = audio2.export(out_f='amr/%s.amr'%(rndstr),format="amr", bitrate="12.20k")
        bsMp3 = export.read()
        transform = aipSpeech.asr(bsMp3, 'amr', 8000, {'dev_pid': '1536', })

        if transform['err_no'] ==0:
            return  '|'.join(transform['result'])
        else:
            print(transform['err_no'],transform['err_msg'])
    except Exception as er:
        print(er);
Exemplo n.º 11
0
def get_pwd_rsa(pwd_, server_time_, nonce_):
    """
        Get rsa2 encrypted password, using RSA module from https://pypi.python.org/pypi/rsa/3.1.1, documents can be accessed at
        http://stuvel.eu/files/python-rsa-doc/index.html
    """
    # n, n parameter of RSA public key, which is published by WEIBO.COM
    # hardcoded here but you can also find it from values return from prelogin status above
    weibo_rsa_n_ = 'EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443'
    # e, exponent parameter of RSA public key, WEIBO uses 0x10001, which is 65537 in Decimal
    weibo_rsa_e_ = 65537
    message_ = str(server_time_) + '\t' + str(nonce_) + '\n' + str(pwd_)
    message_ = StringUtils.convert_to_bytes(message_)
    #construct WEIBO RSA Publickey using n and e above, note that n is a hex string
    key_ = rsa.PublicKey(int(weibo_rsa_n_, 16), weibo_rsa_e_)
    #get encrypted password
    encrypt_pwd_ = rsa.encrypt(message_, key_)
    #trun back encrypted password binaries to hex string
    return binascii.b2a_hex(encrypt_pwd_)
Exemplo n.º 12
0
    def get_total_pages(self, table, category, items_per_page):
        """

        :param table:
        :param category:
        :param items_per_page:
        :return:
        """
        try:
            sql_total = StringUtils.connect("select count(*) from ", table,
                                            " where category=%s")
            all_items = self.db.count(sql_total, category)
            if all_items % items_per_page != 0:
                page_total = all_items / items_per_page + 1
            else:
                page_total = all_items / items_per_page
            return page_total
        except Exception as e:
            print __name__, 'total_pages except: ', e
            return 0
Exemplo n.º 13
0
def get_user(username_):
    username_ = urllib.parse.quote(username_)
    username_ = StringUtils.convert_to_bytes(username_)
    username_ = base64.encodebytes(username_)[:-1]
    return username_
Exemplo n.º 14
0
PATH_STATIC = os.path.join(PATH_BASE, NAME_STATIC)
# 模板路径
PATH_TEMPLATE = os.path.join(PATH_BASE, NAME_TEMPLATE)


# 以下路径都是不含静态文件夹名的相对路径
# 默认使用文件路径保存的路径(files/default/)(该文件夹下放:默认logo、默认缩略图、验证码图片)
PATH_DEFAULT_FILE = os.path.join(NAME_FILE_ROOT, 'default')
# 上传文件的路径(files/upload/)
PATH_UPLOAD = os.path.join(NAME_FILE_ROOT, 'upload')    # 这样写系统相关,使用时要通过os.path.join转为系统无关
# 临时文件存放夹(该文件夹可随意删)(files/temp/)
# PATH_TEMP = os.path.join(NAME_FILE_ROOT, 'temp')
PATH_TEMP = os.path.join(PATH_STATIC, NAME_FILE_ROOT, 'temp')
# 上传的 logo 保存的路径(保存在 upload 文件夹下)
PATH_LOGO = 'logo'

# 默认缩略图路径(不含静态文件夹名,url 形式)
PATH_THUMB_DEFAULT = 'files/default/default_thumb.jpg'
# 默认logo文件的路径(数据库没有内容时用)
PATH_LOGO_DEFAULT = 'files/default/logo.png'


# 验证码存放相对路径
PATH_VERIFY_SAVE_RELATIVE = PathUtils.connect(PATH_STATIC, 'files/default')
# 验证码存放绝对路径
PATH_VERIFY_SAVE_ABSOLUTE = PathUtils.connect(PATH_STATIC, 'files/default/verify.jpg')
# 验证码网页使用路径
PATH_VERIFY_URL = StringUtils.connect('/', NAME_STATIC, '/', 'files/default/verify.jpg')
# 验证码字体文件路径
PATH_VERIFY_FONT = PathUtils.connect(PATH_STATIC, 'files/default/Monaco.ttf')
Exemplo n.º 15
0
def do_login():
    logging.info('Starting to login...')
    username_ = Configuration.UESRNAME
    pwd_ = Configuration.PASSWORD
    """"
        Perform login action with use name, password and saving cookies.
        @param username_: login user name
        @param pwd_: login password
        @param cookie_file_: file name where to save cookies when login succeeded
    """
    # POST data per login weibo, these fields can be captured using httpfox extension in Firefox
    login_data_ = {
        'entry': 'weibo',
        'gateway': '1',
        'from': '',
        'savestate': '7',
        'userticket': '1',
        'pagerefer': '',
        'vsnf': '1',
        'su': '',
        'service': 'miniblog',
        'servertime': '',
        'nonce': '',
        'pwencode': 'rsa2',
        'rsakv': '',
        'sp': '',
        'encoding': 'UTF-8',
        'prelt': '45',
        'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
        'returntype': 'META'
    }
    login_url_ = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.11)'
    try:
        server_time_, nonce_, rsakv_ = get_pre_login_status(username_)
    except Exception as e:
        logging.error(e)
        return
    # Fill POST data
    print('starting to set login_data')
    login_data_['servertime'] = server_time_
    login_data_['nonce'] = nonce_
    login_data_['su'] = get_user(username_)
    login_data_['sp'] = get_pwd_rsa(pwd_, server_time_, nonce_)
    login_data_['rsakv'] = rsakv_

    text_ = HttpClient.get(login_url_, login_data_)
    text_ = StringUtils.convert_to_str(text_, Charset.GBK)
    p_ = re.compile('location\.replace\(\'(.*?)\'\)')
    try:
        # Search login redirection URL
        login_url_ = p_.search(text_).group(1)
        data_ = HttpClient.get(login_url_)
        data_ = StringUtils.convert_to_str(data_, Charset.GBK)
        #Verify login feedback, check whether result is TRUE
        patt_feedback_ = 'feedBackUrlCallBack\((.*)\)'
        p_ = re.compile(patt_feedback_, re.MULTILINE)
        feedback_ = p_.search(data_).group(1)
        feedback_json_ = json.loads(feedback_)
        if feedback_json_['result']:
            HttpClient.save_cookie_in_file()
    except Exception as e:
        logging.error(e)
Exemplo n.º 16
0
 def format_title(info):
     if info:
         title = StringUtils.format_str_info(info)
     else:
         title = ""
     return title
Exemplo n.º 17
0
    def save_upload_file(self, post_streamer, user_id, user_category_info,
                         file_title):
        """
        返回文件路径、文件 id
        :param post_streamer:
        :param user_id: 文件使用者id
        :param user_category_info: 文件使用者的分类
        :param file_title: 文件名称
        :return:
        """
        def clean_file_name(file_name):
            """
            去除文件名中不规范的项目(利用正则表达式)
            """
            re_str = r"[\/\\\:\*\?\"\<\>\| _]"  # '/\:*?"<>|'
            return re.sub(re_str, "", file_name)

        # 获取文件信息
        file_info = {}
        for part in post_streamer.parts:
            """
            [
                { headers:[ {params, name, value}, {params, name, value} ], tempile, size },    # part

                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
            ]
            0、poststreamer.parts,为一个 list 对象,其总共有六个 dict(headers、tempfile、size) 元素
            1、size 文件大小
            2、tempfile 值是一个临时文件对象 (转存要用到)
            4、headers 值是一个 list [ {params, name, value}, ]
            5、六个 dict 中,第一个为需要的

            """
            try:
                file_args = {}
                part["tmpfile"].close()
                # 获取文件后缀
                params = part["headers"][0].get("params", None)
                filename = params[
                    'filename']  # 只有上传文件对应的part才有该键,通过抛出异常来获得有效文件
                fill_suffix = PathUtils.get_file_suffix(filename)
                file_args['id'] = TimeUtils.time_id()
                file_args['user_id'] = user_id
                file_args['user_category'] = user_category_info['category']
                file_args['file_category'] = 'attachment'
                file_args['created'] = TimeUtils.datetime_date_simple()
                file_args['size'] = part["size"]
                file_args['title'] = clean_file_name(file_title)
                if len(file_args['title']) == 0:
                    file_args['title'] = str(file_args['id'])
                # 文件类型
                full_file_type = part["headers"][1].get("value", "text/plain")
                if '/' in full_file_type:
                    file_args['type'] = full_file_type.split('/')[0]
                elif '\\' in full_file_type:
                    file_args['type'] = full_file_type.split('\\')[0]
                elif '\\\\' in full_file_type:
                    file_args['type'] = full_file_type.split('\\\\')[0]
                # 文件名:id_user file_title suffix
                name_file = StringUtils.connect(file_args['user_category'],
                                                '_', file_args['user_id'], '-',
                                                file_args['id'], '-',
                                                file_args['title'],
                                                fill_suffix)
                path_folder_relative = self.make_path_folder_relative_no_static(
                )
                path_folder_absolute = PathUtils.connect(
                    path.PATH_STATIC, path_folder_relative)
                file_args['path'] = PathUtils.to_url(
                    PathUtils.connect(path_folder_relative, name_file))
                path_save = PathUtils.connect(path_folder_absolute, name_file)
                if isinstance(path_save, type('')):
                    path_save = path_save.decode('utf-8')
                # 创建文件夹路径
                if not os.path.exists(path_folder_absolute):
                    os.makedirs(path_folder_absolute)
                # 转存文件(这步将临时文件保存为正式文件, 关键)
                os.rename(part["tmpfile"].name, path_save)
                if not os.path.exists(path_save):  # 判断是否转存成功
                    file_info = {}
                    continue
                file_info = file_args
                # break # 不能终止循环,要通过异常,来删除临时文件
            except Exception as e:
                # traceback.print_exc()
                part["tmpfile"].close()
                os.unlink(part["tmpfile"].name)  # 删除临时文件(有多个,只有一个是上传文件)
        # 将信息写入数据库
        if len(file_info) > 0:
            result = self.insert_one_item(user_category_info['file_table'],
                                          **file_info)
            if result != -1:
                return {
                    '': file_info['id'],
                    'file_path': file_info['path'],
                    'file_title': file_info['title']
                }
            else:
                return {}
        else:
            return {}
Exemplo n.º 18
0
 def forName(name):
     id = StringUtils.normaliseName(name, toLower=True, spacesToUnderscore=True)
     newCountry = Country.get_by_id(id)
Exemplo n.º 19
0
 def getNormalisedCountryName(sa):
     return StringUtils.normaliseName(sa.country, toLower=True, spacesToUnderscore=True, dashesToUnderscore=True)
Exemplo n.º 20
0
 def getAreaForName(self, areaName):
     areaId = StringUtils.normaliseName(areaName, toLower=True, spacesToUnderscore=True)
     area = Area.get_by_id(areaId, parent=self.key)
     return area
Exemplo n.º 21
0
def read_excel_file(input_file_name):
    excel = __check_excel_file(input_file_name)

    cur_sheet_index = 0
    for workbook_sheet in excel.workbook.sheets():
        sheet = Sheet(cur_sheet_index, workbook_sheet.name)

        sheet = __locate_col_index(sheet, workbook_sheet)

        for index in range(workbook_sheet.nrows):
            if index >= 2:
                row = Row()

                # 序号,重排序号
                row.sequence = index - 2 + 1

                # sheet序号
                row.sheet_sequence = cur_sheet_index

                # 博主名
                if sheet.blogger_name_index >= 0:
                    blogger_excel_name = str(workbook_sheet.cell(index, sheet.blogger_name_index).value).strip()
                    blogger_real_name = blogger_excel_name
                    if blogger_excel_name != "" and blogger_excel_name.endswith("V"):
                        blogger_real_name = blogger_excel_name[0 : len(blogger_excel_name) - 1]
                    row.blogger_excel_name = StringUtils.strip_blank(blogger_excel_name)
                    row.blogger_real_name = StringUtils.strip_blank(blogger_real_name)

                # 链接
                if sheet.link_index >= 0:
                    link = str(workbook_sheet.cell(index, sheet.link_index).value).strip()
                    row.link = StringUtils.strip_blank(link.replace("e.weibo.com", "weibo.com"))

                # 粉丝数/万
                if sheet.fans_num_index >= 0:
                    fans_num = str(workbook_sheet.cell(index, sheet.fans_num_index).value).strip()
                    fans_num = StringUtils.convert_to_float(fans_num)
                    row.fans_num = round(float("%.2f" % float(fans_num)), 1)

                # 平均转发数
                if sheet.forward_num_avg_index >= 0:
                    forward_num_avg = str(workbook_sheet.cell(index, sheet.forward_num_avg_index).value).strip()
                    row.forward_num_avg = StringUtils.convert_to_int(forward_num_avg, "-1")

                # 说明
                if sheet.explain_index >= 0:
                    explain = str(workbook_sheet.cell(index, sheet.explain_index).value).strip()
                    row.explain = StringUtils.strip_blank(explain)

                # 转发
                if sheet.forward_price_index >= 0:
                    forward_price = str(workbook_sheet.cell(index, sheet.forward_price_index).value).strip()
                    row.forward_price = StringUtils.convert_to_int(forward_price, "-1")

                # 直发
                if sheet.direct_send_price_index >= 0:
                    direct_send_price = str(workbook_sheet.cell(index, sheet.direct_send_price_index).value).strip()
                    row.direct_send_price = StringUtils.convert_to_int(direct_send_price, "-1")

                # 推荐级别
                if sheet.recommend_level_index >= 0:
                    recommend_level = str(workbook_sheet.cell(index, sheet.recommend_level_index).value).strip()
                    row.recommend_level = StringUtils.strip_blank(recommend_level)

                # 状态码
                if sheet.status_code_index >= 0:
                    status_code = str(workbook_sheet.cell(index, sheet.status_code_index).value).strip()
                    row.status_code = StringUtils.convert_to_int(status_code)

                sheet.rows.append(row)

        excel.sheets.append(sheet)
        cur_sheet_index += 1

    return excel