GithubHelp home page GithubHelp logo

Comments (4)

mwaterfall avatar mwaterfall commented on July 29, 2024

The parser doesn't currently detect images within feed items. This is planned for the future though.

The best thing to do now is to use an NSScanner and scan the content/summary of the feed item and look for "<img" and then "src=", and scan the image url that way.

from mwfeedparser.

paul5 avatar paul5 commented on July 29, 2024

Yes I use this code to get the first image but it doesn't work fine... For example It can't retrieve the 1st image in this code:

[p] [p][img class="aligncenter size-full wp-image-21789" title="mac-app-store_t" src="http://static.slidetomac.com/wp-content/uploads/2011/07/mac-app-store_t.jpg" alt="" width="507" height="300" /][/p] [p][span id="more-21780"][/span][/p] [p]Solo qualche giorno fa Apple....

NOTE: To show you the code I have replaced < and > with [ and ]

My function is:

  • (NSString *)getFirstImage:(NSString *)htmlString{

    NSString *urlImage=nil;
    NSScanner *theScanner = [NSScanner scannerWithString:htmlString];
    // find start of IMG tag
    [theScanner scanUpToString:@"<img" intoString:nil];
    do {
    [theScanner scanUpToString:@"src" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@""'"];
    [theScanner scanUpToCharactersFromSet:charset intoString:nil];
    [theScanner scanCharactersFromSet:charset intoString:nil];
    [theScanner scanUpToCharactersFromSet:charset intoString:&urlImage];

    if(
       [urlImage rangeOfString:@"feedads.g.doubleclick.net"].location == NSNotFound ) return urlImage;
    

    }while (![theScanner isAtEnd]);

    if([theScanner isAtEnd] ) return nil;
    else return urlImage;

}

What's wrong in my code?

from mwfeedparser.

mwaterfall avatar mwaterfall commented on July 29, 2024

I can't see anything wrong with the code at a quick glance. You should use the debugging features in Xcode to step through and see what's going on with your code. Then you'll be able to see what's happening.

Another note, you should probably set the scanner to not skip any whitespace characters (set charactersToBeSkipped to nil), and also I'd scan for " src=" instead of just "src", it's safer.

Best of luck with figuring it out.

from mwfeedparser.

javck avatar javck commented on July 29, 2024

I have made this feather that is getting the first image in the item.

I made my modification Below:

MWFeedItem.h
NSData* firstImg;
@Property (nonatomic, retain) NSData* firstImg;
MWFeedItem.m
add synthesize and dealoc

RootViewController.m
- (NSString *)getFirstImage:(NSString *)htmlString{

NSScanner *theScanner;
NSString *text = nil;

theScanner = [NSScanner scannerWithString: htmlString];

// find start of tag
[theScanner scanUpToString: @"<img src=\"" intoString: NULL];
if ([theScanner isAtEnd] == NO) {
    NSInteger newLoc = [theScanner scanLocation] + 10;
    [theScanner setScanLocation: newLoc];

    // find end of tag
    [theScanner scanUpToString: @"\"" intoString: &text];
}

return text;

}

  • (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item {
    NSLog(@"Parsed Feed Item: “%@”", item.title);

    NSString* str_imageUrl = [self getFirstImage:item.summary];

    item.firstImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:str_imageUrl]];

    if (item) [parsedItems addObject:item];
    }

Hope can help...

from mwfeedparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.