コード例 #1
0
ファイル: bot.py プロジェクト: cuimuxi/gtalkbot
    def presence_control(self, stanza):
        msg = unicode(stanza.get_from())
        t = stanza.get_type()
        frm = stanza.get_from()
        frm_email = "%s@%s" % (frm.node, frm.domain)
        if frm_email == DAEMONACCOUNT[0]:
            p = Presence(to_jid=frm, stanza_type="subscibe")
            return stanza.make_accept_response(), p
        if t == "subscribe":
            msg += u" has requested presence subscription."
            body = "%s 加入群" % frm.node
            p = Presence(to_jid=frm, stanza_type="subscibe")
            send_all_msg(stanza, body)
            add_member(frm)
        elif t == "subscribed":
            msg += u" has accepted our presence subscription request."
            p = Presence(to_jid=frm, stanza_type="subscibe")
            add_member(frm)
        elif t == "unsubscribe":
            msg += u" has canceled his subscription of our."
            body = "%s 离开群" % frm.node
            send_all_msg(stanza, body)
            del_member(frm)
        elif t == "unsubscribed":
            msg += u" has canceled our subscription of his presence."
            del_member(frm)

        logger.info(msg)

        return stanza.make_accept_response(), p
コード例 #2
0
def readJsonFile(fileList):
    fileDict = {}
    for fileName in fileList:
        filePath = os.path.join(COLLECT_FOLDER, fileName)
        with open(filePath, 'rb') as f:
            logger.info('读取文件 %s' % filePath)
            comic = f.read()
            comic = comic.decode()
            comic = json.loads(comic)
            fileDict[fileName] = comic
    return fileDict
コード例 #3
0
ファイル: cmd.py プロジェクト: cuimuxi/gtalkbot
    def _run_cmd(self, stanza, cmd):
        """获取命令"""
        c, args = self._parse_args(cmd)
        email = get_email(stanza.get_from())
        try:
            logger.info('%s run cmd %s', email, c)
            m =getattr(self, c)(stanza, *args)
        except Exception as e:
            logger.warning('Error %s', e.message)
            m = self._send_cmd_result(stanza, 'error')

        return m
コード例 #4
0
def generateComic(comicIndex):
    for comic in comicIndex:
        comicPage = db.getComic(comic)
        comicStr = 'extLink[' + str(comic['编号']) + '] = \n' + json.dumps(
            comicPage, ensure_ascii=False, indent=2)
        with open(os.path.join(GENERATE_FOLDER,
                               str(comic['编号']) + '.js'),
                  'w',
                  encoding='utf8') as comicFile:
            logger.info('[%s] 创建%s文件' %
                        (comic['名称'], str(comic['编号']) + '.js'))
            comicFile.write(comicStr)
コード例 #5
0
ファイル: line.py プロジェクト: liushuochen/SubwayTraffic
def init_line():
    engine, cursor = db.engine.get_engine()
    sql = """
        create table if not exists subway_line(
        uuid  char(27) not null,
        name  varchar(10) not null,
        primary key(name)
        ) charset utf8
        """
    cursor.execute(sql)
    engine.close()
    logger.info("setup subway line finished.")
    return
コード例 #6
0
ファイル: utilities.py プロジェクト: r0lodex/log-service
def connectionStatus(connection, status="close"):
    for key, conn in connection.items():
        if status == "close":
            conn.commit()
            conn.close()
        else:
            try:
                conn.commit()
                conn.close()
            except database.InterfaceError as error:
                logger.info("Closing connections error: {}".format(error))
            conn.reconnect(attempts=3, delay=0)
        logger.info("Connections on {} {}".format(key, status))
コード例 #7
0
ファイル: station.py プロジェクト: liushuochen/SubwayTraffic
def init_station():
    engine, cursor = db.engine.get_engine()
    sql = """
        create table if not exists station(
        uuid      char(27) not null,
        name      varchar(30),
        next_stop text,
        primary key(name)
        ) charset utf8
        """
    cursor.execute(sql)
    engine.close()
    logger.info("Setup station finished.")
    return
コード例 #8
0
def init_verify_code():
    engine, cursor = db.engine.get_engine()
    sql = """
        create table if not exists verify_code(
        uuid    char(27),
        code    varchar(10) not null,
        operate varchar(20) not null,
        modify  datetime not null,
        primary key(uuid)
        ) charset utf8
        """
    cursor.execute(sql)
    engine.close()
    logger.info("setup verify code finished.")
    return
コード例 #9
0
ファイル: relation.py プロジェクト: liushuochen/SubwayTraffic
def init_relation():
    engine, cursor = db.engine.get_engine()
    sql = """
        create table if not exists entity_relation(
        id      bigint  auto_increment  primary key,
        uuid    char(27) not null,
        parent  char(27),
        child   char(27),
        relation varchar(30) not null
        ) charset utf8
    """
    cursor.execute(sql)
    engine.close()
    logger.info("Set up relation finished.")
    return
コード例 #10
0
ファイル: run.py プロジェクト: cuimuxi/gtalkbot
 def roster_updated(self, item = None):
     nodes = []
     if not item:
         for item in self.roster.get_items():
             jid = item.jid
             nodes.append(jid.node)
     else:
         jid = item.jid
         nodes.append(jid.node)
     ujid = JID(USER)
     if ujid.node not in nodes:
         p = Presence(
             to_jid = ujid,
             stanza_type = 'subscribe')
         self.stream.send(p)
     logger.info(nodes)
コード例 #11
0
def insertToDataBase(fileDict):
    for fileName, value in fileDict.items():
        logger.info('--------------------------------------------')
        logger.info('处理文件 %s' % fileName)
        for comic in value:
            db.updateComic(comic)
        logger.info('移动文件 %s 至 %s' % (fileName, TODAY_FOLDER))
        os.rename(os.path.join(COLLECT_FOLDER, fileName),
                  os.path.join(TODAY_FOLDER, fileName))
        logger.info('--------------------------------------------')
コード例 #12
0
def main():
    assert os.path.isfile(
        db.DATABASE), 'comic下数据库文件comic.db不存在,请双击collect.py来生成数据库并收集数据'
    if not os.path.isdir(GENERATE_FOLDER):
        logger.info('创建文件夹 %s' % GENERATE_FOLDER)
        os.mkdir(GENERATE_FOLDER)
    logger.info('从数据库中获取所有Comic数据')
    comicIndex = db.getIndex()
    logger.info('创建index.js文件')
    generateIndex(comicIndex)
    logger.info('创建generate下的文件')
    generateComic(comicIndex)
コード例 #13
0
def init_user():
    engine, cursor = db.engine.get_engine()
    sql = """
        create table if not exists user(
        uuid        char(27) not null,
        email       varchar(30),
        username    varchar(24) default "subway user",
        password    varchar(18) not null,
        token       char(10) not null,
        user_type   enum("admin", "user") not null default "user",
        create_time datetime not null,
        status      enum("active", "down", "lock") not null default "active",
        image       varchar(100),
        primary key(email)
        ) charset utf8
        """
    cursor.execute(sql)
    logger.info("Setup user finished.")

    # add admin user
    conf_path = util.get_root_path() + "/conf/platform.conf"
    deploy_conf = ConfigParser()
    deploy_conf.read([conf_path])
    uuid = util.generate_uuid()
    admin_user = deploy_conf.get("deploy", "admin_user")
    admin_pwd = deploy_conf.get("deploy", "admin_pwd")
    email = deploy_conf.get("deploy", "admin_email")
    image = "/root/image/localhost/default.jpeg"
    try:
        get_user_detail(email)
    except DBError:
        now = util.get_time_string_format()
        token = util.general_token()
        user_type = "admin"
        sql = "insert into user " \
              "values(%s, %s, %s, %s, %s, %s, %s, default, %s)"
        val = (uuid, email, admin_user, admin_pwd, token, user_type, now,
               image)
        cursor.execute(sql, val)
        logger.info("Init default admin user success.")
    engine.commit()
    engine.close()
    return
コード例 #14
0
ファイル: utilities.py プロジェクト: r0lodex/log-service
def archiveLogClick(data, cursor, column_data, table_into, batch_count):
    try:
        total_rows = len(data)
        if total_rows > 0:
            cols = ["`%s`" % column[0] for column in column_data]
            string_literals = ','.join(['%s'] * (len(cols)))
            columns = ",".join(cols)
            query = "INSERT INTO %s (%s) VALUES (%s)" % (table_into, columns,
                                                         string_literals)
            logger.info("Number of rows to migrate: {}".format(total_rows))
            interval = 3
            ids = []
            errorsid = []
            try:
                logger.warning("Insert operations is running, please wait...")
                seq = xrange(total_rows)
                for batched in batch(seq, int(batch_count)):
                    for item in batched:
                        ids.append(data[item]
                                   [0])  # Appending duplicate ids for deletion
                        try:
                            cursor.execute(query, data[item])
                            # logger.info("LogClick ID inserted into {}: {}".format(table_into, data[item][0]))
                        except database.IntegrityError as error:
                            total_rows = total_rows - 1
                            # logger.error("{}, continuing...".format(error))
                            errorsid.append(data[item][0])
                            pass
                    logger.info(
                        "Batch {} completed...continuing".format(batched))
                cursor.execute("select database()")
                db_name = cursor.fetchone()[0]
                if len(errorsid) > 1:
                    logger.warning(
                        "{} duplicates and will be removed from origin".format(
                            len(errorsid)))
                logger.info(
                    "{} records successfully inserted into {}.{}.".format(
                        total_rows, db_name, table_into))
            except IndexError:
                logger.warning("End of insert, continuing...")
            except:
                logger.warning("Something wrong in utilities...")
                exit()
            return ids
        else:
            logger.info("Nothing to update.")
            return []
    except database.ProgrammingError as error:
        logger.error("Error {}".format(error))
コード例 #15
0
ファイル: bot.py プロジェクト: cuimuxi/gtalkbot
    def presence(self, stanza):
        frm = stanza.get_from()
        msg = u"%s has become " % (frm)
        t = stanza.get_type()
        status = stanza.get_status()
        show = stanza.get_show()
        frm_email = "%s@%s" % (frm.node, frm.domain)
        if frm_email != DAEMONACCOUNT[0]:
            if t == "unavailable":
                msg += u"unavailable"
                change_status(frm, 0, show)
            else:
                msg += u"available"
                if not get_member(frm_email) and frm_email != USER:
                    add_member(frm)
                change_status(frm, 1, show)
            if show:
                msg += u"(%s)" % (show,)

            if status:
                msg += u": " + status
            logger.info(msg)
コード例 #16
0
ファイル: utilities.py プロジェクト: r0lodex/log-service
def purgeOrigin(cursor, item_ids):
    # Delete rows with inserted ids in ARCHIVE
    logger.info("Deleting archived records...")

    seq = xrange(len(item_ids))
    try:
        for batched in batch(seq, 150):
            for d in batched:
                iid = item_ids[d]
                delquery = "DELETE FROM %s WHERE id = %s" % (ORIGIN_TABLE, iid)
                cursor.execute(delquery)
                # logger.info("Deleted {}".format(iid))
        cursor.execute("select database()")
        db_name = cursor.fetchone()[0]
        logger.info("{} rows with has been deleted from {}.{}".format(
            len(item_ids), db_name, ORIGIN_TABLE))
    except database.OperationalError as error:
        logger.warning("Error deleting: {}".format(error))
    except database.ProgrammingError as error:
        logger.warning("Error: {}".format(error))
    except database.InterfaceError as error:
        logger.warning("Interface Error: {}".format(error))
コード例 #17
0
ファイル: run.py プロジェクト: cuimuxi/gtalkbot
 def __del__(self):
     if self.PID > 0:
         logger.info("Stop Bot..")
         os.kill(self.PID, 9)
         logger.info("Done")
     logger.info("Stop Bot Daemon")
コード例 #18
0
ファイル: run.py プロジェクト: cuimuxi/gtalkbot
#--- Fork Daemon Process ----------------------------------------------
try:
    pid = os.fork()
    if pid > 0:
        sys.exit(0)
except OSError, e:
    logger.error("Fork #1 failed: %d (%s)", e.errno, e.strerror)
    sys.exit(1)

os.setsid()
os.umask(0)

try:
    pid = os.fork()
    if pid > 0:
        logger.info("Daemon PID %d" , pid)
        pf = open(PIDPATH, 'w')
        pf.write(str(pid))
        pf.close()
        sys.exit(0)
except OSError, e:
    logger.error("Daemon started failed: %d (%s)", e.errno, e.strerror)
    os.exit(1)

#--- Define Daemon ----------------------------------------------
class Daemon():
    PID = 0
    def __init__(self):
        pass

    def run(self):
コード例 #19
0
def main():
    if not os.path.isfile(db.DATABASE):
        logger.info('创建数据库 %s' % db.DATABASE)
        db.initDatabase()
    assert os.path.isdir(COLLECT_FOLDER), 'comic下不存在collect文件夹'
    if not os.path.isdir(TODAY_FOLDER):
        logger.info('创建文件夹 %s' % TODAY_FOLDER)
        os.mkdir(TODAY_FOLDER)
    fileList = list(filter(lambda x: x != 'done', os.listdir(COLLECT_FOLDER)))
    logger.info('将处理以下文件 %s' % str(fileList))
    fileDict = readJsonFile(fileList)
    logger.info('插入数据到数据库')
    insertToDataBase(fileDict)
    logger.info('结束')
    if len(os.listdir(TODAY_FOLDER)) == 0:
        logger.info('删除空文件夹 %s' % TODAY_FOLDER)
        os.rmdir(TODAY_FOLDER)
コード例 #20
0
ファイル: bot.py プロジェクト: cuimuxi/gtalkbot
 def stream_state_changed(self, state, arg):
     logger.info("*** State changed: %s %r ***" % (state, arg))
コード例 #21
0
ファイル: bot.py プロジェクト: cuimuxi/gtalkbot
    def message(self, stanza):
        body = stanza.get_body()
        subject = stanza.get_subject()
        t = stanza.get_type()
        logger.info(u"Message from %s received." % (unicode(stanza.get_from())))
        if subject:
            logger.info(u'Subject: "%s".' % (subject,))
        if t:
            logger.info(u'Type: "%s".' % (t,))
        else:
            logger.info(u'Type: "normal".')
        if stanza.get_type() == "headline":
            # 'headline' messages should never be replied to
            return True
        if subject:
            subject = u"Re: " + subject
        if not body:
            logger.info(u'Body: "%s".' % (body,))
            return
        if body.startswith("$"):
            m = send_command(stanza, body)
        else:
            m = send_all_msg(stanza, body)
        if not DEBUG:
            return m
        logger.info("message %s", m)
        if isinstance(m, list):
            for i in m:
                logger.info("message to %s", i.get_to())
                logger.info("message type %s", i.get_type())
                logger.info("message body %s", i.get_body())
        else:
            logger.info("message to %s", m.get_to())
            logger.info("message body %s", m.get_body())
            logger.info("message type %s", m.get_type())

        return m
コード例 #22
0
@author: Irfan Radzi
@organization: Hermo Creative Sdn Bhd
@created: 04-09-2018
"""

import datetime as DT
from db import logger, connection, ORIGIN, ORIGIN_TABLE, ARCHIVE, ARCHIVE_TABLE, TIMEDELTA, BATCH
from utilities import archiveLogClick, purgeOrigin, connectionStatus
import mysql.connector as database

today = DT.date.today()
batch = BATCH

date_condition = today - DT.timedelta(days=int(TIMEDELTA))
logger.info(
    "Data will be selected from {} onwards ({} days) from table {}.".format(
        date_condition, TIMEDELTA, ORIGIN_TABLE))

try:
    # Get data
    ORIGIN.execute("select * FROM log_clicks WHERE created >= '{0!s}'".format(
        date_condition))
    data = ORIGIN.fetchall()

    connectionStatus(connection, "open")
    ORIGIN.execute("desc {}".format(ORIGIN_TABLE))
    origin_columns = ORIGIN.fetchall()
    # origin_ids = archiveLogClick(data, ORIGIN,origin_columns, ARCHIVE_TABLE, batch) # For the existing DB

    # if len(origin_ids) > 0:
    # logger.warning('%s rows successfully added into origin' % len(origin_ids))
コード例 #23
0
ファイル: bot.py プロジェクト: cuimuxi/gtalkbot
    def roster_updated(self, item=None):
        if not item:
            print u"My roster:"
            for item in self.roster.get_items():
                self.print_roster_item(item)
            return
        print u"Roster item updated:"
        self.print_roster_item(item)


encoding = "utf-8"
sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors="replace")
sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors="replace")


logger.info(u"creating client...")

c = Client(JID(USER), PASSWORD)


logger.info(u"connecting...")
c.connect()

logger.info(u"looping...")
c.loop(None)
try:
    # Component class provides basic "main loop" for the applitation
    # Though, most applications would need to have their own loop and call
    # component.stream.loop_iter() from it whenever an event on
    # component.stream.fileno() occurs.
    pass