<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: More on favatars, and the urlparse module</title>
	<atom:link href="http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse</link>
	<description>scratched tallies on the prison wall</description>
	<pubDate>Thu, 08 Jan 2009 02:10:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: N/A</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-741</link>
		<dc:creator>N/A</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-741</guid>
		<description>That&#8217;s pretty cool, but two improvements I&#8217;d recommend are making the image a link (but I don&#8217;t know what a gravatar is so maybe they&#8217;re meant to fulfill some other purpose) and also supporting icons that are given as a &lt;link rel="shortcut icon"&gt; in the poster&#8217;s &lt;span class="caps"&gt;URL&lt;/span&gt;. I don&#8217;t know if it is an MSism but many things (including Firefox) support it, it isn&#8217;t too hard to add, and it solves the problem of people in &lt;a href="http://www.kryogenix.org/days/2005/01/31/favatars#au1107183822.7"&gt;mrben&#8217;s situation&lt;/a&gt; (assuming he has his own pages on the shared host that he would be linking to and it isn&#8217;t a shared &lt;strong&gt;page&lt;/strong&gt; causing the forced sharing of icons). Here&#8217;s a function to get the most likely &lt;span class="caps"&gt;URL&lt;/span&gt; of an icon:
&lt;code&gt;
&lt;pre&gt;
def getIcoUrl(url):
    """Return the URL of the favourite icon associated with input.
    Returns None if input isn't a valid address."""
    try:
        fp = urllib.urlopen(url)
    except IOError, e:
        if e[1] == "unknown url type":
            # The given URL isn't a web address so adding
            # /favicon.ico won't work. Might as well
            # jump straight to giving up
            return None
    else:
        if fp.headers['content-type'].startswith('text/html') or fp.headers['content-type'].startswith('application/xhtml+xml'):
            data = fp.read()
            fp.close()
            # Try to find any links to shortcut icons
            ICONREGEX = "&#60;link rel="shortcut icon" (?:.*)href=(?:['&#124;"])(.[^'&#124;"&#124;\s]{0,})(?:['&#124;"])(?:.*)&#62;"
            results = re.search(ICONREGEX, data, re.I)
            if results != None:
                # Found the requested URL of the favourite icon
                icourl = results.groups()[0]
                # the base url is already known to be valid, so we
                # don't need to check that the join works.
                # If the icon's url is invalid, we'll let
                # loadResizeImage handle the inability to retrieve it
                return urlparse.urljoin(url, icourl)
    else:
        fp.close()
    # Return the default of url/favicon.ico
    return urlparse.urljoin(url, '/favicon.ico')
&lt;/pre&gt;
&lt;/code&gt;
I put that in code and pre blocks in the hope that it would not try to parse it, but the preview doesn&#8217;t look like it is going to work. It isn&#8217;t very hard to write yourself, but hopefully you can guess what it looks like untextiled (the numbered lists used to be comments) or get at the source somehow.
If you want to use this function you&#8217;ll have to rework the file naming since you probably don&#8217;t want to call this function for every request just to see what the cached image is/should be called, but that shouldn&#8217;t be too hard; saving the images as the user inputted &lt;span class="caps"&gt;URL&lt;/span&gt; instead of the image&#8217;s &lt;span class="caps"&gt;URL&lt;/span&gt; would eliminate the space saving feature of http://website/foobar always pointing to the file http&#8212;-website-favicon.ico for any value of foobar, but if you add in some code to normalise an &lt;span class="caps"&gt;URL &lt;/span&gt;(like always remove trailing slashes) it shouldn&#8217;t prove too much of a problem since most people would enter the root of their namespace. Or you could go all out and cache the results of this function in some sort of mapping file. &#8211; Testing:
&gt;&gt;&gt;print getIcoUrl(&#8216;http://www.python.org/doc/2.3/modindex.html&#8216;)
&#8216;http://www.python.org/doc/2.3/icons/pyfav.gif&#8217;
&gt;&gt;&gt; print getIcoUrl(&#8216;http://www.google.com&#8216;)
&#8216;http://www.google.com/favicon.ico&#8217; &#8211; Transparency: If the icon is a gif (all I tested with), you seem to be able to get transparency working fine by doing i.save(thumb,&#8216;PNG&#8216;,transparency=i.info[&#8216;transparency&#8216;]) in loadResizeImage() but &lt;span class="caps"&gt;ICO&lt;/span&gt; files don&#8217;t get a &#8216;transparency&#8217; key in their info dictionary (so you&#8217;d want to add a has_key() check before doing that form of save).
I have no idea really how transparency works in &lt;span class="caps"&gt;PIL&lt;/span&gt; or file formats in general, so I may be missing something obvious, but getcolors() only shows 4 colors for one of the icons with transparancy problems on your comments (simon.incutio.com/favicon.ico) and it looks like it uses 4 colors so one isn&#8217;t set aside just for transparency and converting to an &lt;span class="caps"&gt;RGBA&lt;/span&gt; image lists each of the 4 colors with a 4th value of 255 (fully opaque, I assume, but all the same value at any rate). Doing the same with  the python.org gif gives you an extra color &lt;strong&gt;and&lt;/strong&gt; lists one of the colors as having a 4th channel of value 0 (vs. 255 for the others) if converted to &lt;span class="caps"&gt;RGBA&lt;/span&gt;. So &lt;span class="caps"&gt;ICO&lt;/span&gt; files don&#8217;t support the same way of getting at the transparency info of other files, and I suspect it doesn&#8217;t support transparency on them at all.</description>
		<content:encoded><![CDATA[<p>That&#8217;s pretty cool, but two improvements I&#8217;d recommend are making the image a link (but I don&#8217;t know what a gravatar is so maybe they&#8217;re meant to fulfill some other purpose) and also supporting icons that are given as a
<link rel="shortcut icon"> in the poster&#8217;s <span class="caps">URL</span>. I don&#8217;t know if it is an MSism but many things (including Firefox) support it, it isn&#8217;t too hard to add, and it solves the problem of people in <a href="http://www.kryogenix.org/days/2005/01/31/favatars#au1107183822.7">mrben&#8217;s situation</a> (assuming he has his own pages on the shared host that he would be linking to and it isn&#8217;t a shared <strong>page</strong> causing the forced sharing of icons). Here&#8217;s a function to get the most likely <span class="caps">URL</span> of an icon:<br />
<code></p>
<pre>
def getIcoUrl(url):
    """Return the URL of the favourite icon associated with input.
    Returns None if input isn't a valid address."""
    try:
        fp = urllib.urlopen(url)
    except IOError, e:
        if e[1] == "unknown url type":
            # The given URL isn't a web address so adding
            # /favicon.ico won't work. Might as well
            # jump straight to giving up
            return None
    else:
        if fp.headers['content-type'].startswith('text/html') or fp.headers['content-type'].startswith('application/xhtml+xml'):
            data = fp.read()
            fp.close()
            # Try to find any links to shortcut icons
            ICONREGEX = "&lt;link rel="shortcut icon" (?:.*)href=(?:['|"])(.[^'|"|\s]{0,})(?:['|"])(?:.*)&gt;"
            results = re.search(ICONREGEX, data, re.I)
            if results != None:
                # Found the requested URL of the favourite icon
                icourl = results.groups()[0]
                # the base url is already known to be valid, so we
                # don't need to check that the join works.
                # If the icon's url is invalid, we'll let
                # loadResizeImage handle the inability to retrieve it
                return urlparse.urljoin(url, icourl)
    else:
        fp.close()
    # Return the default of url/favicon.ico
    return urlparse.urljoin(url, '/favicon.ico')
</pre>
<p></code><br />
I put that in code and pre blocks in the hope that it would not try to parse it, but the preview doesn&#8217;t look like it is going to work. It isn&#8217;t very hard to write yourself, but hopefully you can guess what it looks like untextiled (the numbered lists used to be comments) or get at the source somehow.<br />
If you want to use this function you&#8217;ll have to rework the file naming since you probably don&#8217;t want to call this function for every request just to see what the cached image is/should be called, but that shouldn&#8217;t be too hard; saving the images as the user inputted <span class="caps">URL</span> instead of the image&#8217;s <span class="caps">URL</span> would eliminate the space saving feature of <a href="http://website/foobar" rel="nofollow">http://website/foobar</a> always pointing to the file http&#8212;-website-favicon.ico for any value of foobar, but if you add in some code to normalise an <span class="caps">URL </span>(like always remove trailing slashes) it shouldn&#8217;t prove too much of a problem since most people would enter the root of their namespace. Or you could go all out and cache the results of this function in some sort of mapping file. &#8211; Testing:<br />
>>>print getIcoUrl(&#8216;http://www.python.org/doc/2.3/modindex.html&#8216;)<br />
&#8216;http://www.python.org/doc/2.3/icons/pyfav.gif&#8217;<br />
>>> print getIcoUrl(&#8216;http://www.google.com&#8216;)<br />
&#8216;http://www.google.com/favicon.ico&#8217; &#8211; Transparency: If the icon is a gif (all I tested with), you seem to be able to get transparency working fine by doing i.save(thumb,&#8216;PNG&#8216;,transparency=i.info[&#8216;transparency&#8216;]) in loadResizeImage() but <span class="caps">ICO</span> files don&#8217;t get a &#8216;transparency&#8217; key in their info dictionary (so you&#8217;d want to add a has_key() check before doing that form of save).<br />
I have no idea really how transparency works in <span class="caps">PIL</span> or file formats in general, so I may be missing something obvious, but getcolors() only shows 4 colors for one of the icons with transparancy problems on your comments (simon.incutio.com/favicon.ico) and it looks like it uses 4 colors so one isn&#8217;t set aside just for transparency and converting to an <span class="caps">RGBA</span> image lists each of the 4 colors with a 4th value of 255 (fully opaque, I assume, but all the same value at any rate). Doing the same with  the python.org gif gives you an extra color <strong>and</strong> lists one of the colors as having a 4th channel of value 0 (vs. 255 for the others) if converted to <span class="caps">RGBA</span>. So <span class="caps">ICO</span> files don&#8217;t support the same way of getting at the transparency info of other files, and I suspect it doesn&#8217;t support transparency on them at all.</link>
]]></content:encoded>
	</item>
	<item>
		<title>By: N/A</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-742</link>
		<dc:creator>N/A</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-742</guid>
		<description>&lt;p&gt;Opps, I messed up the formatting a bit. The else: fp.close() should be indented one block.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Opps, I messed up the formatting a bit. The else: fp.close() should be indented one block.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gilmae</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-743</link>
		<dc:creator>gilmae</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-743</guid>
		<description>&lt;p&gt;Why do you convert it to a &lt;span class="caps"&gt;PNG&lt;/span&gt;? Just leaving it as an .ico will solve your transparency problem.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Why do you convert it to a <span class="caps">PNG</span>? Just leaving it as an .ico will solve your transparency problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sil</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-744</link>
		<dc:creator>sil</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-744</guid>
		<description>&lt;p&gt;gilmae: I&#8217;m not sure that all browsers support &lt;span class="caps"&gt;ICO&lt;/span&gt; files. Mozilla seems to, in my testing, but I wasn&#8217;t sure about Konqueror, Safari, or Opera. If someone can confirm that browsers have as good support for &lt;span class="caps"&gt;ICO&lt;/span&gt; files as they do for &lt;span class="caps"&gt;PNG&lt;/span&gt; then I&#8217;m happy.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>gilmae: I&#8217;m not sure that all browsers support <span class="caps">ICO</span> files. Mozilla seems to, in my testing, but I wasn&#8217;t sure about Konqueror, Safari, or Opera. If someone can confirm that browsers have as good support for <span class="caps">ICO</span> files as they do for <span class="caps">PNG</span> then I&#8217;m happy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sil</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-745</link>
		<dc:creator>sil</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-745</guid>
		<description>&lt;p&gt;gilmae: also, the Python Imaging Library can&#8217;t save &lt;span class="caps"&gt;ICO&lt;/span&gt; files, only read them, it seems.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>gilmae: also, the Python Imaging Library can&#8217;t save <span class="caps">ICO</span> files, only read them, it seems.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gilame</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-746</link>
		<dc:creator>gilame</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-746</guid>
		<description>&lt;p&gt;On Windows, &lt;span class="caps"&gt;IE 5&lt;/span&gt;.0+ and Opera 7 do. Netscape 4.08 doesn&#8217;t. I was too lazy to lean over to the Macintosh on my desk and try it there.&lt;br /&gt;
Saving: Fair enough, that is a bit of a deal breaker.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>On Windows, <span class="caps">IE 5</span>.0+ and Opera 7 do. Netscape 4.08 doesn&#8217;t. I was too lazy to lean over to the Macintosh on my desk and try it there.<br />
Saving: Fair enough, that is a bit of a deal breaker.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: t whalen</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-747</link>
		<dc:creator>t whalen</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-747</guid>
		<description>&lt;p&gt;My favicon.ico is actually a 301 &lt;span class="caps"&gt;HTTP&lt;/span&gt; redirect to favicon.png:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;RewriteRule ^favicon.ico$ http://introvert.net/favicon.png        [R=301]&lt;/code&gt;&lt;/p&gt;

	&lt;p&gt;I&#8217;m pretty sure Python&#8217;s urllib will handle this redirect transparently.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>My favicon.ico is actually a 301 <span class="caps">HTTP</span> redirect to favicon.png:</p>
<p><code>RewriteRule ^favicon.ico$ <a href="http://introvert.net/favicon.png" rel="nofollow">http://introvert.net/favicon.png</a>        [R=301]</code></p>
<p>I&#8217;m pretty sure Python&#8217;s urllib will handle this redirect transparently.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Senji</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-748</link>
		<dc:creator>Senji</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-748</guid>
		<description>&lt;p&gt;Do you cache the icons?&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Do you cache the icons?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Senji</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-749</link>
		<dc:creator>Senji</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-749</guid>
		<description>&lt;p&gt;Oops, how did I doublepost that? :)&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Oops, how did I doublepost that? :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sil</title>
		<link>http://www.kryogenix.org/days/2005/01/31/favatarsUrlparse/comment-page-1#comment-750</link>
		<dc:creator>sil</dc:creator>
		<pubDate>Thu, 01 Jan 1970 01:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.kryogenix.org/adpb/2005/01/31/favatarsUrlparse/#comment-750</guid>
		<description>&lt;p&gt;Senji: yes. See the code for details.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Senji: yes. See the code for details.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
