Perl PasteBin scraper











up vote
3
down vote

favorite












I'd like to optimize my code and get a better of understanding of how I can perform the task I am doing better. I've only used Perl threads about 3 or 4 times now.



The purpose of my script / code block is to scrape the Pastebin API for every new public paste that appears at regular intervals using a crontab. Then search inside those new pastes for user defined regular expressions such as "google.com." The paste is then saved to a sqlite DB for later viewing and a notification is sent to a listening nodejs bot.



That being said here is my code block I wish to have improved upon:



sub threadCheckKey {
my ($url, $key, @regexs) = @_;
my $fullURL = $url.$key;
my @flaggedRegex = ();
my $date = strftime "%D", localtime;
my @data = ();

my $thread = threads->create(sub {
my $dbConnection = openDB();
open(GET_DATA, "-|", "curl -s " . $fullURL . " -k") or die("$!");
open(WRITE_FILE, ">", $key . ".txt") or die("$!");
while(my $line = <GET_DATA>) {
print WRITE_FILE $line;
foreach my $regex(@regexs) {
if($line =~ m/$regex/) {
if(!(grep(/$regex/, @flaggedRegex))) {
push(@flaggedRegex, $regex);
}
}
}
}
close(WRITE_FILE);
close(GET_DATA);

my $flaggedSize = @flaggedRegex;
if($flaggedSize == 0) {
#Uncomment below for debugging.
#print "No user defined regex foundn";
return;
}

open(READ_FILE, "<", $key . ".txt") or die("$!");
while(my $line = <READ_FILE>) {
push(@data, $line);
}
close(READ_FILE);

my $inputData = join("r", @data);
my $flaggedReg = join(" | ", @flaggedRegex);
my $updateRow = qq(UPDATE $tables[0] set data = ?, date = ?, regex = ? where pastekey = ?);
my $updateRowPrepare = $dbConnection->prepare($updateRow);
my $executeRowUpdate = $updateRowPrepare->execute($inputData, $date, $flaggedReg, $key);

if($executeRowUpdate < 0) {
print $DBI::errstr;
}

my $msg = qq(**Alert:** New paste found! The paste can be viewed via https://pastebin.com/$key - Regex Trigger: $flaggedReg);

open(SEND_ALERT, "-|", "curl https://localhost:8051/bots/bot_service_id/alert -k --data '{"text":"$msg"}' -H "Content-Type: application/json" -X POST") or die("$!");
close(SEND_ALERT);

$dbConnection->disconnect();
});

if($thread->is_running()) {
sleep(2);
}

if($thread->is_joinable()) {
$thread->join();
}

unlink $key . ".txt";
return;
}









share|improve this question









New contributor




falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    I'd like to optimize my code and get a better of understanding of how I can perform the task I am doing better. I've only used Perl threads about 3 or 4 times now.



    The purpose of my script / code block is to scrape the Pastebin API for every new public paste that appears at regular intervals using a crontab. Then search inside those new pastes for user defined regular expressions such as "google.com." The paste is then saved to a sqlite DB for later viewing and a notification is sent to a listening nodejs bot.



    That being said here is my code block I wish to have improved upon:



    sub threadCheckKey {
    my ($url, $key, @regexs) = @_;
    my $fullURL = $url.$key;
    my @flaggedRegex = ();
    my $date = strftime "%D", localtime;
    my @data = ();

    my $thread = threads->create(sub {
    my $dbConnection = openDB();
    open(GET_DATA, "-|", "curl -s " . $fullURL . " -k") or die("$!");
    open(WRITE_FILE, ">", $key . ".txt") or die("$!");
    while(my $line = <GET_DATA>) {
    print WRITE_FILE $line;
    foreach my $regex(@regexs) {
    if($line =~ m/$regex/) {
    if(!(grep(/$regex/, @flaggedRegex))) {
    push(@flaggedRegex, $regex);
    }
    }
    }
    }
    close(WRITE_FILE);
    close(GET_DATA);

    my $flaggedSize = @flaggedRegex;
    if($flaggedSize == 0) {
    #Uncomment below for debugging.
    #print "No user defined regex foundn";
    return;
    }

    open(READ_FILE, "<", $key . ".txt") or die("$!");
    while(my $line = <READ_FILE>) {
    push(@data, $line);
    }
    close(READ_FILE);

    my $inputData = join("r", @data);
    my $flaggedReg = join(" | ", @flaggedRegex);
    my $updateRow = qq(UPDATE $tables[0] set data = ?, date = ?, regex = ? where pastekey = ?);
    my $updateRowPrepare = $dbConnection->prepare($updateRow);
    my $executeRowUpdate = $updateRowPrepare->execute($inputData, $date, $flaggedReg, $key);

    if($executeRowUpdate < 0) {
    print $DBI::errstr;
    }

    my $msg = qq(**Alert:** New paste found! The paste can be viewed via https://pastebin.com/$key - Regex Trigger: $flaggedReg);

    open(SEND_ALERT, "-|", "curl https://localhost:8051/bots/bot_service_id/alert -k --data '{"text":"$msg"}' -H "Content-Type: application/json" -X POST") or die("$!");
    close(SEND_ALERT);

    $dbConnection->disconnect();
    });

    if($thread->is_running()) {
    sleep(2);
    }

    if($thread->is_joinable()) {
    $thread->join();
    }

    unlink $key . ".txt";
    return;
    }









    share|improve this question









    New contributor




    falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'd like to optimize my code and get a better of understanding of how I can perform the task I am doing better. I've only used Perl threads about 3 or 4 times now.



      The purpose of my script / code block is to scrape the Pastebin API for every new public paste that appears at regular intervals using a crontab. Then search inside those new pastes for user defined regular expressions such as "google.com." The paste is then saved to a sqlite DB for later viewing and a notification is sent to a listening nodejs bot.



      That being said here is my code block I wish to have improved upon:



      sub threadCheckKey {
      my ($url, $key, @regexs) = @_;
      my $fullURL = $url.$key;
      my @flaggedRegex = ();
      my $date = strftime "%D", localtime;
      my @data = ();

      my $thread = threads->create(sub {
      my $dbConnection = openDB();
      open(GET_DATA, "-|", "curl -s " . $fullURL . " -k") or die("$!");
      open(WRITE_FILE, ">", $key . ".txt") or die("$!");
      while(my $line = <GET_DATA>) {
      print WRITE_FILE $line;
      foreach my $regex(@regexs) {
      if($line =~ m/$regex/) {
      if(!(grep(/$regex/, @flaggedRegex))) {
      push(@flaggedRegex, $regex);
      }
      }
      }
      }
      close(WRITE_FILE);
      close(GET_DATA);

      my $flaggedSize = @flaggedRegex;
      if($flaggedSize == 0) {
      #Uncomment below for debugging.
      #print "No user defined regex foundn";
      return;
      }

      open(READ_FILE, "<", $key . ".txt") or die("$!");
      while(my $line = <READ_FILE>) {
      push(@data, $line);
      }
      close(READ_FILE);

      my $inputData = join("r", @data);
      my $flaggedReg = join(" | ", @flaggedRegex);
      my $updateRow = qq(UPDATE $tables[0] set data = ?, date = ?, regex = ? where pastekey = ?);
      my $updateRowPrepare = $dbConnection->prepare($updateRow);
      my $executeRowUpdate = $updateRowPrepare->execute($inputData, $date, $flaggedReg, $key);

      if($executeRowUpdate < 0) {
      print $DBI::errstr;
      }

      my $msg = qq(**Alert:** New paste found! The paste can be viewed via https://pastebin.com/$key - Regex Trigger: $flaggedReg);

      open(SEND_ALERT, "-|", "curl https://localhost:8051/bots/bot_service_id/alert -k --data '{"text":"$msg"}' -H "Content-Type: application/json" -X POST") or die("$!");
      close(SEND_ALERT);

      $dbConnection->disconnect();
      });

      if($thread->is_running()) {
      sleep(2);
      }

      if($thread->is_joinable()) {
      $thread->join();
      }

      unlink $key . ".txt";
      return;
      }









      share|improve this question









      New contributor




      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'd like to optimize my code and get a better of understanding of how I can perform the task I am doing better. I've only used Perl threads about 3 or 4 times now.



      The purpose of my script / code block is to scrape the Pastebin API for every new public paste that appears at regular intervals using a crontab. Then search inside those new pastes for user defined regular expressions such as "google.com." The paste is then saved to a sqlite DB for later viewing and a notification is sent to a listening nodejs bot.



      That being said here is my code block I wish to have improved upon:



      sub threadCheckKey {
      my ($url, $key, @regexs) = @_;
      my $fullURL = $url.$key;
      my @flaggedRegex = ();
      my $date = strftime "%D", localtime;
      my @data = ();

      my $thread = threads->create(sub {
      my $dbConnection = openDB();
      open(GET_DATA, "-|", "curl -s " . $fullURL . " -k") or die("$!");
      open(WRITE_FILE, ">", $key . ".txt") or die("$!");
      while(my $line = <GET_DATA>) {
      print WRITE_FILE $line;
      foreach my $regex(@regexs) {
      if($line =~ m/$regex/) {
      if(!(grep(/$regex/, @flaggedRegex))) {
      push(@flaggedRegex, $regex);
      }
      }
      }
      }
      close(WRITE_FILE);
      close(GET_DATA);

      my $flaggedSize = @flaggedRegex;
      if($flaggedSize == 0) {
      #Uncomment below for debugging.
      #print "No user defined regex foundn";
      return;
      }

      open(READ_FILE, "<", $key . ".txt") or die("$!");
      while(my $line = <READ_FILE>) {
      push(@data, $line);
      }
      close(READ_FILE);

      my $inputData = join("r", @data);
      my $flaggedReg = join(" | ", @flaggedRegex);
      my $updateRow = qq(UPDATE $tables[0] set data = ?, date = ?, regex = ? where pastekey = ?);
      my $updateRowPrepare = $dbConnection->prepare($updateRow);
      my $executeRowUpdate = $updateRowPrepare->execute($inputData, $date, $flaggedReg, $key);

      if($executeRowUpdate < 0) {
      print $DBI::errstr;
      }

      my $msg = qq(**Alert:** New paste found! The paste can be viewed via https://pastebin.com/$key - Regex Trigger: $flaggedReg);

      open(SEND_ALERT, "-|", "curl https://localhost:8051/bots/bot_service_id/alert -k --data '{"text":"$msg"}' -H "Content-Type: application/json" -X POST") or die("$!");
      close(SEND_ALERT);

      $dbConnection->disconnect();
      });

      if($thread->is_running()) {
      sleep(2);
      }

      if($thread->is_joinable()) {
      $thread->join();
      }

      unlink $key . ".txt";
      return;
      }






      multithreading io perl sqlite curl






      share|improve this question









      New contributor




      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 14 at 17:10









      200_success

      127k15148410




      127k15148410






      New contributor




      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 14 at 16:28









      falconspy

      1162




      1162




      New contributor




      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      falconspy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          falconspy is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207665%2fperl-pastebin-scraper%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          falconspy is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          falconspy is a new contributor. Be nice, and check out our Code of Conduct.













          falconspy is a new contributor. Be nice, and check out our Code of Conduct.












          falconspy is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207665%2fperl-pastebin-scraper%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei