Advertisement
Guest User

Java sucks

a guest
Aug 26th, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package com.company;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4.  
  5. import java.net.URL;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import java.util.List;
  9.  
  10. import java.util.*;
  11. import javax.mail.*;
  12. import javax.mail.internet.*;
  13.  
  14. public class Main {
  15.  
  16.     static String url = "bar";
  17.     static String itemString = "foo";
  18.     static String inStockString = "Yes";
  19. //    static String inStockString = "Out of Stock<br> No Backorder";
  20.  
  21.     public static void main(String[] args) throws Exception {
  22.         while (true)
  23.         {
  24.             checkStock();
  25.             Thread.sleep(60 * 1000);
  26.         }
  27.     }
  28.  
  29.     private static void checkStock() throws Exception
  30.     {
  31.         Iterator<String> i =  ((List<String>)IOUtils.readLines(new URL(url).openStream())).iterator();
  32.         while (i.hasNext())
  33.         {
  34.             String s = i.next();
  35. //            System.out.println(s);
  36.             if (s.contains(itemString))
  37.             {
  38.                 s = i.next();
  39. //                System.out.println(s);
  40.                 if (s.contains(inStockString))
  41.                 {
  42.                     System.out.println(new Date() + " In stock");
  43.                     sendMail();
  44.                     System.exit(0);
  45.                 }
  46.                 else
  47.                 {
  48.                     System.out.println(new Date() + " Not in stock");
  49.                 }
  50.             }
  51.         }
  52.     }
  53.  
  54.     private static void sendMail() throws Exception
  55.     {
  56.         // Recipient's email ID needs to be mentioned.
  57.         String to = "ariel@weisberg.ws";
  58.  
  59.         // Sender's email ID needs to be mentioned
  60.         String from = "ariel@weisberg.ws";
  61.  
  62.         // Assuming you are sending email from localhost
  63.         String host = "smtp.fastmail.com";
  64.  
  65.         // Get system properties
  66.         Properties properties = System.getProperties();
  67.  
  68.         String username = "";
  69.         String password =  "";
  70.  
  71.         // Setup mail server
  72.         properties.setProperty("mail.smtp.host", host);
  73.         properties.setProperty("mail.user", username);
  74.         properties.setProperty("mail.password", password);
  75.         properties.put("mail.smtp.auth", "true");
  76.         properties.put("mail.smtp.socketFactory.port", 465);
  77.         properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  78.         properties.put("mail.smtp.socketFactory.fallback", "false");
  79.  
  80.         // Get the default Session object.
  81.         Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator()
  82.         {
  83.             protected PasswordAuthentication getPasswordAuthentication()
  84.             {
  85.                 return new PasswordAuthentication(username, password);
  86.             }
  87.         });
  88.  
  89.         // Create a default MimeMessage object.
  90.         MimeMessage message = new MimeMessage(session);
  91.  
  92.         // Set From: header field of the header.
  93.         message.setFrom(new InternetAddress(from));
  94.  
  95.         // Set To: header field of the header.
  96.         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  97.  
  98.         // Set Subject: header field
  99.         message.setSubject("Stuff is in stock");
  100.  
  101.         // Send the actual HTML message, as big as you like
  102.         message.setText("Indeed it is in stock!");
  103.  
  104.         // Send message
  105.         Transport.send(message);
  106.  
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement