Пример #1
0
def process(users):
    global vk_access_token

    cur = datetime.now()
    index = cur.hour * 60 + cur.minute

    ids = []

    for user in users:
        id = set_id_if_needed(user)
        if id > 0:
            ids.append(id)

    ids_str = ','.join(map(str, ids))

    time.sleep(1)

    results = requests.get(METHODS_URL + 'users.get', {'user_ids': ids_str, 'fields': 'online_mobile,sex,online'}).json()[
        'response']

    for i in range(len(results)):
        res = results[i]
        user = VkList.get_user_by_id(res['uid'])
        if user is None:
            continue

        online = 'offline'
        if res['online'] == 1:
            online = 'online'
        if 'online_mobile' in res and res['online_mobile'] == 1:
            online = 'online_mobile'

        user.times[index] = activity_helper.activity_to_int(online)
        user.name = res['first_name'] + ' ' + res['last_name']
Пример #2
0
def save(force: bool = False) -> bool:
    try:
        global process_save_timeout

        if not force and not process_save_timeout.will_process():
            return

        if force:
            process_save_timeout.force_update()

        server_info.bd_saves += 1
        server_info.bd_last_save_time=datetime.now()
        users = VkList.get_users()
        for user in users:

            if not _has_any_time(user.times):
                continue

            f = open(_get_file_path(user.user_file), 'a')

            cur_time = datetime.now()

            f.write(str(cur_time.year) + '-' + str(cur_time.month) + '-' + str(cur_time.day) + ':')

            start_index = index_of_start(user.times)
            end_index = index_of_end(user.times)

            f.write(str(start_index) + '-' + str(end_index) + ':')

            if start_index >= 0 and end_index >= 0:
                for i in range(start_index, end_index + 1):
                    f.write(str(user.times[i]))
            else:
                for t in user.times:
                    f.write(str(t))
            f.write(os.linesep)
            f.close()
            user.clear_times()

        return True
    except:
        print('error vkSpy save')
        return False
Пример #3
0
def process(force=False):
    try:
        global user_directory, process_timeout, cur_date

        cur = datetime.now()
        users = VkList.get_users()
        if cur_date.day != cur.day:
            cur_date = cur
            for user in users:
                user.clear_times()

        if not force and not process_timeout.will_process():
            return

        _prepare_if_needed()
        VkParser.process(users)
        save()

        return
    except Exception as e:
        print('error VkSpy process'+str(e))
Пример #4
0
def run_command(chat_id, text):
    try:
        params = text
        params = params.replace('_', ' ')
        params = params.split(' ')
        params[0] = params[0].replace('/', '')

        tmp = []
        for t in params:
            tmp.append(t.lower())

        params = tmp;

        params_lens = len(params)

        if params_lens >= 1 and params[0] == 'vk':
            if params_lens >= 3:
                if params[1] == 'add':
                    VkList.add_user(params[2])
                    return
                if params[1] == 'image':

                    if params_lens >= 4:
                        try:
                            date_str = params[3].split('-')
                            # todo
                        except:
                            send_text(chat_id, 'error date processing')
                            return

                    users = VkList.get_users()
                    if params[2] == 'all':
                        for user in users:
                            img = VkSpy.get_image(user)

                            tmp = 'tmp.jpeg'

                            img.save(tmp, 'JPEG')
                            send_image(chat_id, tmp)
                        return

                    index = int(params[2])

                    users = VkList.get_users()

                    if len(users) <= index:
                        send_text(chat_id, 'out of range')
                        return

                    img = VkSpy.get_image(users[index])

                    tmp = 'tmp.jpeg'

                    img.save(tmp, 'JPEG')
                    send_image(chat_id, tmp)

                    return
            if params_lens >= 2:
                if params[1] == 'list':
                    users = VkList.get_users()
                    us = []
                    for u in range(0, len(users)):
                        us.append(str(u) + ' - ' + str(users[u].url) + ' - ' + users[u].name)
                    sep = os.linesep
                    res = sep.join(us)
                    send_text(chat_id, res)
                    return

        if params_lens >= 1 and params[0] == 'status':
            send_text(chat_id, server_info.get_uptime())
            return

        if params_lens >= 2 and params[0] == 'force' and params[1] == 'save':
            if VkSpy.save(True):
                send_text(chat_id, 'save forced ok')
            else:
                send_text(chat_id, 'save forced error')
            return
        if params_lens >= 2 and params[0] == 'force' and params[1] == 'process':
            VkSpy.process(True)
            send_text(chat_id, 'process forced')
            return

        send_text(chat_id, 'unknown command')

    except Exception as e:
        send_text(chat_id, 'error command processing - ' + str(e))