Skip to content

Instantly share code, notes, and snippets.

@curthard89
Created November 9, 2011 10:51
Show Gist options
  • Save curthard89/1351111 to your computer and use it in GitHub Desktop.
Save curthard89/1351111 to your computer and use it in GitHub Desktop.
- (void)refreshFavicons:(NSArray *)subscriptions
force:(BOOL)flag
{
LOGGED_IN
for( GDataSubscription * subscription in subscriptions )
{
if( ! flag && [[self delegate] respondsToSelector:@selector(gDataController:checkFaviconExistanceForSubscription:)] )
{
if( [[self delegate] gDataController:self
checkFaviconExistanceForSubscription:subscription] )
{
continue;
}
}
[favIconQueue addOperationWithBlock:^(void){
NSString * url = [[subscription identifier] stringByReplacingOccurrencesOfString:@"feed/"
withString:@""];
if( [subscription htmlURL] )
{
url = [subscription htmlURL];
}
url = [[NSURL URLWithString:url] host];
// check the link href
NSError * error;
NSString * html = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",url]]
encoding:NSUTF8StringEncoding
error:&error];
if( [html length] )
{
NSXMLDocument * DOM = [[[NSXMLDocument alloc] initWithXMLString:html
options:NSXMLDocumentTidyHTML
error:&error] autorelease];
NSArray * nodes = [[DOM rootElement] nodesForXPath:@"head/link[@rel='shortcut icon']"
error:&error];
if( [nodes count] )
{
NSString * href = [[[nodes lastObject] attributeForName:@"href"] stringValue];
if( [href rangeOfString:@"http"].location != NSNotFound )
{
url = href;
} else {
url = [NSString stringWithFormat:@"http://%@%@",url,href];
}
} else {
url = [NSString stringWithFormat:@"http://%@/favicon.ico",url];
}
} else {
url = [NSString stringWithFormat:FAVICON_URL,url];
}
NSData * blob = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
dispatch_async( dispatch_get_main_queue(), ^{
NSData * returnData = blob;
if( ! [blob length] )
{
returnData = nil;
}
// tell the delegate we receieved the data
if( [[self delegate] conformsToProtocol:@protocol(GDataControllerDelegate)] )
{
[[self delegate] gDataController:self
didReceieveFaviconData:returnData
forSubscription:subscription];
}
});
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment