Junglee Price Drop Alert

PriceTestLinkExample.txt
========================
http://www.junglee.com/dp/B00B15ZVTS 19000 saikatd@xxxxxxx.com
http://www.junglee.com/dp/B0095FOXXG 58000 saikatd@xxxxxxx.com

Client.java
============
import java.io.*;
import java.net.*;
import java.util.*; 

public class Client 
{
	@SuppressWarnings("deprecation")
	public static void main(String args[]) throws Exception
	{
		String inputLine = null;				// define the input stream
		String priceString = null;				// define for reading price from text file
		String priceThresholdString = null;		          // define the price threshold
		String urlString = null;				// define the url of the weblink
		String emailAddress = null;				// define the email address
		String tag = "whole-price";		          // define the tag for catch the price in weblink

		double priceThreshold = 0;				// define the price threshold in double
		double price = 0;					// define the price in double

		URL url = null;					// define the url
		URLConnection connection;				// define the connection
		DataInputStream dis;				// define the input data stream

		// a non-stop loop to continue monitor the prices of some items
		while (true){
			Scanner scan = new Scanner(new FileReader("src/PriceTestLinkExample.txt"));	// scan the text file
			while (scan.hasNext()){
				urlString = scan.next ();						// read the weblink from the text file
				System.out.println ("WebLink: " + urlString);			// print the weblink
				priceThresholdString = scan.next ();					// read the price from the text file
				System.out.println ("Your Threshold Price: Rs. " + priceThresholdString);	// print the price
				emailAddress = scan.next ();						// read the email address from the text file
				System.out.println ("Your Email Address: " + emailAddress);	// print the email address

				try {
					url = new URL (urlString);							// define the URL
					connection = url.openConnection ();						// connect the weblink
					dis = new DataInputStream(connection.getInputStream ());	// input data stream

					while ((inputLine = dis.readLine ()) != null) {				// read the input data stream
						if (inputLine.contains (tag)){				// catch the price using tag
							int startPos = inputLine.indexOf(">");
							int endPos = inputLine.indexOf("<", startPos);
							priceString = inputLine.substring (startPos + 1, endPos);							                                       priceString = priceString.replaceAll("[^\\d.]", "");							                    price = Double.parseDouble (priceString);		// convert string to double
							priceThreshold = Double.parseDouble (priceThresholdString);
							System.out.println ("Current Lowest Price: Rs. " + price);							                    break;
						}
					}
					dis.close();			                        // close the input data stream
				} catch (MalformedURLException me) {
					System.out.println ("MalformedURLException: " + me); // print information if MalformedURLException
				} catch (IOException ioe) {
					System.out.println ("IOException: " + ioe);	     // print information if IOException
				}

				Email email = new Email ();
				// Compare the price, if the price is lower than the price you expected, sent you an email 
				if (price <= priceThreshold){
					System.out.println ("******************");	
					System.out.println ("Price Drop!");
					System.out.println ("Sending email ...");
					email.SendEmail (emailAddress, urlString, priceThreshold, price);	// email the weblink to the email address
					System.out.println ("Email Sent.");
					System.out.println ("******************");
					System.out.println ();
				} else {
					// if not, keep waiting.
					System.out.println ("The price is still higher than you expected, keep waiting.");
					System.out.println ("----------------------------------------------------------");
					System.out.println ();
				}
			}

			// 1 minute delay
			try	{
				Thread.sleep(60000); // do nothing for 60000 milliseconds (60 seconds)
			} catch(InterruptedException e)	{
				e.printStackTrace();
			}
		} 
	}
}

Email.java
===========
import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

public class Email
{ 
	public void SendEmail(String EmailAddress, String WebLink, double priceThreshold, double price) throws Exception 
	{ 
		String host = "smtp.gmail.com"; 					// define mail server host name
		String from = "xxxxx.yyy@gmail.com"; 					// define sending email address
		String to = EmailAddress; 							// define receiving email address
		String username = "xxxxx.yyy@gmail.com"; 				// define sending email username
		String password = "xxxxxxx";				                   // define sending email password

		Properties props = new Properties(); 					          // Get system properties 
		props.put("mail.smtp.host", host);					// Setup mail server host name
		props.put("mail.smtp.port", "587");				// Setup mail server port
		props.put("mail.smtp.starttls.enable","true");			// Setup mail server SSL to true
		props.put("mail.smtp.auth", "true"); 				// Setup mail server authentication to true

		Session session = Session.getDefaultInstance(props); 	// Get session 

		MimeMessage message = new MimeMessage(session); 		// Define message 
		message.setFrom(new InternetAddress(from)); 			// Define message sender 
		message.addRecipient(Message.RecipientType.TO, 
				new InternetAddress(to)); 			// Define message receiver
		message.setSubject("Junglee Price Drop Alert for " + WebLink); 		// Define message subject
		message.setText("Hi, there is a price drop in the item: "
				+ WebLink + ", you subscribed!!!" + "\nYour Threshold Price: Rs. " 
				+ priceThreshold + "\nCurrent Lowest Price: Rs. " + price); 	// Define message text

		// Send message 
		message.saveChanges(); 
		Transport transport = session.getTransport("smtps"); 
		transport.connect(host, username, password); 			// Connect to mail server
		transport.sendMessage(message, message.getAllRecipients()); 
		transport.close(); 
	} 
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s