add solution for nbcontest, progra, n°8
This commit is contained in:
parent
d672a4f41b
commit
e38775674e
1 changed files with 176 additions and 0 deletions
176
newbiecontest/prog/lecture_learning/lecture.pl
Executable file
176
newbiecontest/prog/lecture_learning/lecture.pl
Executable file
|
@ -0,0 +1,176 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
# uses #########################################################################
|
||||
use diagnostics;
|
||||
use warnings;
|
||||
use strict;
|
||||
# Modules
|
||||
# database
|
||||
use DBI;
|
||||
# http
|
||||
use LWP::UserAgent;
|
||||
use HTTP::Cookies;
|
||||
|
||||
# subs #########################################################################
|
||||
# extract cookie from SQLite db
|
||||
sub extract_cookie
|
||||
{
|
||||
# shift db_path
|
||||
my $db_path = shift;
|
||||
|
||||
# variables usefull for database connection and handler
|
||||
my $dbh;
|
||||
my $sth;
|
||||
my $rv;
|
||||
my @row;
|
||||
|
||||
# string containing the query
|
||||
my $query;
|
||||
|
||||
# connect to the db
|
||||
$dbh = DBI->connect("dbi:SQLite:dbname=$db_path","","") or die $DBI::errstr;
|
||||
|
||||
# prepare the query
|
||||
$query = "SELECT name, value FROM moz_cookies WHERE host LIKE\
|
||||
'%newbiecontest.org%' AND name LIKE '%SMFCookie%'";
|
||||
$sth = $dbh->prepare($query);
|
||||
|
||||
# execute
|
||||
$rv = $sth->execute() or die $DBI::errstr;
|
||||
|
||||
# fetch the only row found
|
||||
@row = $sth->fetchrow_array();
|
||||
|
||||
# close the database
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
|
||||
return $row[0], $row[1];
|
||||
|
||||
}
|
||||
|
||||
# Send http request
|
||||
sub send_request
|
||||
{
|
||||
|
||||
# shift args
|
||||
my ($cookie_name, $cookie_val, $url) = @_;
|
||||
|
||||
# create variables
|
||||
my $ua = LWP::UserAgent->new;
|
||||
my $cookies = HTTP::Cookies->new();
|
||||
|
||||
# create the cookie
|
||||
$cookies->set_cookie(0, $cookie_name, $cookie_val, '/', 'newbiecontest.org', 80, 0, 0,\
|
||||
0, 0);
|
||||
|
||||
# add the cookie to the jar
|
||||
$ua->cookie_jar($cookies);
|
||||
|
||||
# send request
|
||||
my $response = $ua->get($url);
|
||||
|
||||
return $response->decoded_content;
|
||||
|
||||
}
|
||||
|
||||
# Write img to file
|
||||
sub write_into_file
|
||||
{
|
||||
|
||||
my $img = shift;
|
||||
my $filename = shift;
|
||||
|
||||
# open
|
||||
open(my $file, '>', $filename) or die("[X] File not found");
|
||||
|
||||
# write
|
||||
print $file $img;
|
||||
|
||||
# close
|
||||
close $file;
|
||||
|
||||
}
|
||||
|
||||
# Extract text using tesseract ocr
|
||||
sub extract_text
|
||||
{
|
||||
my $result;
|
||||
my $filename = shift;
|
||||
|
||||
system("convert", $filename, "-resize", "300%", $filename);
|
||||
|
||||
system("convert", $filename, "-negate", "/tmp/negate.bpm");
|
||||
|
||||
system("tesseract", "/tmp/negate.bpm", "/tmp/text");
|
||||
|
||||
}
|
||||
|
||||
sub read_text
|
||||
{
|
||||
# open file
|
||||
open(my $file, '<', "/tmp/text.txt") or die("[X] File not found");
|
||||
|
||||
# read the first line
|
||||
my $line = <$file>;
|
||||
|
||||
return $line;
|
||||
|
||||
}
|
||||
|
||||
# send answer
|
||||
sub send_answer
|
||||
{
|
||||
my ($cookie_name, $cookie_val, $url, $result) = @_;
|
||||
|
||||
# create variables
|
||||
my $ua = LWP::UserAgent->new;
|
||||
my $cookies = HTTP::Cookies->new();
|
||||
|
||||
# modify the url
|
||||
$url = $url . "?chaine=" . $result;
|
||||
|
||||
# create the cookie
|
||||
$cookies->set_cookie(0, $cookie_name, $cookie_val, '/', 'newbiecontest.org', 80, 0, 0,\
|
||||
0, 0);
|
||||
|
||||
# add the cookie to the jar
|
||||
$ua->cookie_jar($cookies);
|
||||
|
||||
# send request
|
||||
my $response = $ua->get($url);
|
||||
|
||||
print $response->decoded_content;
|
||||
}
|
||||
|
||||
# main #########################################################################
|
||||
# check argument number
|
||||
if (@ARGV != 4) {
|
||||
|
||||
print("Get. The. Fuck. Out\n");
|
||||
print("[i] Usage : ./lecture.pl cookie.db url_in url_out filename_img\n");
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
my $cookie_name;
|
||||
my $cookie_val;
|
||||
my $img;
|
||||
my $result;
|
||||
|
||||
($cookie_name, $cookie_val) = extract_cookie($ARGV[0]);
|
||||
|
||||
$img = send_request($cookie_name, $cookie_val, $ARGV[1]);
|
||||
|
||||
write_into_file($img, $ARGV[3]);
|
||||
|
||||
extract_text($ARGV[3]);
|
||||
|
||||
$result = read_text();
|
||||
|
||||
print $result;
|
||||
|
||||
send_answer($cookie_name, $cookie_val, $ARGV[2], $result);
|
||||
|
||||
print("Done.\n");
|
||||
|
Loading…
Reference in a new issue