-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterJSONParser.pm
133 lines (102 loc) · 2.25 KB
/
TwitterJSONParser.pm
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package TwitterJSONParser;
use strict;
use JSON::PP;
my $INSTANCE = undef;
sub getInstance
{
$INSTANCE = TwitterJSONParser->new() if ($INSTANCE == undef);
return $INSTANCE;
}
sub new
{
my $self = shift;
return bless
{
'errors' => undef
}
}
sub errors { return defined shift->{'errors'}; }
sub errorMessage
{
my $self = shift;
my $additionalInfo = shift;
my $hash =
{
"error" => $additionalInfo,
"twitter_response" => $self->{'errors'}
};
return encode_json $hash;
}
sub invalidUserErrorJSON
{
my $hash = { "error" => "Invalid username. Please check your spelling." };
return encode_json $hash;
}
sub getTextsFromTweetJSON
{
my $self = shift;
my $jsonString = shift;
my $tweetHash = decode_json $jsonString;
my $statuses = $tweetHash->{'statuses'};
my @texts;
for my $status (@$statuses)
{
push @texts, $status->{'text'};
}
return @texts;
}
sub getIdsFromFollowerJSON
{
my $self = shift;
my $jsonString = shift;
my $followerHash = decode_json $jsonString;
my @ids;
my $errors = $followerHash->{'errors'};
if ($errors) {
$self->{'errors'} = $errors;
}
else {
@ids = @{$followerHash->{'ids'}};
}
return @ids;
}
sub encodeArrayOfTweetsToTiltJSON
{
my @tweets = @_;
my $outputHash = { "tweets" => \@tweets };
my $outputJSONString = encode_json $outputHash;
return $outputJSONString;
}
sub encodeArrayOfFollowersToTiltJSON
{
my @followers = @_;
my $followersHash = { "shared_followers" => \@followers };
my $followersJSONString = encode_json $followersHash;
return $followersJSONString;
}
sub getAccessTokenFromAuthJSON
{
my $self = shift;
my $jsonString = shift;
my $authHash = decode_json $jsonString;
die "Invalid token type" unless ($authHash->{'token_type'} == "bearer");
my $accessToken = $authHash->{'access_token'};
return $accessToken;
}
sub getScreenNamesFromUserJSON
{
my $self = shift;
my $jsonString = shift;
my $userArray = decode_json $jsonString;
my @screenNames;
my $errors = (ref($userArray) eq "HASH") ? $userArray->{'errors'} : undef;
if ($errors) { $self->{'errors'} = $errors; }
else {
for my $user (@$userArray)
{
push @screenNames, $user->{'screen_name'};
}
}
return @screenNames;
}
1;