]> git.street.me.uk Git - andy/viking.git/blobdiff - tools/gcget
Add a 'home' default location preference.
[andy/viking.git] / tools / gcget
index 37231d54649c797edfdc1357cfa0e068accb055a..90513ba4a189d8de3387396a0803068dde88e9bd 100755 (executable)
@@ -45,12 +45,14 @@ Happy caching!!!
 #
 #
 try:
-  opts, args = getopt.gnu_getopt(sys.argv[1:], "u:p:", ["help"])
+  opts, args = getopt.gnu_getopt(sys.argv[1:], "u:p:d", ["help"])
 except getopt.GetoptError:
   # print help information and exit:
   help()
   sys.exit(2)
 
+DEBUG = False
+
 for o, a in opts:
   if o == "-p":
     PASS = a
@@ -59,17 +61,20 @@ for o, a in opts:
   if o == "--help" or o == "-h":
     help()
     sys.exit()
+  if o == "-d":
+    DEBUG = True
 
 if len(args) < 2:
   help()
   sys.exit()
 
 #########################
-
-ll = args[0].split(",")
-lat = ll[0]
-lon = ll[1]
-
+#ll = args[0].split(",")
+#lat = ll[0]
+#lon = ll[1]
+#The following line replaced the previous 3 lines.
+lat, lon = args[0].split(",")
+       
 if len(args) >= 3:
   maxdist = args[2]
 else:
@@ -87,18 +92,22 @@ import re
 from mechanize import Browser
 import ClientForm
 
+# get magic number for "Next" button.
+# this is normally 16 (link hidden is $ctl16), unless there are less than 10 pages of results,
+# in which case it will be less (e.g. 09 for 3 pages of results)
 def getmagicnumber(b):
   for i in range(16,0,-1):
-    if re.compile("pgrBottom..ctl%d" % i).search(b.response().get_data()):
+    if re.compile("pgrBottom.ctl%02d" % i).search(b.response().get_data()):
       return i
-  return 0
+  return None
 
 b=Browser()
 b.open("http://geocaching.com/seek/")
-b.follow_link(text="log in")
+b.follow_link(text="Log in")
 b.select_form(nr=0)
-b["myUsername"] = USER
-b["myPassword"] = PASS
+# The Username and Password fields on the Login form changed
+b["ctl00$ContentBody$myUsername"] = USER
+b["ctl00$ContentBody$myPassword"] = PASS
 b.submit()
 
 magicnumber = 0 # the ctl number of Next. get only once
@@ -108,8 +117,15 @@ except: pass
 try:
   b.select_form("form4")
 except:
+  b.select_form("form4")
   print >> sys.stderr, "Invalid username/password"
+  if DEBUG:
+    f=open("gcget.badlogin.html","w")
+    f.write(b.response().get_data())
+    f.close()
+    print >> sys.stderr, "Dumping last HTML page recieved into gcget.badlogin.html"
   sys.exit()
+
 b["origin_lat"] = lat
 b["origin_long"] = lon
 b["dist"] = maxdist
@@ -156,9 +172,12 @@ for ii in range(min(n,(records+19)/20)):
 
   if not magicnumber:
     magicnumber = getmagicnumber(b)
+    if not magicnumber:
+#      print "couldn't find magic number!" # why does this happen?
+      break
 
   b.select_form(nr=0)
-  [f for f in b.forms()][0].new_control("hidden", "pgrBottom$_ctl%d" % magicnumber, {})
+  [f for f in b.forms()][0].new_control("hidden", "ctl00$ContentBody$pgrBottom$ctl%02d" % magicnumber, {})
   b.submit()
 
 sys.stderr.write("\n")
@@ -169,3 +188,4 @@ if pages:
 #  f=open("delmeNOW","w")
 #  f.write(b.response().get_data())
 #  f.close()
+