Ejemplo n.º 1
0
 def GET(self):
     if xconfig.is_mute():
         return dict(code="fail", message="mute")
     try:
         tm = time.localtime()
         if tm.tm_hour >= 0 and tm.tm_hour <= 6:
             return False
         if tm.tm_hour == 7 and tm.tm_min < 30:
             return False
         if tm.tm_min == 0:
             msg = "现在时间是%s点整" % tm.tm_hour
         else:
             msg = "现在时间是%s点%s分" % (tm.tm_hour, tm.tm_min)
         if tm.tm_hour >= 23:
             return False
             msg += ",夜深了,请注意休息"
         xutils.say(msg)
         # voice.Release()
         return dict(code="success", message="")
     except Exception as e:
         return dict(code="fail", message=str(e))
     else:
         pass
     finally:
         pass
Ejemplo n.º 2
0
def handle_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--data", default="./data")
    parser.add_argument("--delay", default="0")
    parser.add_argument("--ringtone", default="no")
    parser.add_argument("--port", default="1234")
    parser.add_argument("--webbrowser", default="no")
    parser.add_argument("--debug", default="yes")
    parser.add_argument("--minthreads", default="10")

    args = parser.parse_args(sys.argv[1:])

    # 处理Data目录
    xconfig.set_data_path(args.data)
    # 端口号
    xconfig.PORT = args.port

    # 延迟加载,避免定时任务重复执行
    delay = int(args.delay)
    time.sleep(delay)

    # 启动提醒
    if args.ringtone == "yes":
        xutils.say("系统启动")
    if args.webbrowser == "yes":
        xconfig.OPEN_IN_BROWSER = True
    if args.debug == "yes":
        xconfig.DEBUG = True
    else:
        web.config.debug = False

    xconfig.minthreads = int(args.minthreads)
    web.config.minthreads = xconfig.minthreads
Ejemplo n.º 3
0
 def GET(self):
     for i in range(1, 31):
         if not check_network_health():
             xutils.say("网络连接异常,重试第%s次" % i)
         else:
             xutils.say("网络连接正常")
             return dict(code="success")
         time.sleep(1)
     return dict(code="fail", message="网络连接异常")
Ejemplo n.º 4
0
    def GET(self):
        path = xutils.get_argument("path")
        length = 1000
        read = xutils.get_argument("read", "false")
        direction = xutils.get_argument("direction", "forward")
        encoding = "utf-8"
        page = 0

        if not path:
            return dict(code="fail", message="parameter path is empty")

        print("path:", path)
        path = xutils.get_real_path(path)
        print("real path:", path)

        if not os.path.exists(path):
            return dict(code="fail", message="file `%s` not exists" % path)

        basename, ext = os.path.splitext(path)
        key = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path))
        bookmark = xutils.cache_get(key, {})
        bookmark['path'] = path
        # bookmarkpath = '%s@%s.bookmark' % (xauth.get_current_name(), basename)
        # bookmark = dict()
        # if os.path.exists(bookmarkpath):
        #     try:
        #         bookmark = json.loads(xutils.readfile(bookmarkpath))
        #         if not isinstance(bookmark, dict):
        #             bookmark = dict()
        #     except:
        #         pass

        page = bookmark.get("page", 0)
        size = xutils.get_file_size(path, format=False)

        with open(path, encoding=encoding) as fp:
            text = "dummy"
            if direction == "backward":
                page = page - 1
            if direction == "forward":
                page = page + 1
            if page < 0:
                page = 0
            bookmark["page"] = page
            self.seek_page(fp, bookmark, length)
            current = fp.tell()
            text = fp.read(length)
            if read == "true":
                xutils.say(text)
            if direction in ("forward", "backward"):
                # xutils.writefile(bookmarkpath, json.dumps(bookmark))
                xutils.cache_put(key, bookmark)
        return dict(code="success",
                    data=text,
                    page=page,
                    current=current,
                    size=size)
Ejemplo n.º 5
0
    def GET(self):
        path      = xutils.get_argument("path")
        length    = 1000
        read      = xutils.get_argument("read", "false")
        direction = xutils.get_argument("direction", "forward")
        page      = 0

        if not path:
            return dict(code = "fail", message = "parameter path is empty")

        debug_info("path:", path)
        path = xutils.get_real_path(path)
        debug_info("real path:", path)

        if not os.path.exists(path):
            return dict(code = "fail", message = "file `%s` not exists" % path)

        basename, ext    = os.path.splitext(path)
        key              = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path))
        bookmark         = xutils.cache_get(key, {})
        bookmark['path'] = path
        page             = bookmark.get("page", 0)
        size             = xutils.get_file_size(path, format=False)
        debug_info("bookmark info:", bookmark)

        encoding = fsutil.detect_encoding(path)
        debug_info("detected encoding:", encoding)
        
        with open(path, encoding = encoding) as fp:
            text = "dummy"
            if direction == "backward":
                page = page - 1
            if direction == "forward":
                page = page + 1
            if page < 0:
                page = 0
            try:
                bookmark["page"] = page
                seek_page(fp, bookmark, length)
                current = fp.tell()
                text    = fp.read(length)
            except UnicodeDecodeError as e:
                # xutils.print_exc()
                bookmark['page'] = 0
                seek_page(fp, bookmark, length);
                current = fp.tell()
                text    = fp.read()

            if read == "true":
                xutils.say(text)

            if direction in ("forward", "backward"):
                # xutils.writefile(bookmarkpath, json.dumps(bookmark))
                xutils.cache_put(key, bookmark)
        return dict(code="success", data=text, page=page, current=current, size=size)
Ejemplo n.º 6
0
 def default_request(self):
     content = xutils.get_argument("content")
     try:
         xutils.say(content)
         return dict(code="success")
     except Exception as e:
         xutils.print_stacktrace()
         return dict(code="fail", message=str(e))
     finally:
         # 报异常
         # voice.Release()
         # return "OK"
         pass
Ejemplo n.º 7
0
 def GET(self, msg=None):
     repeat = xutils.get_argument("repeat", 3, type=int)
     content = xutils.get_argument("content")
     repeat = min(10, repeat)
     if msg is None:
         msg = content
     if xconfig.is_mute():
         return dict(code="fail", message="mute")
     msg = xutils.unquote(msg)
     for i in range(repeat):
         xutils.say(msg)
         time.sleep(2)
     return dict(code="success")
Ejemplo n.º 8
0
def handle_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--data", default="./data")
    parser.add_argument("--delay", default="0")
    parser.add_argument("--ringtone", default="no")
    parser.add_argument("--port", default="1234")
    parser.add_argument("--webbrowser", default="no")
    parser.add_argument("--debug", default="yes")
    parser.add_argument("--minthreads", default="10")
    parser.add_argument("--useCacheSearch", default="no")
    parser.add_argument("--useUrlencode", default="no")
    parser.add_argument("--devMode", default="no")
    parser.add_argument("--initScript", default=None)
    parser.add_argument("--master", default="no")
    parser.add_argument("--test", default="no")

    web.config.debug = False
    args = parser.parse_args(sys.argv[1:])

    # 处理Data目录
    xconfig.init(args.data)
    # 端口号
    xconfig.PORT = args.port

    # 延迟加载,避免定时任务重复执行
    delay = int(args.delay)
    time.sleep(delay)

    # 启动提醒
    if args.ringtone == "yes":
        xutils.say("系统启动")
    if args.webbrowser == "yes":
        xconfig.OPEN_IN_BROWSER = True
    if args.debug == "yes":
        xconfig.DEBUG = True
        web.config.debug = True
    if args.useCacheSearch == "yes":
        xconfig.USE_CACHE_SEARCH = True
    if args.useUrlencode == "yes":
        xconfig.USE_URLENCODE = True
    if args.devMode == "yes":
        xconfig.DEV_MODE = True
    if args.test == "yes":
        xconfig.IS_TEST = True

    xconfig.minthreads = int(args.minthreads)
    xconfig.INIT_SCRIPT = args.initScript
    web.config.minthreads = xconfig.minthreads
Ejemplo n.º 9
0
    def GET(self):
        path = xutils.get_argument("path")
        length = 1000
        read = xutils.get_argument("read", "false")
        direction = xutils.get_argument("direction", "forward")
        encoding = "utf-8"
        page = 0

        print("path:", path)
        path = xutils.get_real_path(path)
        print("real path:", path)

        basename, ext = os.path.splitext(path)
        bookmarkpath = basename + ".bookmark"
        bookmark = dict()
        if os.path.exists(bookmarkpath):
            try:
                bookmark = json.loads(xutils.readfile(bookmarkpath))
                if not isinstance(bookmark, dict):
                    bookmark = dict()
            except:
                pass
        page = bookmark.get("page", 0)
        size = xutils.get_file_size(path, format=False)

        with open(path, encoding=encoding) as fp:
            text = "dummy"
            if direction == "backward":
                page = page - 1
            if direction == "forward":
                page = page + 1
            if page < 0:
                page = 0
            bookmark["page"] = page
            self.seek_page(fp, bookmark, length)
            current = fp.tell()
            text = fp.read(length)
            if read == "true":
                xutils.say(text)
            if direction in ("forward", "backward"):
                xutils.savetofile(bookmarkpath, json.dumps(bookmark))
        return dict(code="success",
                    data=text,
                    page=page,
                    current=current,
                    size=size)
Ejemplo n.º 10
0
def search(ctx, delay_mins_str, message):
    if not xauth.is_admin():
        return []

    delay_mins = parse_int(delay_mins_str)
    millis = time.time() + int(delay_mins) * 60
    tm = time.localtime(millis)
    tm_hour = tm.tm_hour
    tm_min = tm.tm_min
    add_alarm(tm_hour, tm_min, message)

    result = SearchResult()
    result.name = "提醒"
    result.raw = "提醒创建成功,将于%s点%s分提醒 %s" % (tm_hour, tm_min, message)
    result.url = "/system/crontab"
    xutils.say(result.raw)
    return [result]
