Auto-Filter

Development-related discussion, including bundled plugins
dViper
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 07 Jun 2013, 01:54

Auto-Filter

Postby dViper » 07 Jun 2013, 02:31

Before hand I would like to excuse my self because I know that the questions I will ask are not directly related to tt-rss but are rather web programming (maybe basic at that) problems.

I've recently taken a course on Machine Learning and would like to test it out on tt-rss.
I currently have it installed and running on my machine and have tried various combinations of code to print a file on the server with the contents (and tags) of the article "liked" or "not liked". I have managed to get the images on the articles (copy paste from mailto plugin) but i am not familiar with web programming (php, java, ajax, etc.). From what I understand, PHP runs on server javascript on the client side, what I want is to have a button on the articles one for like and another for not liked that when pressed will not rederict the user but will give the desired info to the server for it to append to a file.
It would seem that I must use PHP to make the buttons and javascript to collect the info and send it to another PHP file for writing to file. I was able to get the input from user skipping but it redirected to another the page. I started over and have the following init PHP code:

Code: Select all

<?php
class LA_Filter extends Plugin {
   private $host;

   function about() {
      return array(1.0,
         "Adds a filter that adapts to users preferences",
         "AF");
   }

   function init($host) {
      $this->host = $host;
      $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
   }

   function get_js() {
      return file_get_contents(dirname(__FILE__) . "/ChkLike.js");
   }

   function hook_article_button($line) {
      return "<img src=\"plugins/la_filter/like\"
         class='tagsPic' style=\"cursor : pointer\"
         onclick=\"likeArticle(".$line["id"].",1)\"
         title='".__('Like')."'>

         <img src=\"plugins/la_filter/dislike\"
         class='tagsPic' style=\"cursor : pointer\"
         onclick=\"likeArticle(".$line["id"].",0)\"
         title='".__('Do Not Like')."'>
         ";
   }
//<a href=\"plugins/la_filter/mayLike.php?id=1\"><img src=\"plugins/la_filter/like\" ~ </a>
//onclick=\"dlikeArticle(".$line['int_id'].");\"


   function likeArticle() {
      $myFile = "./LikeFile.txt";
      $fh = fopen($myFile, 'a');

      $stringData = "New Stuff 1\n";
//      $stringData = $article["content"];

      fwrite($fh, $stringData);
      fclose($fh);
   }

   function dlikeArticle() {
      $file = fopen("test.txt","w");
      echo fwrite($file,"Hello World. Testing!");
      fclose($file);
   }

   function api_version() {
      return 2;
   }
}
?>


I realized that the like function in this PHP code does not work because it would run on the client (if it worked) so i Made a Javascript file called ChkLike.js with the function likeArticle that tries to send some data to a second PHP file called mayLike.php the codes i have so far are:
js:

Code: Select all

function likeArticle(id,YN) {

    try {
        URL url;
        URLConnection urlConnection;
        DataOutputStream outStream;
        DataInputStream inStream;
 
        // Build request body
        String body =
        "like=" + URLEncoder.encode("1", "UTF-8") +
        "&test=" + URLEncoder.encode("what", "UTF-8");
 
        // Create connection
        url = new URL("localhost/tt-rss/plugins/la_filter/mayLike.php");
        urlConnection = url.openConnection();
        ((HttpURLConnection)urlConnection).setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Content-Length", ""+ body.length());
 
        // Create I/O streams
        outStream = new DataOutputStream(urlConnection.getOutputStream());
        inStream = new DataInputStream(urlConnection.getInputStream());
 
        // Send request
        outStream.writeBytes(body);
        outStream.flush();
        outStream.close();
 
        // Get Response
        // - For debugging purposes only!
        String buffer;
        while((buffer = inStream.readLine()) != null) {
            System.out.println(buffer);
        }
 
        // Close I/O streams
        inStream.close();
        outStream.close();
    }
    catch(Exception ex) {
        System.out.println("Exception cought:\n"+ ex.toString());
}


}


PHP:

Code: Select all

<?php

$like= $_POST['like'];

   if ($like == 1)
   {
      $myFile = "/plugins/la_filter/LikeFile.txt";
      $fh = fopen($myFile, 'a');

      $stringData = "New Stuff 1\n";
//      $stringData = $article["content"];

      fwrite($fh, $stringData);
      fclose($fh);
   }
   else if ($like == 0)
   {
      print '<script type="text/javascript">';
      print 'alert("Nice try")';
      print '</script>';
   }

?>


If any can help fix this code or has a more simple implementation I would be very great full.
The current problem is that with the plugin enabled when i call the javascript the tt-rss does not load (with user logedin).

dViper
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 07 Jun 2013, 01:54

Re: Auto-Filter

Postby dViper » 09 Jun 2013, 00:56

I looked in to it and now use ajax to send the server the notification. I use:

Code: Select all

   function hook_article_button($line) {
      return "<img src=\"plugins/la_filter/like\"
         class='tagsPic' style=\"cursor : pointer\"
         onclick=\"
   {
   var xmlhttp;
   try{
      xmlhttp = new XMLHttpRequest();
   } catch (e){alert('Browser Error!');}

   xmlhttp.open('GET', '/tt-rss/plugins/la_filter/mayLike.php?like=1', true) ;       
   xmlhttp.send();
        }\"
         title='".__('Like')."'>

         <img src=\"plugins/la_filter/dislike\"
         class='tagsPic' style=\"cursor : pointer\"
         onclick=\"{    alert('Second button works!');     }\"
         title='".__('Do Not Like')."'>
         ";
   }


Now when the first image is clicked it sends like=1 to my PHP file without refreshing or redirecting and the PHP file appends info to a file in the server. What I need is for the information to be the contents of the specific article where it was clicked but I don't know how to get it and send it to my PHP file from the event of my image being clicked.
Can anyone point me in the right direction.


Return to “Development”

Who is online

Users browsing this forum: No registered users and 2 guests