Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import org.apache.commons.io.IOUtils;
- import java.net.URL;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class Main {
- static String url = "bar";
- static String itemString = "foo";
- static String inStockString = "Yes";
- // static String inStockString = "Out of Stock<br> No Backorder";
- public static void main(String[] args) throws Exception {
- while (true)
- {
- checkStock();
- Thread.sleep(60 * 1000);
- }
- }
- private static void checkStock() throws Exception
- {
- Iterator<String> i = ((List<String>)IOUtils.readLines(new URL(url).openStream())).iterator();
- while (i.hasNext())
- {
- String s = i.next();
- // System.out.println(s);
- if (s.contains(itemString))
- {
- s = i.next();
- // System.out.println(s);
- if (s.contains(inStockString))
- {
- System.out.println(new Date() + " In stock");
- sendMail();
- System.exit(0);
- }
- else
- {
- System.out.println(new Date() + " Not in stock");
- }
- }
- }
- }
- private static void sendMail() throws Exception
- {
- // Recipient's email ID needs to be mentioned.
- String to = "ariel@weisberg.ws";
- // Sender's email ID needs to be mentioned
- String from = "ariel@weisberg.ws";
- // Assuming you are sending email from localhost
- String host = "smtp.fastmail.com";
- // Get system properties
- Properties properties = System.getProperties();
- String username = "";
- String password = "";
- // Setup mail server
- properties.setProperty("mail.smtp.host", host);
- properties.setProperty("mail.user", username);
- properties.setProperty("mail.password", password);
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.socketFactory.port", 465);
- properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- properties.put("mail.smtp.socketFactory.fallback", "false");
- // Get the default Session object.
- Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator()
- {
- protected PasswordAuthentication getPasswordAuthentication()
- {
- return new PasswordAuthentication(username, password);
- }
- });
- // Create a default MimeMessage object.
- MimeMessage message = new MimeMessage(session);
- // Set From: header field of the header.
- message.setFrom(new InternetAddress(from));
- // Set To: header field of the header.
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- // Set Subject: header field
- message.setSubject("Stuff is in stock");
- // Send the actual HTML message, as big as you like
- message.setText("Indeed it is in stock!");
- // Send message
- Transport.send(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement