Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

finished ebay script

  • Loading branch information...
commit 62323086a949613b87623aa9b704b1175c771d7c 1 parent a130513
siyigu1 siyigu1 authored
1  app/models/ebay.rb
@@ -2,6 +2,7 @@ class Ebay < ActiveRecord::Base
2 2 belongs_to :address
3 3 has_one :subscription, :as => :subscribable
4 4
  5 + validates :username, :presence => true
5 6 validates :first_name, :presence => true
6 7 validates :last_name, :presence => true
7 8 validates :country, :presence => true
5 db/migrate/20120408193358_add_column_to_ebay.rb
... ... @@ -0,0 +1,5 @@
  1 +class AddColumnToEbay < ActiveRecord::Migration
  2 + def change
  3 + add_column :ebays, :username, :string
  4 + end
  5 +end
15 db/schema.rb
@@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13
14   -ActiveRecord::Schema.define(:version => 20120403010959) do
  14 +ActiveRecord::Schema.define(:version => 20120408193358) do
15 15
16 16 create_table "addresses", :force => true do |t|
17 17 t.string "line1"
@@ -49,6 +49,19 @@
49 49
50 50 add_index "dmvs", ["address_id"], :name => "index_dmvs_on_address_id"
51 51
  52 + create_table "ebays", :force => true do |t|
  53 + t.string "first_name"
  54 + t.string "last_name"
  55 + t.string "country"
  56 + t.string "phone_number"
  57 + t.integer "address_id"
  58 + t.datetime "created_at"
  59 + t.datetime "updated_at"
  60 + t.string "username"
  61 + end
  62 +
  63 + add_index "ebays", ["address_id"], :name => "index_ebays_on_address_id"
  64 +
52 65 create_table "subscribers", :force => true do |t|
53 66 t.string "name"
54 67 t.string "img_path"
137 lib/ebay_udpate.py
... ... @@ -1,9 +1,11 @@
1 1
2 2 import sys
3 3 import mechanize
  4 +from BeautifulSoup import BeautifulSoup
  5 +import re
4 6
5 7 # Validate arguments
6   -if (sys.argv.__len__() != 11):
  8 +if (sys.argv.__len__() != 12):
7 9 print ("Arguments Invalid. \nUsage: filename username password first_name last_name address1 addres2 city state zipcode country phone_number")
8 10 sys.exit(0)
9 11
@@ -20,12 +22,28 @@
20 22 country = sys.argv[10]
21 23 phone = sys.argv[11]
22 24
  25 +
  26 +#parse the phone number
  27 +if(phone.find(" ext:") != -1):
  28 + phone_array = phone.split(' ext:',1);
  29 + phone_digits = phone_array[0].split("-", 2);
  30 + phone_extension = phone_array[1];
  31 +else:
  32 + phone_digits = phone.split("-",2);
  33 +
  34 +if len(phone_digits) != 3:
  35 + print "Phone Argument Invalid."
  36 + print "Please enter your phone number in the following format:"
  37 + print "XXX-XXX-XXXX ext:XX if you have an extension number"
  38 + print "or XXX-XXX-XXXX if you don't have an extension number"
  39 + sys.exit(0)
  40 +
23 41 # Prepare browser
24 42 browser = mechanize.Browser()
25 43 browser.set_handle_robots(False)
26 44 browser.addheaders = [("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]
27 45
28   -browser.open("https://signin.ebay.com/ws/eBayISAPI.dll?SignIn")
  46 +browser.open("https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&ru=http%3A%2F%2Fwww.ebay.com%2F")
29 47
30 48 # Select log-in form
31 49 browser.select_form(name="SignInForm")
@@ -33,80 +51,83 @@
33 51 browser["pass"] = password
34 52
35 53 browser.submit()
36   -
37   -if (browser.title() == "Welcome to eBay - Sign in"): # Wrong password or username
  54 +if (browser.title() != "eBay.com"): # Wrong password or username
38 55 print "The username and password you entered do not match any accounts on record."
39 56 sys.exit(0)
40 57
41 58 # Go to Add New Address page
42   -browser.open("http://payments.ebay.com/ws/eBayISAPI.dll?UserAddresses&cmd=standalone")
  59 +open_response = browser.open("http://payments.ebay.com/ws/eBayISAPI.dll?UserAddresses&cmd=standalone")
  60 +
  61 +# Get the country corresponding code for ebay
  62 +open_response_html = open_response.read()
  63 +open_soup = BeautifulSoup(open_response_html)
  64 +country_select = open_soup.find("select", id="country")
  65 +country_options = country_select.findAll("option")
  66 +
  67 +country_code = -1
  68 +
  69 +for option in country_options:
  70 + if country == option.string.encode():
  71 + country_code = option["value"].encode()
  72 +
  73 +if country_code == -1:
  74 + print "Update address failed."
  75 + print "Your country is not supported by eBay."
  76 + sys.exit(0)
  77 +
  78 +
43 79 browser.select_form(name="pageForm")
44 80
45 81 # Fill out the form
46   -browser["country"] = country
  82 +browser["country"] = [country_code]
47 83 browser["contactName"] = first_name + " " + last_name
48 84 browser["address1"] = address1
49 85 browser["address2"] = address2
50   -browser["stateName"] = state
51   -browser["enterAddressStateOrRegion"] = state
  86 +browser["city"] = city
  87 +browser["stateName"] = [state]
52 88 browser["zip"] = zipcode
53   -phone_array = phone.split('ext.',1);
54   -phone_digit = phone_array[0].split("-", 1);
55   -phone_extension = phone_array[1];
56   -browser["dayphone1"] = phone_digit[0];
57   -browser["dayphone2"] = phone_digit[1];
58   -browser["dayphone3"] = phone_digit[2];
59   -browser["dayphone4"] = phone_extension;
  89 +browser["dayphone1"] = phone_digits[0]
  90 +browser["dayphone2"] = phone_digits[1]
  91 +browser["dayphone3"] = phone_digits[2]
  92 +
  93 +if(phone.find(" ext:") != -1):
  94 + browser["dayphone4"] = phone_extension;
60 95
61 96 #make this address preferred address
62   -browser["preferred"] = 1;
  97 +browser["preferred"] = ["1"];
63 98
64 99 # Submit the form by clicking Save & Continue
65   -browser.submit(name="pageForm")
  100 +response = browser.submit(nr=3);
66 101
67   -if (browser.title() == "Your Account"):
  102 +#get the resposne html
  103 +response_html = response.read();
  104 +soup = BeautifulSoup(response_html);
  105 +errorMsg = soup.find("div", "stsMsg");
  106 +
  107 +#check if error message printed
  108 +if(errorMsg == None):
68 109 print "Congratulations! Successfully added new address!"
69 110 sys.exit(0)
70 111 else:
71   - browser.select_form(nr=1)
72   -
73   -try:
74   -
75   - # Select original address if an address was entered
76   - # and Amazon has a suggestion
77   - browser.find_control("addr").items[0].selected = True
78   -
79   - # Store suggestion for future print
80   - suggested_address1 = browser["addr_1address1"]
81   - suggested_address2 = browser["addr_1address2"]
82   - suggested_address3 = browser["addr_1address3"]
83   - suggested_city = browser["addr_1city"]
84   - suggested_state = browser["addr_1state"]
85   - suggested_zip = browser["addr_1zip"]
86   -
87   - browser.submit(name="useSelectedAddress")
88   -
89   - if (browser.title() == "Your Account"):
90   - # On address book. Successfully added new address
91   - print "Congratulations! Successfully added new address!"
92   -
93   - # Print suggestion
94   - print "Address suggested by Amazon:"
95   - print suggested_address1 # address1 should not be empty in any case
96   -
97   - # Print address lines if they are not empty
98   - if (suggested_address2 != ""):
99   - print suggested_address2
100   -
101   - if (suggested_address3 != ""):
102   - print suggested_address3
103   -
104   - print suggested_city + " " + suggested_state + ", " + suggested_zip
105   -
  112 + print "Update address failed."
  113 + errorName = soup.findAll("span", "status_fieldName")
  114 + errorInfo = soup.findAll("span", "status_errorMsg")
  115 +
  116 + #no error name meaning the error is an address error
  117 + #print the suggested address
  118 + if(len(errorName) == 0):
  119 + recommend_address_div = soup.findAll("div", id="raddrPanel")
  120 + recommend_address_spans = recommend_address_div[0].findAll("span")
  121 + print "Please verify your address. The address recommended for you is:"
  122 + for i in range (1, 3):
  123 + for content in recommend_address_spans[i].contents:
  124 + if content.string != None:
  125 + print(content.string),
  126 +
  127 + #print the error otherwise
106 128 else:
107   - # Address not added. Unknown Error
108   - print "Failed to add new address!"
  129 + print "You have to fix the errors below:"
  130 + for i in range(len(errorName)):
  131 + print (errorName[i].string.encode()) + " - " + (errorInfo[i].string.encode())
109 132
110   -except mechanize._form.ControlNotFoundError, e:
111   - # Could not find the control, due to invalid address. Exit
112   - print "Failed to add new address! Address not valid!"
  133 + sys.exit(0)
8 spec/models/ebay_spec.rb
@@ -3,15 +3,21 @@
3 3 describe Ebay do
4 4
5 5 before do
6   - @valid_ebay = Ebay.new(:first_name => "ebay", :last_name => "ebay", :country => "United States", :phone_number => "123-123-1234")
  6 + @valid_ebay = Ebay.new(:username => "usrname", :first_name => "ebay", :last_name => "ebay", :country => "United States", :phone_number => "123-123-1234")
7 7 @valid_ebay.address = Address.new(:line1 => "Address", :city => "somewhere", :zip => "123456", :state => "somestate")
8 8 @valid_ebay.should be_valid
9 9 end
10 10
  11 +
11 12 it "is a subscribable" do
12 13 @valid_ebay.subscription.should be_nil
13 14 end
14 15
  16 + it "requires username" do
  17 + @valid_ebay.username = ""
  18 + @valid_ebay.should_not be_valid
  19 + end
  20 +
15 21 it "requires first_name" do
16 22 @valid_ebay.first_name = ""
17 23 @valid_ebay.should_not be_valid

0 comments on commit 6232308

Please sign in to comment.
Something went wrong with that request. Please try again.