Plugins/Twitter Plugin/MGTwitterEngine/MGTwitterStatusesParser.m
author Zachary West <zacw@adium.im>
Thu Nov 19 21:12:23 2009 -0500 (2009-11-19)
changeset 2768 85857106a45e
parent 776 243d6da010b2
child 2914 597b3a38a84a
permissions -rw-r--r--
Implement the Retweet API. This means checking home_timeline and sending proper retweet messages. Fixes #12556.

On an annoying note, home_timeline (despite saying "Returns the 20 most recent statuses, including retweets, posted by the authenticating user and that user's friends") does not include outgoing retweets. This will either be fixed by Twitter quickly, or not. I'm tired of Twitter's inconsistent and buggy API. So, as such, there's currently no way to remove a retweet done by yourself.
     1 //
     2 //  MGTwitterStatusesParser.m
     3 //  MGTwitterEngine
     4 //
     5 //  Created by Matt Gemmell on 11/02/2008.
     6 //  Copyright 2008 Instinctive Code.
     7 //
     8 
     9 #import "MGTwitterStatusesParser.h"
    10 
    11 
    12 @implementation MGTwitterStatusesParser
    13 
    14 
    15 #pragma mark NSXMLParser delegate methods
    16 
    17 
    18 - (void)parser:(NSXMLParser *)theParser didStartElement:(NSString *)elementName 
    19   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    20     attributes:(NSDictionary *)attributeDict
    21 {
    22     //NSLog(@"Started element: %@ (%@)", elementName, attributeDict);
    23     [self setLastOpenedElement:elementName];
    24     
    25     if ([elementName isEqualToString:@"status"]) {
    26         // Make new entry in parsedObjects.
    27         NSMutableDictionary *newNode = [NSMutableDictionary dictionaryWithCapacity:0];
    28         [parsedObjects addObject:newNode];
    29         currentNode = newNode;		
    30     } else if ([elementName isEqualToString:@"user"]) {
    31         // Add a 'user' dictionary to current node.
    32         NSMutableDictionary *newNode = [NSMutableDictionary dictionaryWithCapacity:0];
    33         [currentNode setObject:newNode forKey:elementName];
    34         currentNode = newNode;
    35 	} else if ([elementName isEqualToString:@"retweeted_status"]) {
    36         // Add a 'retweeted_status' dictionary to current node.
    37         NSMutableDictionary *newNode = [NSMutableDictionary dictionaryWithCapacity:0];
    38         [currentNode setObject:newNode forKey:elementName];
    39         currentNode = newNode;		
    40     } else if (currentNode) {
    41         // Create relevant name-value pair.
    42         [currentNode setObject:[NSMutableString string] forKey:elementName];
    43     }
    44 }
    45 
    46 
    47 - (void)parser:(NSXMLParser *)theParser foundCharacters:(NSString *)characters
    48 {
    49     //NSLog(@"Found characters: %@", characters);
    50     // Append found characters to value of lastOpenedElement in currentNode.
    51     if (lastOpenedElement && currentNode) {
    52         [[currentNode objectForKey:lastOpenedElement] appendString:characters];
    53     }
    54 }
    55 
    56 
    57 - (void)parser:(NSXMLParser *)theParser didEndElement:(NSString *)elementName 
    58   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    59 {
    60     [super parser:theParser didEndElement:elementName namespaceURI:namespaceURI qualifiedName:qName];
    61     
    62     if ([elementName isEqualToString:@"user"] || [elementName isEqualToString:@"retweeted_status"]) {
    63         currentNode = [parsedObjects lastObject];
    64     } else if ([elementName isEqualToString:@"status"]) {
    65         [self addSource];
    66         currentNode = nil;
    67     }
    68 }
    69 
    70 
    71 @end