This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsp.pl
143 lines (131 loc) · 4.46 KB
/
wsp.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
#!/usr/bin/perl
#########################################
# Windows Store Parser v1.6 #
# igroykt (c)04.09.2014-23.09.2014 #
#########################################
use strict;
use XML::LibXML qw( );
use File::Copy qw(move);
use File::Copy qw(copy);
use Tie::File;
use MIME::Lite;
use MIME::Base64 ();
use Time::Piece;
use utf8;
use Switch;
binmode(STDOUT,':utf8');
#ENV
my $pwd="/root/bin/wsp";
#RU SETTINGS
my $ru_current=$pwd."/ru_current.txt";
my $ru_previous=$pwd."/ru_previous.txt";
my $ru_tmp=$pwd."/ru_tmp.xml";
my $ru_mail=$pwd."/ru_mail.txt";
my $ru_url="";
#EN SETTINGS
my $en_current=$pwd."/en_current.txt";
my $en_previous=$pwd."/en_previous.txt";
my $en_tmp=$pwd."/en_tmp.xml";
my $en_mail=$pwd."/en_mail.txt";
my $en_url="";
#MAIL SETTINGS
my $to='';
my $from='';
my $ru_subject='Windows Store Reviews [RU]';
my $en_subject='Windows Store Reviews [EN]';
sub trimDate{
my($string)=@_;
$string=substr($string,0,-18);
return $string;
}
sub parseReviews{
my $num_args=scalar(@_);
my $getXML=`lynx -dump -source '$_[2]' > $_[1]`;
my $parser=XML::LibXML->new();
my $root=$parser->parse_file($_[1]);
my @current=();
for my $review ($root->findnodes('Reviews/Review')){
my $review_author=$review->find('Author/OrderedBasicName');
my $review_rating=$review->find('Rating');
my $review_comment=$review->find('Comment');
my $review_date=$review->find('LastUpdatedDate');
push(@current,trimDate($review_date)." Автор: ".$review_author." Рейтинг: ".$review_rating." Комментарий: ".$review_comment."\n");
}
my %seen=();
my @unique=grep{ ! $seen{ $_ }++ } @current;
my $date=localtime->strftime('%Y-%m-%d');
@unique=grep(/$date/, @unique);
open (CFILE,">>:utf8",$_[0]);
foreach(@unique){print CFILE $_;}
close(CFILE);
unlink $_[1];
}
sub normalizeText {
my $num_args=scalar(@_);
my $fileHandler;
my @text=();
open $fileHandler,'<:utf8',$_[0];
while(my $line=<$fileHandler>){
substr($line,0,9)='';
$line=~ s/Автор: /Автор: Не указан /;
$line=~ s/Рейтинг: /Рейтинг: Не указан /;
$line=~ s/Комментарий: /Комментарий: Не указан /;
$line=~ s/Автор/\nАвтор/;
$line=~ s/Рейтинг/\nРейтинг/;
$line=~ s/Комментарий/\nКомментарий/;
push(@text,$line);
}
close $fileHandler;
open $fileHandler,'>:utf8',$_[0];
print {$fileHandler} @text;
close $fileHandler;
}
sub makeUniq {
my $num_args=scalar(@_);
tie my @lines,'Tie::File',$_[0];
my %seen=();
@lines=grep { ! $seen{$_}++ } @lines;
}
sub sendMail {
my $num_args=scalar(@_);
if (-s $_[3]){
my $DIFF=`diff -u $_[2] $_[3] |grep '-'|sed '1,3d'|sed 's/-//g'|grep -v '+'|sed '/Комментарий/G' > $_[1]`;
makeUniq($_[1]);
normalizeText($_[1]);
}
if (-z $_[1]){
unlink $_[1];
}
if (-s $_[1]){
open my $fileHandler,'<',$_[1] or die "Couldn't open $_[1]: $!";
my $text=do {
local $/;
<$fileHandler>
};
close $fileHandler;
my $mail=MIME::Lite->new(
Encoding=> '8bit',
Type => 'text/plain; charset=UTF-8',
From => $from,
To => $to,
Subject => $_[0],
Data => $text
);
$mail->send;
unlink $_[1];
}
move $_[2],$_[3];
}
switch($ARGV[0]){
case "parse"{
&parseReviews($ru_current,$ru_tmp,$ru_url);
&parseReviews($en_current,$en_tmp,$en_url);
}
case "send"{
&sendMail($ru_subject,$ru_mail,$ru_current,$ru_previous);
&sendMail($en_subject,$en_mail,$en_current,$en_previous);
}
else{
print "USAGE: ./wsp.pl [option]\nOPTIONS:\n parse - get reviews\n send - send reviews via email (need smtp relay)\n";
}
}