O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Amazon Hacks
By Paul Bausch
August 2003
More Info

HACK
#25
Create Several Birthday Reminders at Once
You probably have dozens of birthdays to track. You can speed up the process of adding Amazon event reminders with a script
The Code
[Discuss (0) | Link to this hack]

The Code

As with other services that require you to be logged in, the event reminder service requires an Amazon session ID that is tied to your account. This script first logs in as you via SSL and sets the session ID. Then it loops through the birthdays, calling the AddEvent subroutine, which sends a form post to Amazon with the event information.

<%
' add_reminders.asp
' Turns a supplied list of names and birthdates into Amazon events
' Usage: An ASP page called from any web browser

Const YOUR_EMAIL = "insert email address"
Const AMZN_PASSWORD = "insert Amazon password"
Const WinHttpRequestOption_EnableRedirects = 6

Sub AddEvent(sid,name,e_month,e_day)
    strURL = "https://www.amazon.com/exec/obidos/reminder-edit-done/" 
    strURL = strURL & strSessionID
    strPostData = "reminder-id=" & _
                  "&reminder-frequency=on" & _
                  "&sort-by=reminder-date" & _
                  "&customer-email=" & YOUR_EMAIL & _
                  "&occasion-id=3" & _
                  "&smart-send-options-enabled=yes" & _
                  "&time-zone-name=US/Pacific" & _
                  "&7-days-before=no" & _
                  "&recipient-customer-id=" & _
                  "&recipient-name=" & name & _
                  "&recipient-lower-email=" & _
                  "&gift-recipient-customer-id=" & _
                  "&gift-recipient-age-range=00-00" & _
                  "&gift-recipient-gender=" & _
                  "&month=" & e_month & _
                  "&day-of-month=" & e_day & _
                  "&14-days-before=on" & _
                  "&0-days-before=no" & _
                  "&1-days-before=no" & _
                  "&interest1=" & _
                  "&30-days-before=no" & _
                  "&x=63" & _
                  "&y=7"
    
    Set httppost = Server.CreateObject("Msxml2.SERVERXMLHTTP")
    httppost.Open "POST", strURL, false
    httppost.setRequestHeader "Content-Type",&return;
        "application/x-www-form-urlencoded"
    httppost.Send(strPostData)
    If Err.Number <> 0 Then
        'WScript.Echo httppost.Error
    End If    
    strText = httppost.ResponseText
    strHeaders = httppost.GetAllResponseHeaders
    strStatus = httppost.status
    Set httppost = Nothing
End Sub

If Request("txaBirthdays") <> "" Then
    'Start an Amazon Session
    strURL = "https://www.amazon.com/exec/obidos/flex-sign-in-done/"
    strPostData = "email=" & YOUR_EMAIL & _
                  "&password=" & AMZN_PASSWORD & _
                  "&method=get" & _
                  "&opt=an" & _
                  "&cont-page=cm/reminders-signed-in-continue" & _
                  "&cont-type=add-reminder" & _
                  "&response=reminder-add" & _
                  "&response=" & _
                  "stores/gifts/gifts-sign-in-secure.html" & _
                  "&action=sign-in" & _
                  "&next-page=" & _
                  "stores/gifts/gifts-register-secure.html"
    
    Set httppost = Server.CreateObject("WinHttp.WinHttpRequest.5")
    httppost.Open "POST", strURL, false
    'Turn off redirects
    httppost.Option(WinHttpRequestOption_EnableRedirects) = False
    httppost.setRequestHeader "Content-Type", &return;
"application/x-www-form-urlencoded"
    httppost.setRequestHeader "User-Agent", &return;
"(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"
    httppost.setRequestHeader "Accept", &return;
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"
    httppost.Send strPostData
    If Err.Number <> 0 Then
        response.write httppost.Error
    End If    
    strHeaders = httppost.GetAllResponseHeaders
    strStatus = httppost.status
    'response.write "<xmp>" & strStatus & Chr(13) & strHeaders & "</xmp>"
    
    Set objRegExpr = New regexp
    objRegExpr.Pattern = "session-id=(.*?);"
    objRegExpr.Global = True
    objRegExpr.IgnoreCase = True
    Set objMatches = objRegExpr.Execute(strHeaders)
    If objMatches.Count = 0 Then
        response.write "Amazon session couldn't be started."
        response.end
    Else
        For Each objMatch In objMatches
            strSessionID = objMatch.SubMatches(0)
        Next
    End If
    Set objMatches = Nothing
    Set objRegExpr = Nothing
    Set httppost = Nothing
    
    'Loop through the Birthdays
    cntEvent = 0
    arBDays = Split(Request("txaBirthdays"),Chr(13))
    For x = 0 To UBound(arBDays)
        arNameDate = Split(arBDays(x),",")
        strName = arNameDate(0)
        strDate = arNameDate(1)
        arMonthDay = Split(strDate,"/")
        strMonth = arMonthDay(0)
        strDay = arMonthDay(1)
        AddEvent strSessionID, strName, strMonth, strDay
        cntEvent = cntEvent + 1
    Next
    response.write cntEvent & &return;
" events added! Check your email for confirmation."
Else
%>
<html>
<head>
    <title>Add Event Reminders</title>
</head>

<body>
Add a list of birthdays below, one on each line:<br>
Format: [Name], [Birthday mm/dd]
<br>
<form action="add_reminders.asp" method="post">
    <textarea cols="35" rows="6" name="txaBirthdays"></textarea>
<br><input type="submit" value="Add Items">
</form>
</body>
</html>
<% End If %>


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.