def test_dates_and_frequencies(self): message = { 'id': 1, 'peer_id': 123456, 'from_id': 123456, 'text': 'Test message', 'random_id': 17, 'important': False } random.seed(17) messages = [] for n in range(10): delta_ts = random.randint(86400, 604800) ts = int(1543611600 + delta_ts * random.choice([1, -1])) date = {'date': ts} messages.append(Message(**{**message, **date})) dates, freq = count_dates_from_messages(messages) self.assertEqual(sorted(freq), [1, 1, 1, 2, 2, 3]) self.assertEqual( set(dates), { dt.date(2018, 11, 24), dt.date(2018, 11, 25), dt.date(2018, 11, 26), dt.date(2018, 11, 28), dt.date(2018, 12, 2), dt.date(2018, 12, 3) })
def messages_get_history(user_id, offset=0, count=20) -> list: """ Получить историю переписки с указанным пользователем :param user_id: идентификатор пользователя, с которым нужно получить историю переписки :param offset: смещение в истории переписки :param count: число сообщений, которое нужно получить """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "offset must be positive integer" assert count >= 0, "number of messages must be positive integer" message_l = [] max_count = 200 query_params = { 'user_id': user_id, 'access_token': VK_CONFIG['access_token'], 'offset': offset, 'count': min(max_count, count), 'v': VK_CONFIG['version'] } url = f"{VK_CONFIG['domain']}/messages.getHistory" while count > 0: response = get(url, params=query_params) count -= min(count, max_count) query_params['offset'] += 200 query_params['count'] = min(count, max_count) message_l += response.json()['response']['items'] time.sleep(0.4) messages = [Message(**message) for message in message_l] return messages
def get_list_of_messages(user_id: int, offset: int = 0, count: int = 20) -> List[Message]: messages = messages_get_history(user_id, offset, count) messages_list = [] for j in range(len(messages)): messages_list.append(Message(**messages[j])) return messages_list
def messages_get_history(user_id, offset=0, count=200): """ Получить историю переписки с указанным пользователем :param user_id: идентификатор пользователя, с которым нужно получить историю переписки :param offset: смещение в истории переписки :param count: число сообщений, которое нужно получить """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "user_id must be positive integer" assert count >= 0, "user_id must be positive integer" messages = [] domain = "https://api.vk.com/method" access_token = config.VK_CONFIG['access_token'] user_id = user_id while count > 200: query_params = { 'domain': domain, 'access_token': access_token, 'user_id': user_id, 'offset': offset, 'count': 200 } query = "{domain}/messages.getHistory?access_token={access_token}&user_id={user_id}&offset={offset}&count={count}&v=5.53".format( **query_params) response = get(query) messages.extend(response.json()['response']['items']) time.sleep(0.5) count -= 200 offset += 200 query_params = { 'domain': domain, 'access_token': access_token, 'user_id': user_id, 'offset': offset, 'count': count } query = "{domain}/messages.getHistory?access_token={access_token}&user_id={user_id}&offset={offset}&count={count}&v=5.53".format( **query_params) response = get(query) messages.extend(response.json()['response']['items']) for num, message in enumerate(messages): message = Message(text=message['body'], date=message['date']) messages[num] = message return messages
def messages_get_history(user_id, offset=0, count=20) -> list: """ Получить историю переписки с указанным пользователем :param user_id: идентификатор пользователя, с которым нужно получить историю переписки :param offset: смещение в истории переписки :param count: число сообщений, которое нужно получить """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "user_id must be positive integer" assert count >= 0, "user_id must be positive integer" list_parms = { 'domain': config.VK_CONFIG['domain'], 'access_token': config.VK_CONFIG['access_token'], 'user_id': user_id, 'offset': offset, 'count': count, 'v': config.VK_CONFIG['version'] } url = "{domain}/messages.getHistory?access_token={access_token}&user_id={user_id}&v={v}".format( **list_parms) response = get(url) count = response.json()['response']['count'] messages = [] while count > 0: url = "{domain}/messages.getHistory?access_token={access_token}&user_id={user_id}&offset={offset}&count={count}&v={v}".format( **list_parms) response = get(url) messages.extend(response.json()['response']["items"]) count -= min(count, 200) list_parms['offset'] += 200 list_parms['count'] = min(count, 200) time.sleep(0.3334) new_messages = [] for message in messages: print(message) new_messages.append(Message(**message)) messages = new_messages return messages
def messages_get_history(user_id, offset=0, count=200): assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "user_id must be positive integer" assert count >= 0, "user_id must be positive integer" messages = [] domain = "https://api.vk.com/method" access_token = config.VK_CONFIG['access_token'] user_id = 313933490 while count > 200: query_params = { 'domain': domain, 'access_token': access_token, 'user_id': user_id, 'offset': offset, 'count': 200 } query = "{domain}/messages.getHistory?access_token={access_token}&user_id={user_id}&offset={offset}&count=" \ "{count}&v=5.53".format(**query_params) response = get(query) messages.extend(response.json()['response']['items']) time.sleep(0.5) count -= 200 offset += 200 query_params = { 'domain': domain, 'access_token': access_token, 'user_id': user_id, 'offset': offset, 'count': count } query = "{domain}/messages.getHistory?access_token={access_token}&user_id={user_id}&offset={offset}&count={count}" \ "&v=5.53".format(**query_params) response = get(query) messages.extend(response.json()['response']['items']) for num, message in enumerate(messages): message = Message(text=message['body'], date=message['date']) messages[num] = message return messages
def messages_get_history(user_id: int, offset: int = 0, count: int = 20) -> List[Message]: """ Get user messages history :param user_id: user ID with which we want to get messages :param offset: offset from last message :param count: count of messages :return: json object with messages """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "user_id must be positive integer" assert count >= 0, "user_id must be positive integer" query_params = { 'access_token': config.VK_CONFIG['access_token'], 'user_id': user_id, 'offset': offset, 'count': min(count, 200), 'v': config.VK_CONFIG['version'] } messages = [] while count > 0: response = get(config.VK_CONFIG['domain'] + "/messages.getHistory", query_params) if response: result = response.json() if 'error' in result: print(result['error']['error_msg']) else: for message in result['response']['items']: messages.append(Message(**message)) count -= min(count, 200) query_params['offset'] += 200 query_params['count'] = min(count, 200) return messages
def messages_get_history(user_id, offset=0, count=300): """ Получить историю переписки с указанным пользователем :param user_id: идентификатор пользователя, с которым нужно получить историю переписки :param offset: смещение в истории переписки :param count: число сообщений, которое нужно получить """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "user_id must be positive integer" assert count >= 0, "user_id must be positive integer" url = config.VK_CONFIG.get('domain') url += "messages.getHistory" access_token = config.VK_CONFIG.get('access_token') version = config.VK_CONFIG.get('version') messages = [] while True: query_params = { 'access_token': access_token, 'user_id': user_id, 'v': version, 'offest': offset, 'count': min(count, 200) } response = get(url, params=query_params) gotHistory = response.json()["response"]["items"] for message in gotHistory: messages.append(Message(**message)) if count < 200: break else: time.sleep(1) count -= 200 return messages
def messages_get_history(user_id: int, offset: int = 0, count: int = 20) -> List: """ Получить историю переписки с указанным пользователем :param user_id: идентификатор пользователя, с которым нужно получить историю переписки :param offset: смещение в истории переписки :param count: число сообщений, которое нужно получить """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" assert isinstance(offset, int), "offset must be positive integer" assert offset >= 0, "offset must be positive integer" assert count >= 0, "count must be positive integer" params = config.VK_CONFIG.copy() params.update({"user_id": user_id, "offset": offset, "count": 200}) url = f"{params['domain']}/messages.getHistory" full_requests_num = count // 200 result = [] for i in range(full_requests_num + 1): if i == full_requests_num: params["count"] = count - params["offset"] response = get(url, params).json() if response.get("response") and (messages := response["response"].get("items")): result.extend(messages) params["offset"] += 200 return [Message(**msg) for msg in result]
from api import messages_get_history from api_models import Message from messages import count_dates_from_messages, plotly_messages_freq messages = messages_get_history(505540783, offset=10, count=50) messages_list = [Message(**mes) for mes in messages] dates_freqs = count_dates_from_messages(messages_list) plotly_messages_freq(dates_freqs[0], dates_freqs[1])
from age import age_predict from api import messages_get_history, get_friends from messages import count_dates_from_messages, plotly_messages_freq from api_models import Message from network import get_network, plot_graph print(age_predict(155393609)) history = messages_get_history(155393609, count=300) mlist = [Message(**mes) for mes in history] dates = count_dates_from_messages(mlist) plotly_messages_freq(dates[0], dates[1]) friends = get_friends(155393609, fields='bdate')['response']['items'] ids = [] names = [] for friend in friends: ids.append(friend['id']) names.append(friend['first_name'] + ' ' + friend['last_name']) edges = get_network(ids) plot_graph(edges, names)