about summary refs log tree commit diff stats
path: root/src/tweet.cpp
blob: bf95bc0899d4b77f543a94da1522023ba467a7e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "tweet.h"
#include <json.hpp>
#include <cassert>

using nlohmann::json;

namespace twitter {
  
  tweet::tweet() : _valid(false)
  {
    
  }
  
  tweet::tweet(std::string data) : _valid(true)
  {
    auto _data = json::parse(data);
    _id = _data.at("id");
    _text = _data.at("text");
    _author = user(_data.at("user").dump());
    _retweeted = _data.at("retweeted");
    
    if (_data.find("entities") != _data.end())
    {
      auto _entities = _data.at("entities");
      if (_entities.find("user_mentions") != _entities.end())
      {
        for (auto _mention : _entities.at("user_mentions"))
        {
          _mentions.push_back(std::make_pair(_mention.at("id"), _mention.at("screen_name").get<std::string>()));
        }
      }
    }
  }
  
  tweet_id tweet::getID() const
  {
    assert(_valid);
    
    return _id;
  }
  
  std::string tweet::getText() const
  {
    assert(_valid);
    
    return _text;
  }
  
  const user& tweet::getAuthor() const
  {
    assert(_valid);
    
    return _author;
  }
  
  bool tweet::isRetweet() const
  {
    assert(_valid);
    
    return _retweeted;
  }
  
  std::vector<std::pair<user_id, std::string>> tweet::getMentions() const
  {
    return _mentions;
  }
  
  tweet::operator bool() const
  {
    return _valid;
  }
  
};