Ejemplo n.º 11
0
def by_time(ctx, period, time_str, message):
    if not xauth.is_admin():
        return None
    print(period, time_str, message)
    v = time_pattern.findall(time_str)
    if len(v) > 0:
        tm_hour, tm_min = v[0]
        tm_hour = int(tm_hour)
        if period == "下午":
            tm_hour += 12
        if tm_min == "半":
            tm_min = 30
        if tm_min == "":
            tm_min = 0
        add_alarm(tm_hour, tm_min, message)
        out = "提醒创建成功,将于%s点%s分提醒%s" % (tm_hour, tm_min, message)
        xutils.say(out)
        return [SearchResult("提醒", "/system/crontab", out)]
Ejemplo n.º 12
0
    def GET(self):
        city_code = xutils.get_argument("city_code", "101020100")
        city_name = xutils.get_argument("city_name", "上海")
        city_name = xutils.u(city_name)

        message = None

        db = xtables.get_record_table()
        record = db.select_one(where="type='weather' AND DATE(ctime)=$date_str AND key=$key", 
            vars=dict(date_str=xutils.format_date(), key=city_name))
        if record is not None:
            message = record.value
        else:
            url = "http://www.weather.com.cn/weather1d/%s.shtml" % city_code
            html = six.moves.urllib.request.urlopen(url).read()
            if html == b"<!-- empty -->":
                return dict(code="fail", message=u("city_code错误"))
            soup = BeautifulSoup(html, "html.parser")
            elements = soup.find_all(id="hidden_title")
            # print(elements)
            # print(len(html))
            # return html
            if len(elements) > 0:
                weather = elements[0]
                message = weather.attrs["value"]
                message = message.replace("/", u("至"))
                db.insert(ctime=xutils.format_datetime(), 
                    cdate=xutils.format_date(),
                    type="weather",
                    key=city_name,
                    value=message
                )

        if message is not None:
            message = u(message)
            if not xconfig.is_mute():
                xutils.say("%s %s" % (city_name, message))
            # six.print_(type(message), message)
            return dict(code="success", data=message)
        else:
            return dict(code="fail", message="结果为空")
Ejemplo n.º 13
0
def by_time(ctx):
    if not xauth.is_admin():
        return None
    period = ctx.groups[0]
    time_str = ctx.groups[1]
    message = ctx.groups[2]
    print(period, time_str, message)
    v = time_pattern.findall(time_str)
    if len(v) > 0:
        tm_hour, tm_min, tm_min2 = v[0]
        tm_hour = int(tm_hour)
        if period == "下午":
            tm_hour += 12
        if tm_min == "半":
            tm_min = 30
        if tm_min2 != None and tm_min2.isdigit():
            tm_min = int(tm_min2)
        if tm_min == "":
            tm_min = 0
        add_alarm(tm_hour, tm_min, message)
        out = "提醒创建成功,将于%s点%s分提醒%s" % (tm_hour, tm_min, message)
        xutils.say(out)
        ctx.tools.append(SearchResult("提醒", "/system/crontab", out))
Ejemplo n.º 14
0
def handle_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--data", default="./data")
    parser.add_argument("--delay", default="0")
    parser.add_argument("--ringtone", default="no")
    parser.add_argument("--port", default=DEFAULT_PORT)
    parser.add_argument("--webbrowser", default="no")
    parser.add_argument("--debug", default="yes")
    parser.add_argument("--minthreads", default="10")
    parser.add_argument("--useCacheSearch", default="no")
    parser.add_argument("--useUrlencode", default="no")
    parser.add_argument("--devMode", default="no")
    parser.add_argument("--initScript", default="init.py")
    parser.add_argument("--master", default="no")
    parser.add_argument("--test", default="no")

    web.config.debug = False
    args = parser.parse_args(sys.argv[1:])

    # 处理Data目录,创建各种目录
    try:
        xconfig.init(args.data)
    except Exception as e:
        xconfig.errors.append("创建目录失败")
        xutils.print_exc()
    # 端口号
    xconfig.PORT = args.port

    # 延迟加载,避免定时任务重复执行
    delay = int(args.delay)
    time.sleep(delay)

    # 启动提醒
    if args.ringtone == "yes":
        xutils.say("系统启动")
    if args.webbrowser == "yes":
        xconfig.OPEN_IN_BROWSER = True
    if args.debug == "yes":
        xconfig.DEBUG = True
        web.config.debug = True
    if args.useCacheSearch == "yes":
        xconfig.USE_CACHE_SEARCH = True
    if args.useUrlencode == "yes":
        xconfig.USE_URLENCODE = True
    if args.devMode == "yes":
        xconfig.DEV_MODE = True
    if args.test == "yes":
        xconfig.IS_TEST = True

    xconfig.MIN_THREADS = int(args.minthreads)
    xconfig.INIT_SCRIPT = args.initScript
    web.config.minthreads = xconfig.MIN_THREADS

    port = xconfig.PORT
    if port != DEFAULT_PORT:
        # 指定端口优先级最高
        os.environ["PORT"] = port

    if not os.environ.get("PORT"):
        os.environ["PORT"] = port

    xconfig.set("port", port)
    xconfig.set("start_time", xutils.format_datetime())