Skip to content

Commit

Permalink
Merge 5ac46f4 into 5d930f3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbedrich committed May 8, 2015
2 parents 5d930f3 + 5ac46f4 commit 59d4760
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
36 changes: 36 additions & 0 deletions pycaching/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import os
import logging
import datetime
from pycaching.errors import ValueError
Expand Down Expand Up @@ -171,6 +172,11 @@ def __init__(self, wp, geocaching, *, name=None, cache_type=None, location=None,
def __str__(self):
return self.wp

def __repr__(self):
lst = ["%s: %s" % (self.wp, self.name),
]
return os.linesep.join(lst)

def __eq__(self, other):
return self.wp == other.wp

Expand Down Expand Up @@ -382,3 +388,33 @@ def pm_only(self, pm_only):
def inside_area(self, area):
"""Calculate if geocache is inside given area"""
return area.inside_area(self.location)

def to_gpx(self):
"""
Return the cache description as XML string
"""
xml = """
<wpt lon="{1}" lat="{0}">
<time>{2}</time>
<name>{3}</name>
<desc>{4} by {6}, {5} ({8}/{9})</desc>
<url>http://coord.info/{3}</url>
<urlname>{4}</urlname>
<sym>Geocache</sym>
<type>Geocache|{5}</type>
<groundspeak:cache xmlns:groundspeak="http://www.groundspeak.com/cache/1/0/2" id="{3}"archived="false" available="true">
<groundspeak:name>{4}</groundspeak:name>
<groundspeak:placed_by>{6}</groundspeak:placed_by>
<groundspeak:type>{6}</groundspeak:type>
<groundspeak:container>{7}</groundspeak:container>
<groundspeak:difficulty>{8}</groundspeak:difficulty>
<groundspeak:terrain>{9}</groundspeak:terrain>
<groundspeak:long_description html="True"> {10} </groundspeak:long_description>
<groundspeak:encoded_hints>{11}</groundspeak:encoded_hints>
</groundspeak:cache>
</wpt>
""".format(self.location.latitude, self.location.longitude, self.hidden.isoformat(),
self.wp, self.name, self.cache_type, self.author, self.size, self.difficulty,
self.terrain, self.description, self.hint,
)
return xml
39 changes: 39 additions & 0 deletions scripts/save-gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
# coding: utf8

import argparse
import pycaching
import time

def parse():
parser = argparse.ArgumentParser(usage="save-gpx -u user -p pass GC12345", description="Retrieve and save information about a cache in GPX format.", add_help=True)
parser.add_argument("-u", "--user", type=str, dest='user')
parser.add_argument("-p", "--password", type=str, dest="password")
parser.add_argument("geocache", nargs='+')
return parser.parse_args()


def process(opts):
geocaching = pycaching.login(opts.user, opts.password)
for cache_name in opts.geocache:
if cache_name.endswith(".gpx"):
cache_name = cache_name[:-4]
print("Retrieve %s" % cache_name)
cache = geocaching.load_cache(cache_name)
with open(cache_name + ".gpx", "w") as f:
f.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n')
f.write('<gpx \n')
f.write(' version="1.0"\n')
f.write(' creator="pycaching"\n')
f.write(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n')
f.write(' xmlns="http://www.topografix.com/GPX/1/0"\n')
f.write(' xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/2 http://www.groundspeak.com/cache/1/0/2/cache.xsd">\n')
f.write(' <name>GeoCaching GPX</name>\n')
f.write(' <time>%s</time>\n'%time.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
f.write(' <keywords>geocache, cache, opencaching, waypoint</keywords>\n')
f.write(cache.to_gpx())
f.write('</gpx>')

if __name__=="__main__":
opts = parse()
process(opts)
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"long_description": long_description,
"keywords": ["geocaching", "crawler", "geocache", "cache", "searching", "geocoding"],
"install_requires": ["MechanicalSoup >= 0.2.0", "geopy >= 1.0.0"],
"test_suite": "test"
}
"test_suite": "test",
"scripts": ["scripts/save-gpx"]
}

setup(**info)

0 comments on commit 59d4760

Please sign in to comment.