-
Notifications
You must be signed in to change notification settings - Fork 1
/
AITopics.pm
180 lines (159 loc) · 5.02 KB
/
AITopics.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package AITopics;
use strict;
use Exporter;
use JSON -support_by_pp;
use HTTP::Request;
use YAML;
use MIME::Base64;
use Data::Dumper;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw($host upload_file update_node process_nodes get_node_by_alias);
%EXPORT_TAGS = (DEFAULT => [qw(upload_file update_node process_nodes get_node_by_alias)]);
my $host;
my $token;
my $session_name;
my $sessid;
my $config;
my $json = JSON->new->utf8;
$json->allow_singlequote();
sub upload_file {
my $ua = shift;
my $filename = shift;
my $tmpfile = shift;
my $filesize = -s $tmpfile;
local($/) = undef;
open(FILE, $tmpfile) or die "$!";
# use "" as second parameter to prevent linebreaks
my $encoded = encode_base64(<FILE>, "");
close(FILE);
my $json_content = '{"filesize": "'.$filesize.'", "filename": "'.$filename.'", "file": "'.$encoded.'"}';
my $create_req = HTTP::Request->new(POST => "$host/rest/file");
$create_req->content_type('application/json');
$create_req->content($json_content);
my $resp = $ua->request($create_req);
if($resp->is_error) {
print STDERR $resp->message;
return 0;
} else {
my %data = %{$json->decode($ua->{content})};
return $data{'fid'};
}
}
sub update_node {
my $ua = shift;
my $nid = shift;
my %data = @_;
print "Updating $nid ... ";
print Dumper(\%data);
my $json_content = $json->encode(\%data);
my $update_req = HTTP::Request->new(PUT => "$host/rest/node/".$nid);
$update_req->header('X-CSRF-Token', $token);
$update_req->header('session', "$session_name=$sessid");
$update_req->content_type('application/json');
$update_req->content($json_content);
my $resp = $ua->request($update_req);
if($resp->is_error) {
print STDERR $resp->message;
return 0;
} else {
print "Done.\n";
return 1;
}
}
sub get_node_by_alias_edit_link {
# try grabbing the nid from the edit link (sometimes the url
# aliases aren't available from the view, presumably due to a bug
# in views_url_alias module)
my $ua = shift;
my $alias = shift;
print "Trying to grab alias by 'edit' link on node view... ";
$ua->get("$host/$alias");
if($ua->{content} =~ m!<a href="/node/(\d+)/edit">Edit</a>!) {
my $nid = $1;
print "Answer: $nid\n";
return $nid;
} else {
print "Answer: none\n";
return '';
}
}
sub get_node_by_alias {
my $ua = shift;
my $alias = shift;
print "Grabbing nid of alias $alias ... ";
$ua->get("$host/rest/node-by-alias?alias=$alias");
my @json_data = @{$json->decode($ua->{content})};
if($#json_data >= 0) {
my %node = %{$json_data[0]};
if($node{'node_title'} ne '') {
print "Answer: ".$node{'nid'}.".\n";
return $node{'nid'};
} else {
return get_node_by_alias_edit_link($ua, $alias);
}
} else {
return get_node_by_alias_edit_link($ua, $alias);
}
}
sub process_nodes {
my $ua = shift;
my $service = shift;
my $process_func = shift;
my $paginated = shift;
$config = YAML::LoadFile("aitopics.conf");
$host = $config->{host};
print STDERR "Logging in...\n";
my $login_req = HTTP::Request->new(
POST => "$host/rest/user/login");
$login_req->content_type('application/json');
$login_req->content('{"username" : "'.$config->{username}.'",'.
' "password" : "'.$config->{password}.'"}');
my %resp = %{$json->decode($ua->request($login_req)->{'_content'})};
$session_name = $resp{'session_name'};
$sessid = $resp{'sessid'};
my $token_req = HTTP::Request->new(
POST => "$host/services/session/token");
$token_req->content_type('application/json');
$token_req->content('{"name" : "'.$config->{username}.'",'.
' "pass" : "'.$config->{password}.'"}');
my $token_resp = $ua->request($token_req);
$token = $token_resp->decoded_content;
my @nodes;
my $page = 0;
if($#ARGV == 0) { $page = int($ARGV[0]); }
while (1) {
if($paginated) {
print STDERR "Getting page $page\n";
$ua->get("$host/rest/$service&page=$page");
} else {
$ua->get("$host/rest/$service");
}
my @json_data = @{$json->decode($ua->{content})};
if($#json_data >= 0) {
push(@nodes, @json_data);
if($paginated) {
$page++;
} else {
last;
}
} else {
last;
}
}
foreach my $i (0..$#nodes) {
if($i % 100 == 0) { print "\n".($i+1)."/".($#nodes+1)."\n"; }
my %nid = %{$nodes[$i]};
$ua->get("$host/rest/node/".$nid{'nid'});
my %node = %{$json->decode($ua->{content})};
if($process_func->($ua, %node)) {
print "{$node{'nid'}}\n";
} else {
print ".";
}
}
print STDERR "\n";
}
1;