private void OldSchoolGrabbing(OfflineCacheData ocd2) { // La vielle méthode un peu incimpréhensible qui marche bool bSpoilersAlreadyInDescription = false; // Build images to download from description if (ocd2._descHTML == "") { // Nothing to do for the description } else { // Description present, looking for img url's ocd2._descHTML = "<html><body>" + ocd2._descHTML + "</body></html>"; // Parse HTML description to retrieve images // Load the Html into the agility pack HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(ocd2._descHTML); // Now, using LINQ to get all Images var imageNodes = doc.DocumentNode.SelectNodes("//img"); if (imageNodes != null) { foreach (HtmlAgilityPack.HtmlNode node in imageNodes) { if (node.Attributes.Contains("src")) { String url = node.Attributes["src"].Value; if (ocd2._ImageFilesFromDescription.ContainsKey(url) == false) { String file = ocd2._Code + "_" + Guid.NewGuid(); ocd2._ImageFilesFromDescription.Add(url, file); } } } } // Surfoo style: try to get spoilers from the description bSpoilersAlreadyInDescription = GetSpoilersFromDescription(ocd2); // Free some memory :-) ocd2._descHTML = ""; } _daddy.Log("Retrieving spoilers from cache " + ocd2._Code); if (!bSpoilersAlreadyInDescription) { _daddy.Log("Spoilers taken from HTML parsing"); // Parse html page and detect image to download in addition to the ones in the description // Spoilers ? GetImageFromParsing(ocd2); } else { _daddy.Log("Spoilers taken from GPX description"); } }
/// <summary> /// Fetch geocache base on ocd._Code and download HTML page to list images to grab /// Images will be stored in ocd._ImageFilesSpoilers /// </summary> /// <param name="ocd">OCD object to complete</param> /// <param name="bUseKeyWords">If true, spoilers will be downloaded based on keywords provided in keywordsspoiler</param> /// <param name="htmlCode">Cache html page</param> public void GetImageFromParsingImpl(OfflineCacheData ocd, bool bUseKeyWords, string htmlCode) { // Patch String chunk = MyTools.GetSnippetFromText("<ul class=\"CachePageImages NoPrint\">", "</ul>", htmlCode); chunk = "<html><body>" + chunk + "</body></html>"; // Parse HTML to retrieve links // Load the Html into the agility pack HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(chunk); // Now, using LINQ to get all Images var linkNodes = doc.DocumentNode.SelectNodes("//a[@href]"); if (linkNodes != null) { //String ahref = ""; foreach (HtmlAgilityPack.HtmlNode node in linkNodes) { if (node.Attributes.Contains("href")) { String name = HtmlAgilityPack.HtmlEntity.DeEntitize(node.InnerText); bool bKeep = true; if (bUseKeyWords) { bKeep = false; // Check if one keyword is contained in the name String n = name.ToLower(); foreach (String s in keywordsspoiler) { if (n.Contains(s)) { bKeep = true; break; } } } if (bKeep) { String url = node.Attributes["href"].Value; OfflineImageWeb oiw = new OfflineImageWeb(); oiw._url = url; oiw._localfile = ocd._Code + "_P_" + Guid.NewGuid(); oiw._name = name; ocd._ImageFilesSpoilers.Add(oiw._url, oiw); } } } } }
/// <summary> /// Merge stats from existing OfflineCacheData if they are better defined /// use new stats IF /// - more _iNbFounds /// - or more _iNbNotFounds /// - or more _iNbFavs /// - don't bother using the date, it's wrong anyway /// If altitude of ocd is valid, we will merge it anyway /// </summary> /// <param name="ocd">existing OfflineCacheData with stats</param> /// <returns>true if stats have been merged</returns> public bool MergeStats(OfflineCacheData ocd) { // Simple check if ((this == ocd) || (this._Code != ocd._Code)) { return(false); } bool merged = false; // use new stats IF // more _iNbFounds // or more _iNbNotFounds // or more _iNbFavs // don't bother using the date, it's wrong anyway if ((ocd._iNbFavs > this._iNbFavs) || (ocd._iNbFounds > this._iNbFounds) || (ocd._iNbNotFounds > this._iNbNotFounds)) { // We merge it this._iNbFavs = ocd._iNbFavs; this._iNbFounds = ocd._iNbFounds; this._iNbFoundsPremium = ocd._iNbFoundsPremium; this._iNbNotFounds = ocd._iNbNotFounds; this._iNbNotFoundsPremium = ocd._iNbNotFoundsPremium; this._dRating = ocd._dRating; this._dRatingSimple = ocd._dRatingSimple; merged = true; } else { merged = false; } // Si l'altitude est valide, on l'utilise if (ocd._dAltiMeters != Double.MaxValue) { this._dAltiMeters = ocd._dAltiMeters; merged = true; } return(merged); }
/// <summary> /// Fetch geocache base on ocd._Code and download HTML page to list images to grab /// Images will be stored in ocd._ImageFilesSpoilers /// </summary> /// <param name="ocd">OCD object to complete</param> private void GetImageFromParsing(OfflineCacheData ocd) { bool bUseKeyWords = false; if ((keywordsspoiler != null) && (keywordsspoiler.Count != 0)) { bUseKeyWords = true; } try { Geocache geo = _daddy._caches[ocd._Code]; String url = geo._Url; /* * WebClient client = new WebClient(); * AssignProxy(client); * string htmlCode = client.DownloadString(url); */ // nouvelle méthode utilisant le cookie si besoin HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); objRequest.Proxy = _daddy.GetProxy(); if (cookieJar != null) { objRequest.CookieContainer = cookieJar; } HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); string htmlCode; using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream())) { htmlCode = responseStream.ReadToEnd(); responseStream.Close(); } GetImageFromParsingImpl(ocd, bUseKeyWords, htmlCode); } catch (Exception) { } }
/// <summary> /// Read stats from a file (for import) /// </summary> /// <param name="line">Stats for ONE OfflineCacheData</param> /// <returns>New OfflineCacheData with stats from file</returns> public static OfflineCacheData ReadStats(String line) { if (line != "") { List <string> myList = line.Split(';').ToList(); if (myList.Count == 10) { OfflineCacheData ocd = new OfflineCacheData(); ocd._Code = myList[0]; DateTime dt; DateTime.TryParseExact(myList[1], "yyyy/MM/dd HH:mm:ss", null, System.Globalization.DateTimeStyles.None, out dt); ocd._dateExport = dt; ocd._iNbFavs = Int32.Parse(myList[2]); ocd._iNbFounds = Int32.Parse(myList[3]); ocd._iNbNotFounds = Int32.Parse(myList[4]); ocd._iNbFoundsPremium = Int32.Parse(myList[5]); ocd._iNbNotFoundsPremium = Int32.Parse(myList[6]); ocd._dRating = Double.Parse(myList[7], CultureInfo.CreateSpecificCulture("en-GB")); ocd._dRatingSimple = Double.Parse(myList[8], CultureInfo.CreateSpecificCulture("en-GB")); try { ocd._dAltiMeters = Double.Parse(myList[9], CultureInfo.CreateSpecificCulture("en-GB")); } catch (Exception) { } return(ocd); } else { return(null); } } else { return(null); } }
public bool DoIt() { if (_Daddy != null) { String exePath = Path.GetDirectoryName(Application.ExecutablePath); Directory.SetCurrentDirectory(exePath); String imgPath = "ExportedImages"; Directory.CreateDirectory(imgPath); // Now export List <Geocache> lst = _Daddy.GetDisplayedCaches(); String offdatapath = ConfigurationManager.AppSettings["datapath"] + "\\Offline\\"; int nb = 0; foreach (Geocache geo in lst) { // Save images & spoilers and geotag them if (_Daddy._od._OfflineData.ContainsKey(geo._Code)) { _Daddy.Log(">>>>>>> Exporting " + geo._Code); // Do we have images to load and save? OfflineCacheData ocd = _Daddy._od._OfflineData[geo._Code]; String lastS = geo._Code.Substring(geo._Code.Length - 1, 1); String prelastS = geo._Code.Substring(geo._Code.Length - 2, 1); if (ocd._ImageFilesSpoilers.Count != 0) { _Daddy.Log("Go with spoilers"); int ipic = 0; foreach (KeyValuePair <string, OfflineImageWeb> paire in ocd._ImageFilesSpoilers) { String radix = geo._Code + "_" + String.Format("{0:000}", ipic) + "_" + HtmlAgilityPack.HtmlEntity.DeEntitize(paire.Value._name) + ".jpg"; radix = _Daddy.SanitizeFilename(radix); String fjpeg = imgPath + "\\" + radix; ipic++; try { // Load image Image img = Image.FromFile(offdatapath + paire.Value._localfile); // Save to Jpeg try { // Test with this filename img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception) { // Ok standard filename, something wrong happened fjpeg = imgPath + "\\" + geo._Code + "_" + String.Format("{0:000}", ipic) + ".jpg"; img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); } img.Dispose(); _Daddy.WriteCoordinatesToImage(fjpeg, geo._dLatitude, geo._dLongitude); nb++; } catch (Exception e1) { // Do nothing _Daddy.Log(e1.Message); } } } } } _Daddy.MSG(nb.ToString() + " Image(s) exported"); return(true); } else { MessageBox.Show("Nothing exported"); return(false); } }
public bool DoIt() { /* * http://garmin.blogs.com/softwareupdates/2012/01/geocaching-with-photos.html * * Works With All Geocaches * You can also take advantage of geocache photos on your Garmin handheld for geocaches obtained from a source other than OpenCaching.com, it just takes some work. A geocache’s photos, JPEG only, need to be placed on the handheld’s mass storage in the following manner * * Photos * \Garmin\GeocachePhotos\Last Character\Second To Last Character\Full Code\ * * Spoiler Photos * <Photos Path>\Spoilers\ * For example, photos for a geocache with code OXZTXGC would be placed under the path * * \Garmin\GeocachePhotos\C\G\OXZTXGC\ * * And spoilers would be placed under * * \Garmin\GeocachePhotos\C\G\OXZTXGC\Spoilers * * If the geocache has only three characters total, a 0 (zero) is used for the second to last character. For example, photos for a geocache with code OXR would be placed under the path * * \Garmin\GeocachePhotos\R\0\OXR\ */ if (_Daddy != null) { String exePath = Path.GetDirectoryName(Application.ExecutablePath); Directory.SetCurrentDirectory(exePath); String imgGarmin = "Garmin"; imgGarmin = "Garmin\\GeocachePhotos"; Directory.CreateDirectory(imgGarmin); Directory.SetCurrentDirectory(imgGarmin); // Now export List <Geocache> lst = _Daddy.GetDisplayedCaches(); String offdatapath = ConfigurationManager.AppSettings["datapath"] + "\\Offline\\"; int nb = 0; foreach (Geocache geo in lst) { // Save images & spoilers and geotag them if (_Daddy._od._OfflineData.ContainsKey(geo._Code)) { _Daddy.Log(">>>>>>> Exporting " + geo._Code); // Do we have images to load and save? OfflineCacheData ocd = _Daddy._od._OfflineData[geo._Code]; String lastS = geo._Code.Substring(geo._Code.Length - 1, 1); String prelastS = geo._Code.Substring(geo._Code.Length - 2, 1); if ((ocd._ImageFilesSpoilers.Count != 0) || (ocd._ImageFilesFromDescription.Count != 0)) { int ipic = 0; _Daddy.Log("Images found !"); // Spoilers _Daddy.Log("Spoilers ?"); if (ocd._ImageFilesSpoilers.Count != 0) { _Daddy.Log("Go with spoilers"); String imgPath = lastS + "\\" + prelastS + "\\" + geo._Code + "\\Spoilers"; Directory.CreateDirectory(imgPath); foreach (KeyValuePair <string, OfflineImageWeb> paire in ocd._ImageFilesSpoilers) { String radix = geo._Code + "_" + String.Format("{0:000}", ipic) + "_" + HtmlAgilityPack.HtmlEntity.DeEntitize(paire.Value._name) + ".jpg"; radix = _Daddy.SanitizeFilename(radix); String fjpeg = imgPath + "\\" + radix; ipic++; try { // Load image Image img = Image.FromFile(offdatapath + paire.Value._localfile); // Save to Jpeg try { // Test with this filename img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception) { // Ok standard filename, something wrong happened fjpeg = imgPath + "\\" + geo._Code + "_" + String.Format("{0:000}", ipic) + ".jpg"; img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); } img.Dispose(); _Daddy.WriteCoordinatesToImage(fjpeg, geo._dLatitude, geo._dLongitude); nb++; } catch (Exception e1) { // Do nothing _Daddy.Log(e1.Message); } } } // Images from description _Daddy.Log("Description ?"); if (ocd._ImageFilesFromDescription.Count != 0) { _Daddy.Log("Go with description"); String imgPath = lastS + "\\" + prelastS + "\\" + geo._Code; Directory.CreateDirectory(imgPath); foreach (KeyValuePair <string, string> paire in ocd._ImageFilesFromDescription) { String fjpeg = imgPath + "\\" + geo._Code + "_" + String.Format("{0:000}", ipic) + ".jpg"; ipic++; try { // Load image Image img = Image.FromFile(offdatapath + paire.Value); // Save to Jpeg img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); img.Dispose(); nb++; } catch (Exception e1) { // Do nothing _Daddy.Log(e1.Message); } } } } } } _Daddy.MSG(nb.ToString() + " Image(s) exported"); return(true); } else { MessageBox.Show("Nothing exported"); return(false); } }
private void button1_Click(object sender, EventArgs e) { // Look for tags to add foreach (ListViewItem item in listView1tagmgr.Items) { // To be added if not already present in the initial list if (_TagsInit.Contains(item.Text) == false) { _TagsToAdd.Add(item.Text); } } // Look for tag to remove foreach (String t in _TagsInit) { // to be removed if not present in the listview if (listView1tagmgr.FindItemWithText(t) == null) { _TagsToDel.Add(t); } } // Ok, now parse all the caches and update their tags foreach (Geocache cache in _selCaches) { // Create ocd is not existing if (cache._Ocd == null) { OfflineCacheData ocd1 = new OfflineCacheData(); ocd1._dateExport = DateTime.Now; ocd1._Comment = ""; ocd1._Code = cache._Code; ocd1._bBookmarked = false; _daddy.AssociateOcdCache(cache._Code, ocd1, cache); } // Add tags cache._Ocd.AddTags(_TagsToAdd); // Remove Tags cache._Ocd.RemoveTags(_TagsToDel); // Check if need to remove ocd data? if (cache._Ocd.IsEmpty()) { _daddy.RemoveAssociationOcdCache(cache._Code); } } // update tag list _daddy.CreateListStringTags(); // Update listview foreach (ListViewItem item in _selLVItems) { String code = item.Text; Geocache geo = _daddy._caches[code]; EXMultipleImagesListViewSubItem subitem = ((EXMultipleImagesListViewSubItem)(item.SubItems[_daddy._ID_LVTag])); //if (true) { ArrayList imgitem = _daddy.GetListImageTags(geo._Ocd); if ((imgitem != null) && (imgitem.Count != 0)) { subitem.MyImages = imgitem; subitem.MyValue = geo._Ocd.GetTags(); } else { subitem.MyImages = null; subitem.MyValue = ""; } }/* * else * { * String t = ""; * if (geo._Ocd != null) * t = geo._Ocd.GetTags(); * subitem.Text = t; * subitem.MyValue = t; * }*/ } }
private String ExportExploristGC(String path) { Directory.SetCurrentDirectory(path); String imgMagellan = "MAGELLAN"; if (Directory.Exists(imgMagellan)) { try { Directory.Delete(imgMagellan, true); } catch (Exception) {} } String gpxmagellan = "MAGELLAN\\Geocaches"; Directory.CreateDirectory(gpxmagellan); imgMagellan = "MAGELLAN\\Images\\Geocaches"; Directory.CreateDirectory(imgMagellan); Directory.SetCurrentDirectory(imgMagellan); String fileRadix = path + "\\" + gpxmagellan + "\\GPX_ExploristGC.gpx"; String offdatapath = ConfigurationManager.AppSettings["datapath"] + "\\Offline\\"; System.IO.StreamWriter file = new System.IO.StreamWriter(fileRadix, false); file.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); file.WriteLine("<gpx xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" creator=\"Groundspeak, Inc. All Rights Reserved. http://www.groundspeak.com\" 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/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd\" xmlns=\"http://www.topografix.com/GPX/1/0\">"); file.WriteLine(" <name>Cache Listing Generated from Geocaching.com</name>"); file.WriteLine(" <desc>This is an individual cache generated from Geocaching.com</desc>"); file.WriteLine(" <author>Account \"" + ConfigurationManager.AppSettings["owner"] + "\" From Geocaching.com</author>"); file.WriteLine(" <email>[email protected]</email>"); file.WriteLine(" <url>http://www.geocaching.com</url>"); file.WriteLine(" <urlname>Geocaching - My Geocaching Manager Export " + DateTime.Now.ToLongDateString() + "</urlname>"); // 2011-09-18T07:00:00Z String date = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ"); file.WriteLine(" <time>" + date + "</time>"); file.WriteLine(" <keywords>cache, geocache</keywords>"); double minlat = 90.0; double minlon = 180.0; double maxlat = -90.0; double maxlon = -180.0; String bigtext = ""; List <Geocache> lst = _Daddy.GetSelectedCaches(); foreach (Geocache geo in lst) { if (geo._dLatitude < minlat) { minlat = geo._dLatitude; } if (geo._dLongitude < minlon) { minlon = geo._dLongitude; } if (geo._dLatitude > maxlat) { maxlat = geo._dLatitude; } if (geo._dLongitude > maxlon) { maxlon = geo._dLongitude; } String chunk = geo.ToGPXChunk(); // Save images & spoilers in base 64? if (_Daddy._od._OfflineData.ContainsKey(geo._Code)) { OfflineCacheData ocd = _Daddy._od._OfflineData[geo._Code]; if ((ocd._ImageFilesSpoilers.Count != 0) || (ocd._ImageFilesFromDescription.Count != 0)) { int ipic = 0; String fjpeg = ""; if (ocd._ImageFilesSpoilers.Count != 0) { String spoilersImg = ""; foreach (KeyValuePair <string, OfflineImageWeb> paire in ocd._ImageFilesSpoilers) { try { // Load image fjpeg = geo._Code + "_" + String.Format("{0:000}", ipic) + ".jpg"; Image img = Image.FromFile(offdatapath + paire.Value._localfile); img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); img.Dispose(); String imgsrc = "<br><li>" + paire.Value._name + "<br><img src=\"" + fjpeg + "\"></li>"; spoilersImg += imgsrc; ipic++; } catch (Exception e1) { // Do nothing _Daddy.Log("!!!!!! Spoiler " + e1.Message); } } chunk = chunk.Replace("</groundspeak:long_description>", spoilersImg + "</groundspeak:long_description>"); } if (ocd._ImageFilesFromDescription.Count != 0) { foreach (KeyValuePair <string, string> paire in ocd._ImageFilesFromDescription) { try { // Load image fjpeg = geo._Code + "_" + String.Format("{0:000}", ipic) + ".jpg"; Image img = Image.FromFile(offdatapath + paire.Value); img.Save(fjpeg, System.Drawing.Imaging.ImageFormat.Jpeg); img.Dispose(); // Save to Jpeg chunk = chunk.Replace(paire.Key, fjpeg); ipic++; } catch (Exception e1) { // Do nothing _Daddy.Log("!!!!!! Description " + e1.Message); } } } } } bigtext += chunk; } // <bounds minlat="-12.03455" minlon="-77.017917" maxlat="-12.03455" maxlon="-77.017917" /> String bounds = ""; bounds = " <bounds minlat=\"" + minlat + "\" minlon=\"" + minlon + "\" maxlat=\"" + maxlat + "\" maxlon=\"" + maxlon + "\" />"; bounds = bounds.Replace(",", "."); file.WriteLine(bounds); file.Write(bigtext); file.WriteLine("</gpx>"); file.Close(); System.Diagnostics.Process.Start(path + "\\MAGELLAN"); MessageBox.Show("Export done in " + fileRadix, "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Information); return(fileRadix); }
private bool GetSpoilersFromDescription(OfflineCacheData ocd) // cf. Surfoo { List <KeyValuePair <String, String> > spoilers = new List <KeyValuePair <string, string> >(); try { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(ocd._descHTML); var commentNodes = doc.DocumentNode.SelectNodes("//comment()"); if (commentNodes != null) { foreach (HtmlAgilityPack.HtmlNode node in commentNodes) { String cmt = node.InnerHtml; if (cmt.StartsWith("<!-- Spoiler4Gpx [")) { int ipos1, ipos2; String sName = "", sLink = ""; ipos1 = cmt.IndexOf("["); ipos2 = cmt.IndexOf("](http"); if ((ipos1 != -1) && (ipos2 != -1) && (ipos1 < ipos2)) { sName = cmt.Substring(ipos1 + 1, ipos2 - ipos1 - 1); } ipos1 = cmt.IndexOf("](http"); ipos2 = cmt.IndexOf(") -->"); if ((ipos1 != -1) && (ipos2 != -1) && (ipos1 < ipos2)) { sLink = cmt.Substring(ipos1 + 2, ipos2 - ipos1 - 2); } if ((sName != "") && (sLink != "")) { spoilers.Add(new KeyValuePair <string, string>(sName, sLink)); } } } } if (spoilers.Count == 0) { return(false); } else { // Now, deal with these spoilers bool bUseKeyWords = false; if ((keywordsspoiler != null) && (keywordsspoiler.Count != 0)) { bUseKeyWords = true; } foreach (KeyValuePair <String, String> paire in spoilers) { String name = paire.Key; String url = paire.Value; bool bKeep = true; if (bUseKeyWords) { bKeep = false; // Check if one keyword is contained in the name String n = name.ToLower(); foreach (String s in keywordsspoiler) { if (n.Contains(s)) { bKeep = true; break; } } } if (bKeep) { OfflineImageWeb oiw = new OfflineImageWeb(); oiw._url = url; oiw._localfile = ocd._Code + "_P_" + Guid.NewGuid(); oiw._name = name; ocd._ImageFilesSpoilers.Add(oiw._url, oiw); } } } return(true); } catch (Exception) { return(false); } }