def refresh_cache(cacheid): """ Update the stored cache info with new data if it exists """ cacheid = cacheid.upper() conn = sqlite.check_db() g_arr = get_row(conn, cacheid) ret = get_cache_page(conn, cacheid, g_arr.cacheurl) if ret is None: print("Failed to update cache details, are we logged in?") return lat, lon, short, body, hint, attributes = ret dltime = int(time.time()) g_arr.dltime = dltime g_arr.lat = float(lat) g_arr.lon = float(lon) g_arr.short = short g_arr.body = body g_arr.hint = hint sqlite.add_to_db(conn, g_arr, attributes)
def get_cache_list(lat, lon): """ Search for the nearest 50 unfound caches not owned by the app """ loc = htmlcode.decdeg2dm(lat, lon) url = "https://www.geocaching.com/play/search?lat=" + str(lat) + "&lng=" + str(lon) + \ "&origin=" + loc + "&radius=100km&o=2&sort=Distance&asc=True" print(url) conn = sqlite.check_db() try: html = SESSION.get(url) data = html.text except Exception as error: print("606 - bombed out, are we still logged in?") print(error) return None if "li-user-info" not in data: print("bombed out, are we still logged in?") return None data = data.split('<tbody id="geocaches">', 1)[1].split("</tbody>", 1)[0].strip() rows = data.split('<tr data-rownumber="') for row in rows: try: row = row.strip() if row == "": continue lat = 0.0 lon = 0.0 short = "" body = "" hint = "" dltime = 0 found = 0 if row.find("cache-types.svg#icon-found") != -1: found = 1 cacheid = row.split('data-id="', 1)[1].split('"', 1)[0].strip() print("Found cacheid: " + cacheid) cachename = row.split('data-name="', 1)[1].split('"', 1)[0].strip() cachesize = row.split('data-column="ContainerSize">', 1)[1].split('</td>', 1)[0].strip() cacheowner = row.split('<span class="owner">', 1)[1].split('</span>', 1)[0].strip() cachetype = row.split('<span class="cache-details">', 1)[1].split('|', 1)[0].strip() diff = row.split('data-column="Difficulty">', 1)[1].split('</td>', 1)[0].strip() diff = float(diff) terr = row.split('data-column="Terrain">', 1)[1].split('</td>', 1)[0].strip() terr = float(terr) hidden = row.split('data-column="PlaceDate">', 1)[1].split('</td>', 1)[0].strip() hidden = clean_up(hidden) lastfound = row.split('data-column="DateLastVisited">', 1)[1].split('</td>', 1)[0].strip() lastfound = clean_up(lastfound) cacheurl = "https://www.geocaching.com" + row.split( '<a href="', 1)[1] cacheurl = cacheurl.split('"', 1)[0].strip() cache = get_row(conn, cacheid) if cache is None or (cache.lat == 0.0 and cache.lon == 0.0): ret = get_cache_page(conn, cacheid, cacheurl) if ret is None: print("Failed to update cache details, are we logged in?") return lat, lon, short, body, hint, attributes, found = ret else: print(cacheid + ": Already exists in the db, skipping...") lat = cache.lat lon = cache.lon short = cache.short body = cache.body hint = cache.hint attributes = sqlite.get_attributes(conn, cacheid) g_arr = geocache.GeoCache() g_arr.cacheid = cacheid g_arr.dltime = int(time.time()) g_arr.cachename = cachename g_arr.cacheowner = cacheowner g_arr.cachesize = cachesize g_arr.cacheurl = cacheurl g_arr.cachetype = cachetype g_arr.lat = float(lat) g_arr.lon = float(lon) g_arr.diff = diff g_arr.terr = terr g_arr.hidden = hidden g_arr.lastfound = lastfound g_arr.short = short g_arr.body = body g_arr.hint = hint g_arr.found = found sqlite.add_to_db(conn, g_arr, attributes) except Exception as error: print("459 - Failed to parse cache info.") print(error) close_db(conn)
def dl_cache(cacheid): """ Download and parse a cache page """ cacheid = cacheid.upper() conn = sqlite.check_db() cache_url = "https://www.geocaching.com/geocache/" + cacheid print(cache_url) try: html = SESSION.get(cache_url) data = html.text except Exception as error: print("494 - bombed out, are we still logged in?") print(error) return "bombed out, are we still logged in?" # TODO: check if cache is not found, 404 page doesnt show logged in user menu if "li-user-info" not in data: print("bombed out, are we still logged in?") return "bombed out, are we still logged in?" if data.find( "<strong id=\"ctl00_ContentBody_GeoNav_logText\">Found It!</strong>" ) != -1: found = 1 else: found = 0 print("Found cacheid: " + cacheid) cachename = data.split('<span id="ctl00_ContentBody_CacheName">', 1)[1] cachename = cachename.split('</span>', 1)[0].strip() cachesize = data.split('" title="Size: ', 1)[1].split(' ', 1)[0].strip() cacheowner = data.split('<div id="ctl00_ContentBody_mcd1">', 1)[1] cacheowner = cacheowner.split('">', 1)[1].split('</a>', 1)[0].strip() cachetype = data.split( '<a href="/about/cache_types.aspx" target="_blank" title="', 1)[1] cachetype = cachetype.split('"', 1)[0].strip() diff = data.split('<span id="ctl00_ContentBody_uxLegendScale" title="', 1)[1] diff = diff.split('alt="', 1)[1].split(' ', 1)[0].strip() diff = float(diff) terr = data.split('<span id="ctl00_ContentBody_Localize12" title="', 1)[1] terr = terr.split('alt="', 1)[1].split(' ', 1)[0].strip() terr = float(terr) hidden = data.split('<div id="ctl00_ContentBody_mcd2">', 1)[1] hidden = hidden.split('</div>', 1)[0].split(':', 1)[1].strip() hidden = hidden.split('\n', 1)[0].strip() hidden = clean_up(hidden) bits = data.split("var lat=", 1)[1].split(", guid='")[0].strip() lat = bits.split(", lng=", 1)[0].strip() lon = bits.split(", lng=", 1)[1].strip() short = data.split('<span id="ctl00_ContentBody_ShortDescription">', 1)[1] short = short.split("</span>", 1)[0].strip() body = data.split('<span id="ctl00_ContentBody_LongDescription">', 1)[1] body = body.split('<p id="ctl00_ContentBody_hints">', 1)[0].strip() body = '<span id="ctl00_ContentBody_LongDescription">' + body hint = data.split('<div id="div_hint" class="span-8 WrapFix">', 1)[1] hint = hint.split('</div>', 1)[0].strip() attributes = [] tmpstr = data.split('<div class="WidgetBody">', 1)[1] tmpstr = tmpstr.split('<p class="NoBottomSpacing">', 1)[0] for line in tmpstr.split('<img src="/images/attributes/'): line = line.strip() if line == "": continue line = line.split('.png"', 1)[0] if line == "attribute-blank": continue attributes.append(line) print("attribute == " + line) tmpstr = "{" + data.split('initialLogs = {', 1)[1] tmpstr = tmpstr.split('};', 1)[0] + "}" user_token = data.split("userToken = '", 1)[1].split("';", 1)[0].strip() save_logs(conn, cacheid, tmpstr, user_token) cursor = conn.cursor() cursor.execute("SELECT visited FROM logbook WHERE cacheid=? and logtype='Found it' " + \ "ORDER BY logid DESC LIMIT 1", (cacheid,)) ret = cursor.fetchone() cursor.close() if ret is not None: lastfound = ret[0] else: lastfound = -1 g_arr = geocache.GeoCache() g_arr.cacheid = cacheid g_arr.dltime = int(time.time()) g_arr.cachename = cachename g_arr.cacheowner = cacheowner g_arr.cachesize = cachesize g_arr.cacheurl = cache_url g_arr.cachetype = cachetype g_arr.lat = float(lat) g_arr.lon = float(lon) g_arr.diff = diff g_arr.terr = terr g_arr.hidden = hidden g_arr.lastfound = lastfound g_arr.short = short g_arr.body = body g_arr.hint = hint g_arr.found = found sqlite.add_to_db(conn, g_arr, attributes) close_db(conn) return True