/
txt.pl
165 lines (110 loc) · 2.87 KB
/
txt.pl
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
#!/usr/bin/local/perl
use JSON::Parse qw(parse_json json_file_to_perl);
use Date::Parse;
use List::MoreUtils qw(uniq);
use List::Util qw(shuffle);
use Date::Format;
use Data::Dumper;
my (%usedText, $text);
$text = makeText();
open $write, ">>dump.txt";
print $write Dumper(%usedText);
close $write;
print "$text";
exit;
sub makeText {
#$length = @_[0] + 1;
$length = @_[0] ? @_[0] > 0 : int(rand(2)) + 1;
my @legs = getLegs();
my @templates = (
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, %s, but %s. ",
"Eventually, %s. ",
"But after %s, %s. ".
"%s; %s. ",
"%s, which made us realize %s. ",
"Finally, %s. ",
"Even though %s, %s. ",
"%s -- $s. ",
"Back then, %s. ",
"%s, and then %s. ",
"Meanwhile, %s. ",
"%s, and %s at last. ",
"%s, %s and finally %s. "
);
$text = '';
for ($l = 0; $l < $length; $l++){
$template = $templates[int(rand($#templates))];
my @use = shuffle @legs;
print "Using 1:$use[0] and 2: $use[1]\n";
$usedText{"$use[0]"} = 'y';
$usedText{"$use[1]"} = 'y';
print Dumper(%usedText);
$text .= ucfirst ( sprintf($template, @use));
$text =~ s/\s(\.|\,|\;|\:)/$1/ig;
}
# some problems with quote marks I think
$text =~ s/\'|\"|\`//ig;
return $text;
}
sub getLegs {
# later, get this as a parameter
$string = $pageDate;
# $string = str2time("2013-11-30");
print "Getting text from $pageDate\n";
my @tweets;
my @legs;
%params = (
'q' => '%23tbt+when',
'apikey' => '09C43A9B270A470B8EB8F2946A9369F3', # I don't know how long this one will work, but should be able to switch out later if need
'type' => 'tweet',
'offset' => '0',
'perpage' => '100',
'sort' => 'date',
'maxtime' => $string
#'offset' => int(rand(50)) * 10,
);
$url = 'http://otter.topsy.com/search.js?';
#$nurl = 'http://otter.topsy.com/search.js?q=%23tbt+"remember+when"\&type=tweet\&offset=0\&perpage=10\&maxtime=1391288415\&apikey=09C43A9B270A470B8EB8F2946A9369F3';
foreach (keys %params){
$url .= $_ . "=" . $params{$_} . '\&';
}
$result = `curl $url`;
$data = parse_json($result);
#print $result;
foreach (@{$data->{response}->{list}}){
$twt = $_->{title};
# Some filters:
$twt =~ s/#.+?(\s|$)/it /ig; # replace hashtags with "it"
$twt =~ s/http.+?(\s|$)//ig; # remove links
$twt =~ s/\@.+?(\s|$)/you /ig; # replace mentions with "you"
push(@tweets, $twt);
}
print "Tweets: " . scalar(@tweets) . "\n";
if (scalar(@tweets) == 0){
print "Result: $result";
}
foreach (@tweets){
if (/when\s+?(.+?)([\?\!\.\;\,\:\&\-\"]|$)/ig){
my $leg = $1;
$leg =~ s/[^\w\s\d]//ig;
unless($usedText{"$leg"} eq 'y'){
push (@legs, $leg);
}
}
}
@goodlegs = uniq @legs;
print "Legs: " . scalar(@goodlegs) . "\n";
return @goodlegs;
}