diff options
author | Brendan Long <self@brendanlong.com> | 2017-10-21 20:27:41 +0300 |
---|---|---|
committer | Brendan Long <self@brendanlong.com> | 2017-10-21 20:29:32 +0300 |
commit | bc54172073afe8fab80d1fce00349b7a765adf94 (patch) | |
tree | f6d7af697a0a206b30f6651bd184180889fb572d | |
parent | 3aec4fc78eaabe876c1e0b8fadc9d9c3d51d6f7a (diff) |
Only read and write metadata once
-rw-r--r-- | src/FavIcon.vala | 159 |
1 files changed, 80 insertions, 79 deletions
diff --git a/src/FavIcon.vala b/src/FavIcon.vala index b2802a63..14b758aa 100644 --- a/src/FavIcon.vala +++ b/src/FavIcon.vala @@ -127,100 +127,105 @@ public class FeedReader.FavIcon : GLib.Object } } - var default_expires = new DateTime.now_utc().add_days(Constants.REDOWNLOAD_FAVICONS_AFTER_DAYS); - if(m_metadata.expires == null || m_metadata.expires.to_unix() < default_expires.to_unix()) + try { - m_metadata.expires = default_expires; - yield m_metadata.save_to_file_async(metadata_filename); - } + var obvious_icons = new Gee.ArrayList<string>(); - var obvious_icons = new Gee.ArrayList<string>(); + if(m_feed.getIconURL() != null) + obvious_icons.add(m_feed.getIconURL()); - if(m_feed.getIconURL() != null) - obvious_icons.add(m_feed.getIconURL()); + // try domainname/favicon.ico + var uri = new Soup.URI(m_feed.getURL()); + string? siteURL = null; + if(uri != null) + { + string hostname = uri.get_host(); + siteURL = uri.get_scheme() + "://" + hostname; + + var icon_url = siteURL; + if(!icon_url.has_suffix("/")) + icon_url += "/"; + icon_url += "favicon.ico"; + obvious_icons.add(icon_url); + } - // try domainname/favicon.ico - var uri = new Soup.URI(m_feed.getURL()); - string? siteURL = null; - if(uri != null) - { - string hostname = uri.get_host(); - siteURL = uri.get_scheme() + "://" + hostname; - - var icon_url = siteURL; - if(!icon_url.has_suffix("/")) - icon_url += "/"; - icon_url += "favicon.ico"; - obvious_icons.add(icon_url); - } + // Try to find one of those icons + foreach(var url in obvious_icons) + { + var stream = yield downloadIcon(url, cancellable); + if(stream != null) + return stream; - // Try to find one of those icons - foreach(var url in obvious_icons) - { - var stream = yield downloadIcon(url, cancellable); - if(stream != null) - return stream; + if(cancellable != null && cancellable.is_cancelled()) + return null; + } - if(cancellable != null && cancellable.is_cancelled()) + // If all else fails, download html and parse to find location of favicon + if(siteURL == null) return null; - } - // If all else fails, download html and parse to find location of favicon - if(siteURL == null) - return null; - - var message_html = new Soup.Message("GET", siteURL); - if(Settings.tweaks().get_boolean("do-not-track")) - message_html.request_headers.append("DNT", "1"); + var message_html = new Soup.Message("GET", siteURL); + if(Settings.tweaks().get_boolean("do-not-track")) + message_html.request_headers.append("DNT", "1"); - string html; - try - { - var bodyStream = yield Utils.getSession().send_async(message_html); - html = (string)yield Utils.inputStreamToArray(bodyStream, cancellable); - } - catch (Error e) - { - Logger.warning(@"Request for $siteURL failed: " + e.message); - return null; - } - if(html != null && message_html.status_code == 200) - { - var html_cntx = new Html.ParserCtxt(); - html_cntx.use_options(Html.ParserOption.NOERROR + Html.ParserOption.NOWARNING); - Html.Doc* doc = html_cntx.read_doc(html, siteURL, null, Html.ParserOption.NOERROR + Html.ParserOption.NOWARNING); - if(doc == null) + string html; + try + { + var bodyStream = yield Utils.getSession().send_async(message_html); + html = (string)yield Utils.inputStreamToArray(bodyStream, cancellable); + } + catch (Error e) { - Logger.debug(@"Utils.downloadFavIcon: parsing html on $siteURL failed"); + Logger.warning(@"Request for $siteURL failed: " + e.message); return null; } - - try + if(html != null && message_html.status_code == 200) { - // check for <link rel="icon"> - var xpath = grabberUtils.getURL(doc, "//link[@rel='icon']"); - - if(xpath == null) - // check for <link rel="shortcut icon"> - xpath = grabberUtils.getURL(doc, "//link[@rel='shortcut icon']"); - - if(xpath == null) - // check for <link rel="apple-touch-icon"> - xpath = grabberUtils.getURL(doc, "//link[@rel='apple-touch-icon']"); + var html_cntx = new Html.ParserCtxt(); + html_cntx.use_options(Html.ParserOption.NOERROR + Html.ParserOption.NOWARNING); + Html.Doc* doc = html_cntx.read_doc(html, siteURL, null, Html.ParserOption.NOERROR + Html.ParserOption.NOWARNING); + if(doc == null) + { + Logger.debug(@"Utils.downloadFavIcon: parsing html on $siteURL failed"); + return null; + } - if(xpath != null) + try + { + // check for <link rel="icon"> + var xpath = grabberUtils.getURL(doc, "//link[@rel='icon']"); + + if(xpath == null) + // check for <link rel="shortcut icon"> + xpath = grabberUtils.getURL(doc, "//link[@rel='shortcut icon']"); + + if(xpath == null) + // check for <link rel="apple-touch-icon"> + xpath = grabberUtils.getURL(doc, "//link[@rel='apple-touch-icon']"); + + if(xpath != null) + { + xpath = grabberUtils.completeURL(xpath, siteURL); + return yield downloadIcon(xpath, cancellable); + } + } + finally { - xpath = grabberUtils.completeURL(xpath, siteURL); - return yield downloadIcon(xpath, cancellable); + delete doc; } } - finally + + return null; + } + finally + { + var default_expires = new DateTime.now_utc().add_days(Constants.REDOWNLOAD_FAVICONS_AFTER_DAYS); + if(m_metadata.expires == null || m_metadata.expires.to_unix() < default_expires.to_unix()) { - delete doc; + m_metadata.expires = default_expires; } + yield m_metadata.save_to_file_async(metadata_filename); } - - return null; } private async InputStream? downloadIcon(string? icon_url, Cancellable? cancellable) throws GLib.Error @@ -233,7 +238,6 @@ public class FeedReader.FavIcon : GLib.Object string filename_prefix = m_icon_path + m_feed.getFeedFileName(); string local_filename = @"$filename_prefix.ico"; - string metadata_filename = @"$filename_prefix.txt"; string etag = m_metadata.etag; string last_modified = m_metadata.last_modified; @@ -286,7 +290,6 @@ public class FeedReader.FavIcon : GLib.Object m_metadata.last_modified = message.response_headers.get_one("Last-Modified"); var cache_control = message.response_headers.get_list("Cache-Control"); - m_metadata.expires = new DateTime.now_utc().add_days(Constants.REDOWNLOAD_FAVICONS_AFTER_DAYS); if(cache_control != null) { foreach(var header in message.response_headers.get_list("Cache-Control").split(",")) @@ -297,13 +300,11 @@ public class FeedReader.FavIcon : GLib.Object var seconds = int64.parse(parts[1]); var expires = new DateTime.now_utc(); expires.add_seconds(seconds); - if(expires.to_unix() > m_metadata.expires.to_unix()) - m_metadata.expires = expires; + m_metadata.expires = expires; } } m_metadata.last_modified = message.response_headers.get_one("Last-Modified"); - yield m_metadata.save_to_file_async(metadata_filename); return new MemoryInputStream.from_data(data); } |