% Wilma Bainbridge
% March 13 2017

% Updated for new SkyScanner on 11/5/20

close all
clear all

%pause(6);

mainurl = 'https://www.skyscanner.com/transport/flights/';
origin = 'chia/';
destination = 'bkkt/';
startdates = 4;
enddates = [10 11];
arbitraryname='airfares-from-washington-to-bangkok-in-january-2021.html';
adults= '?adults=1';
additionalterms = '&children=0&adultsv2=1&childrenv2=&infants=0&cabinclass=economy&rtn=1&preferdirects=false&outboundaltsenabled=true&inboundaltsenabled=true&seo_airline=fr&qp_prevProvider=ins_month&qp_prevCurrency=USD&qp_prevPrice=803&priceSourceId=unspecified-unspecified#results';



% Java stuff to control the mouse and keyboard
import java.awt.*;
import java.awt.event.*;
import java.awt.Robot;
mouse = Robot;

counter = 1;
for date1 = startdates
    for date2 = enddates
        if date1 < date2
            if date1 < 10
                date1str = ['21010' num2str(date1) '/'];
            else
                date1str = ['2101' num2str(date1) '/'];
            end
            if date2 < 10
                date2str = ['21010' num2str(date2) '/'];
            else
                date2str = ['2101' num2str(date2) '/'];
            end
            
            disp(['Searching for CHICAGO to BANGKOK, JAN ' num2str(date1) ' TO ' num2str(date2) '...']);
            disp('Opening browser.');
            web([mainurl origin destination date1str date2str arbitraryname adults additionalterms],'-browser');
            %
            % New code for my other monitor
            disp('Waiting for page to load...');
            pause(8)
            
            disp('Select and copy text.');
            % select first text
            mouse.mouseMove(-1630,580);
            mouse.mousePress(InputEvent.BUTTON1_MASK);
            pause(0.3);
            mouse.mouseRelease(InputEvent.BUTTON1_MASK);
            mouse.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
            mouse.keyPress(java.awt.event.KeyEvent.VK_A);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_A);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
            pause(1);
            
            % now copy
            mouse.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
            mouse.keyPress(java.awt.event.KeyEvent.VK_C);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_C);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
            
            % paste it into a document
            disp('Paste text into document.');
            pause(3);
            mouse.keyPress(java.awt.event.KeyEvent.VK_ALT);
            mouse.keyPress(java.awt.event.KeyEvent.VK_TAB);
            pause(0.3);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_ALT);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_TAB);
            pause(1);
            mouse.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
            mouse.keyPress(java.awt.event.KeyEvent.VK_V);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_V);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
            pause(0.3);
            
            % put a line
            for cc = 1:40
                mouse.keyPress(java.awt.event.KeyEvent.VK_X);
                mouse.keyRelease(java.awt.event.KeyEvent.VK_X);
            end
            
            disp('Go back to browser and repeat!');
            pause(0.3);
            % go back to browser
            mouse.keyPress(java.awt.event.KeyEvent.VK_ALT);
            mouse.keyPress(java.awt.event.KeyEvent.VK_TAB);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_ALT);
            mouse.keyRelease(java.awt.event.KeyEvent.VK_TAB);
            pause(1);
            counter = counter+1;
        end
    end
end