Exemple #1
0
    def update(self):
        new_domain_names = self._get_domain_name_list()

        for domain in self.domains:
            if domain.name not in new_domain_names:
                self.domains.remove(domain)
                self.domain_names.remove(domain.name)

        for name in new_domain_names:
            if name not in self.domain_names:
                self.domain_names.append(name)
                self.domains.append(Domain(name, self.path))

        domain_updated = False
        for domain in self.domains:
            if domain.update_ip():
                print('%s: IP of %s changed to %s' %
                      (timestamp2str(time.time()), domain.name, domain.ip))
                domain_updated = True

        if domain_updated:
            self._write_hosts()
            return True
        else:
            return False
Exemple #2
0
def get_busline_realtime_info(busline, site):
    check_update(b)
    if not site.isdigit():
        site = get_site_id_by_name(busline, site)
        if not site:
            return '请使用正确地ID或者站点名字, 查询请使用类似`公交 busline`'
    realtime_infos = b.get_busline_realtime_info(busline, site)

    return '\n'.join([
        (u'*车次{0}: 下站: {1} 离下一站的距离: {2}米, 预计到达下一站的时间: {3}\n'
         u'离本站的距离: {4}米, 预计到达本站的时间: {5}').format(
             index, r['ns'], r['nsd'] if r['nsd'] != '-1' else u'进站中',
             timestamp2str(r['nst']), r['sd'],
             timestamp2str(r['st']) if r['st'] != '-1' else u'未知')
        for index, r in enumerate(realtime_infos, 1)
        if r.values().count('-1') < 3
    ])
Exemple #3
0
def get_busline_realtime_info(busline, site):
    check_update(b)
    id = b.query_busline_id_by_name(busline)
    if not site.isdigit():
        site = get_site_id_by_name(busline, site)
        if not site:
            return '请使用正确地ID或者站点名字, 查询请使用类似`公交 busline`'
    realtime_infos = b.get_busline_realtime_info(id, site)

    return '\n'.join([
        (u'*车次{0}: 下站: {1} 离下一站的距离: {2}米, 预计到达下一站的时间: {3}\n'
         u'离本站的距离: {4}米, 预计到达本站的时间: {5}').format(
             index, r['ns'], r['nsd'] if r['nsd'] != '-1' else u'进站中',
             timestamp2str(r['nst']), r['sd'],
             timestamp2str(r['st']) if r['st'] != '-1' else u'未知')
        for index, r in enumerate(realtime_infos, 1)
        if r.values().count('-1') < 3
    ])
Exemple #4
0
def download_files(msg):
    """
    下载多媒体附件
    """
    filename = '_'.join([msg['User']['RemarkName'] or msg['User']['NickName'], ut.timestamp2str(msg['CreateTime']), msg['FileName']])
    full_filename = os.path.join(st.downloads_dir, filename)
    msg['Text'](full_filename)

    itchat.send('@%s@%s' % ('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
    itchat.send(st.msgs['received'], msg['FromUserName'])
Exemple #5
0
def disp_online_info(online_info):
    log('Online status:           ' + online_info['online'])
    if online_info['online'] == 'Online':
        log('Online user:             '******'username'])
        log('Time of login:           '******'login_timestamp']))
        log('Online time:             ' +
            sec2human(online_info['online_time']))
        log('Monthly online time:     ' +
            sec2human(online_info['monthly_online_time']))
        log('Online IP:               ' + online_info['ip'])
        log('IPv4 uasge:              ' + bytes2human(online_info['bytes']))
        log('Account balance:         ' + str(online_info['balance']))
Exemple #6
0
def get_busline_realtime_info(busline, site):
    check_update(b)
    if not site.isdigit():
        site = get_site_id_by_name(busline, site)
        if not site:
            return "请使用正确地ID或者站点名字, 查询请使用类似`公交 busline`"
    realtime_infos = b.get_busline_realtime_info(busline, site)

    return "\n".join(
        [
            (u"*车次{0}: 下站: {1} 离下一站的距离: {2}米, 预计到达下一站的时间: {3}\n" u"离本站的距离: {4}米, 预计到达本站的时间: {5}").format(
                index,
                r["ns"],
                r["nsd"] if r["nsd"] != "-1" else u"进站中",
                timestamp2str(r["nst"]),
                r["sd"],
                timestamp2str(r["st"]) if r["st"] != "-1" else u"未知",
            )
            for index, r in enumerate(realtime_infos, 1)
            if r.values().count("-1") < 3
        ]
    )
Exemple #7
0
    def _write_hosts(self):
        lines = []
        with open('/etc/hosts', 'r') as hosts:
            lines = hosts.readlines()
            for domain in self.domains:
                matched = False
                for i in range(len(lines)):
                    domain_name_reg = '\\s+' + \
                        domain.name.replace(
                            '-', '\\-').replace('_', '\\_').replace('.', '\\.')
                    if re.search(domain_name_reg, lines[i],
                                 re.RegexFlag.IGNORECASE):
                        lines[i] = re.sub(r'\d+\.\d+\.\d+\.\d+', domain.ip,
                                          lines[i])
                        matched = True
                if not matched:
                    lines.append('%s %s\n' % (domain.ip, domain.name))

        with open('/etc/hosts', 'w') as hosts:
            print('%s: writing hosts' % timestamp2str(time.time()))
            hosts.writelines(lines)
Exemple #8
0
if __name__ == '__main__':
    conf = {}
    with open('client.conf', 'r') as fp:
        conf = json.load(fp)

    if conf['daemon'].lower() == 'true':
        daemonize(stdout='/tmp/ddns_client.log',
                  stderr='/tmp/ddns_client_err.log')

    domain = conf['domain']
    ddns_host = conf['ddns_host']
    ssh_user = conf['ssh_user']
    ssh_port = conf['ssh_port']
    ssh_password = conf['ssh_password']
    id_rsa = conf['id_rsa']
    remote_dir = conf['remote_dir']
    interval = conf['interval']

    last_ip = None

    while True:
        new_ip = get_ip()
        while last_ip == new_ip:
            new_ip = get_ip()
            time.sleep(interval)
        print('%s: IP changed from %s to %s' %
              (timestamp2str(time.time()), last_ip, new_ip))
        send_ip(new_ip, domain, ddns_host, ssh_user, ssh_port, ssh_password,
                id_rsa, remote_dir)
        last_ip = new_ip
Exemple #9
0
 def get_end_date(self):
     """ Return first datafeed datetime
     """
     dt = self.stratbt.data._dataname['open'].index
     return timestamp2str(dt[-1])
Exemple #10
0
def update_dnsmasq():
    for pid in psutil.pids():
        if psutil.pid_exists(pid) and psutil.Process(
                pid).username() == 'dnsmasq':
            os.kill(pid, signal.SIGHUP)
            print('%s: dnsmasq updated' % timestamp2str(time.time()))
 def get_end_date(self):
     """ Return first datafeed datetime
     """
     dt = self.get_date_index()
     return timestamp2str(dt[-1])