0
votes

I've spent about 16 hours researching and attempting different code changes, but cannot figure this one out. I have an iOS app that consumes a website using ASIHTTPrequest:

-(void)refresh{
    NSURL *url = [NSURL URLWithString@"http://undignified.podbean.com/"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSString *response = [request responseString];
    NSLog(@"%@",response];
 }

The above code returns the website source and spits it out to the console via NSLog. The goal I'm trying to achieve from this is search through the 'responseString' for URLs ending in *.mp3, load those into an array and finally load the mp3 URLs into a UITableView.

To summarize:

  1. Consuming website data with ASIHTTPRequest
  2. Trying to search through the responseString for all links that have *.mp3 extensions and load them into an array.
  3. Add parsed links to UITableView.

I think at this junction I have attempted too many things to make any sound judgements at this point. Any suggestions or nudges in the right direction would be greatly appreciative.

Below is an example of the response (HTML). Please note this is only a snippet as the entire HTML source is rather large, but this includes a section to the mp3 files:

a href="http://undignified.podbean.com/mf/web/ih2x8r/UndignifiedShow01.mp3"         target="new"><img src="http://www.podbean.com/wp-content/plugins/podpress/images/audio_mp3_button.png" border="0" align="top" class="podPress_imgicon" alt="icon for podbean" /></a> &nbsp;Standard Podcasts [00:40:24m]: <a href="javascript:void(null);" onclick="podPressShowHidePlayerDiv('podPressPlayerSpace_2649518', 'mp3Player_2649518_0', '300:30', 'http://undignified.podbean.com/mf/play/ih2x8r/UndignifiedShow01.mp3'); return false;"><span id="podPressPlayerSpace_2649518_label_mp3Player_2649518_0">Play Now</span></a> | <a href="javascript:void(null);" onclick="window.open ('http://undignified.podbean.com/wp-content/plugins/podpress/podpress_backend.php?podPressPlayerAutoPlay=yes&amp;standalone=yes&amp;action=showplayer&amp;pbid=0&amp;b=458211&amp;id=2649518&amp;filename=http://undignified.podbean.com/mf/play/ih2x8r/UndignifiedShow01.mp3', 'podPressPlayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=660,height=360'); return false;">Play in Popup</a> | <a href="http://www.podbean.com/podcast-download?b=458211&f=http://undignified.podbean.com/mf/web/ih2x8r/UndignifiedShow01.mp3" target="26108">Download</a> | <a href="http://www.podbean.com/podcast-players?b=458211&p=2649518&f=http://undignified.podbean.com/mf/play/ih2x8r/UndignifiedShow01.mp3" target="38148">Embeddable Player</a> | <a>Hits (214)</a><br/
3
This question would be much easier to answer if you were to post either the actual URL you are trying to parse or a good sample of the response.NJones
Added URL and sample of the response.Fostenator
You tagged your question with ASIHTTPRequest and you also show a snippet of your request initializer but then you mention NSURLConnection which is not used for ASIHTTPRequest - please straighten that out. For the potential timing issue, measure but do not assume.Till
I've cleaned up the question a littl bit and removed the part that Till found confusing.Fostenator

3 Answers

0
votes

I really don't like answers that say "Just don't do it that way." But... Just don't do it that way.

It is possible to extract all of the links to mp3s in the html from the address you posted. But this is almost always the wrong way to approach things, and this case is no exception.

Essentially what it seems you are trying to do is create a podcaster client. You should give some thought as to how others have handled this type of use case before. Generally a podcast will have an associated RSS feed that outlines exactly the data you are looking for, again your podcast is no exception. If one simply navigates to the link supplied in your question and then looks around the page for either "subscribe to podcast" or "RSS" they will find the link that leeds to the RSS feed: http://undignified.podbean.com/feed/. This address leads to the XML which contains the items of the podcasts.

This document, unlike the document returned by your original address, is valid XML meaning it can be parsed with an NSXMLParser. NSXMLParser is very powerful & flexible. But a little hard to get started with. Here is some sample code for an NSXMLParser subclass which acts as it's own delegate.

UnDigParser.h

#import <Foundation/Foundation.h>
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate>
@property (readonly) NSArray *links;
@end

UnDigParser.m

#import "UnDigParser.h"
@implementation UnDigParser{
    NSMutableArray *_links;
}
@synthesize links = _links;
-(void)parserDidStartDocument:(NSXMLParser *)parser{
    _links = [[NSMutableArray alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"enclosure"]){
        NSString *link = [attributeDict objectForKey:@"url"];
        if (link){
            [_links addObject:link];
        }
    }
}
-(BOOL)parse{
    self.delegate = self;
    return [super parse];
}
@end

This code can be tested like so:

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    NSURL *url = [NSURL URLWithString:@"http://undignified.podbean.com/feed/"];
    // This is a sync call thus the background thread
    UnDigParser *parser = [[UnDigParser alloc] initWithContentsOfURL:url];
    [parser parse];
    NSLog(@"links:%@",parser.links);
});

The logged output was:

links:(
    "http://undignified.podbean.com/mf/feed/cfdayc/UndignifiedShow03.mp3",
    "http://undignified.podbean.com/mf/feed/wbbpjw/UndignifiedShow02Final.mp3",
    "http://undignified.podbean.com/mf/feed/ih2x8r/UndignifiedShow01.mp3",
    "http://undignified.podbean.com/mf/feed/t4x54d/UndignifiedShow00.mp3"
)

That should be enough to get you started.

0
votes

I would do this in two steps:

  1. Get all href values

  2. Use regular expressions to check for a valid url ending in .mp3

BTW, I think ASIHTTPRequest is getting outdated. And if you only use it to fetch HTML, I suggest you look into the build in methods from the iOS framework to do just that.

0
votes

Using requestDidReceiveResponseHeadersSelector and get file name from header, trying print all key and value from NSDictionary header.