Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package com.bruno.checkpph;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.ScheduledExecutorService;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. import org.jsoup.Jsoup;
  15. import org.jsoup.nodes.Document;
  16. import org.jsoup.nodes.Element;
  17. import org.jsoup.select.Elements;
  18.  
  19.  
  20. public class CheckPPH
  21. {
  22.     private static final ScheduledExecutorService mScheduler = Executors.newScheduledThreadPool(1);
  23.    
  24.    
  25.     public static void main(String[] args)
  26.     {
  27.        
  28.         mScheduler.scheduleAtFixedRate(mRunnable, 5, 90, TimeUnit.SECONDS);
  29.     }
  30.    
  31.    
  32.     private static Runnable mRunnable = new Runnable()
  33.     {
  34.         public void run()
  35.         {
  36.             checkEtc();
  37.         }
  38.     };
  39.    
  40.    
  41.     private static void checkEtc()
  42.     {
  43.        
  44.         File file = new File("/home/brx/jobs.txt");
  45.        
  46.         Scanner numScan = null;
  47.         try{
  48.             numScan = new Scanner(file);
  49.         }
  50.         catch(FileNotFoundException e){
  51.             e.printStackTrace();
  52.         }
  53.        
  54.         String line;
  55.        
  56.         ArrayList<String> links = new ArrayList<String>();
  57.         while(numScan.hasNext()){
  58.             line = numScan.nextLine();
  59.             links.add(line);
  60.         }
  61.        
  62.         Document doc = null;
  63.         try{
  64.             doc = Jsoup
  65.                 .connect(
  66.                     "http://www.peopleperhour.com/freelance-android-jobs?remote=GB&onsite=GB&filter=all&sort=latest#scroll=0")
  67.                 .get();
  68.         }
  69.         catch(IOException e){
  70.             e.printStackTrace();
  71.         }
  72.        
  73.         // <a class="job" title="Android App" href="http://www.peopleperhour.com/job/android-app-665907">Android App</a>
  74.        
  75.         StringBuilder sb = new StringBuilder();
  76.         Elements newsHeadlines = doc.select("a.job");
  77.         for(Element element : newsHeadlines){
  78.            
  79.             String title = element.attr("title");
  80.             String href = element.attr("href");
  81.            
  82.             if(!title.equalsIgnoreCase("Post Job") && !href.contains("?ref=topnav_loggedout")){
  83.                 if(!links.contains(href)){
  84.                     sb.append(href + "\n");
  85.                     System.out.print("adding to string buider:" + title);
  86.                     notifySend(href);
  87.                 }
  88.                 else{
  89.                     System.out.print("NOT adding to string buider:" + title);
  90.                 }
  91.             }
  92.         }
  93.         writeToFile(sb);
  94.     }
  95.    
  96.     private static void notifySend(String title)
  97.     {
  98.         String command = "notify-send " + title + " -u critical -i '/home/brx/Pictures/pph.png' -t 1000";
  99.        
  100.         Process p;
  101.         try{
  102.             p = Runtime.getRuntime().exec(command);
  103.             p.waitFor();
  104.             p.destroy();
  105.         }
  106.         catch(Exception e){}
  107.     }
  108.    
  109.     private static void writeToFile(StringBuilder sb)
  110.     {
  111.         try{
  112.             File file = new File("/home/brx/yo.txt");
  113.            
  114.             if(!file.exists()){
  115.                 file.createNewFile();
  116.             }
  117.            
  118.             FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
  119.             BufferedWriter bw = new BufferedWriter(fw);
  120.             bw.write(sb.toString());
  121.             bw.close();
  122.             System.out.println("Done writing to /home/brx/yo.txt"); // For testing
  123.         }
  124.         catch(IOException e){
  125.             System.out.println("Error: " + e);
  126.             e.printStackTrace();
  127.         }
  128.     }
  129.    
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement