Permalink
Browse files

Added a LibXML parser for the Social Graph API requests (friends/ids …

…& followers/ids)
  • Loading branch information...
tarasis committed Mar 21, 2010
1 parent e62712c commit e744c0b80a6efe2b382e5b95e837d279275ecfa3
View
@@ -52,8 +52,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
//NSLog(@"getRecentlyUpdatedFriendsFor: connectionIdentifier = %@", [twitterEngine getRecentlyUpdatedFriendsFor:nil startingAtPage:0]);
//NSLog(@"getFollowersIncludingCurrentStatus: connectionIdentifier = %@", [twitterEngine getFollowersIncludingCurrentStatus:YES]);
//NSLog(@"getUserInformationFor: connectionIdentifier = %@", [twitterEngine getUserInformationFor:TESTING_PRIMARY_USER]);
- //NSLog(@"getFriendIDsFor: connectionIdentifier = %@", [twitterEngine getFriendIDsFor:@"duemoko" startingFromCursor:-1]);
- //NSLog(@"getFollowerIDsFor: connectionIdentifier = %@", [twitterEngine getFollowerIDsFor:username startingFromCursor:-1]);
// Direct Message methods:
//NSLog(@"getDirectMessagesSinceID: connectionIdentifier = %@", [twitterEngine getDirectMessagesSinceID:0 startingAtPage:0]);
@@ -93,6 +91,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Help methods:
//NSLog(@"testService: connectionIdentifier = %@", [twitterEngine testService]);
+
+ // Social Graph methods
+ //NSLog(@"getFriendIDsFor: connectionIdentifier = %@", [twitterEngine getFriendIDsFor:@"duemoko" startingFromCursor:-1]);
+ //NSLog(@"getFollowerIDsFor: connectionIdentifier = %@", [twitterEngine getFollowerIDsFor:username startingFromCursor:-1]);
#if YAJL_AVAILABLE
// Search method
View
@@ -30,6 +30,7 @@
#import "MGTwitterMessagesLibXMLParser.h"
#import "MGTwitterUsersLibXMLParser.h"
#import "MGTwitterMiscLibXMLParser.h"
+ #import "MGTwitterSocialGraphLibXMLParser.h"
#else
#import "MGTwitterStatusesParser.h"
#import "MGTwitterUsersParser.h"
@@ -682,6 +683,11 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
connectionIdentifier:identifier requestType:requestType
responseType:responseType URL:URL];
break;
+ case MGTwitterSocialGraph:
+ [MGTwitterSocialGraphLibXMLParser parserWithXML:xmlData delegate:self
+ connectionIdentifier:identifier requestType:requestType
+ responseType:responseType URL:URL];
+ break;
default:
break;
}
@@ -28,6 +28,7 @@
C9EF98A10D68AB970041A17E /* NSString+UUID.m in Sources */ = {isa = PBXBuildFile; fileRef = C9EF98A00D68AB970041A17E /* NSString+UUID.m */; };
C9FEB12C0D6095AF009AA322 /* MGTwitterStatusesParser.m in Sources */ = {isa = PBXBuildFile; fileRef = C9FEB12B0D6095AF009AA322 /* MGTwitterStatusesParser.m */; };
D47CF9B511541D0A00E07931 /* MGTwitterSocialGraphParser.m in Sources */ = {isa = PBXBuildFile; fileRef = D47CF9B411541D0A00E07931 /* MGTwitterSocialGraphParser.m */; };
+ D47CFCA9115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.m in Sources */ = {isa = PBXBuildFile; fileRef = D47CFCA8115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -92,6 +93,8 @@
C9FEB12B0D6095AF009AA322 /* MGTwitterStatusesParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGTwitterStatusesParser.m; sourceTree = "<group>"; };
D47CF9B311541D0A00E07931 /* MGTwitterSocialGraphParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGTwitterSocialGraphParser.h; sourceTree = "<group>"; };
D47CF9B411541D0A00E07931 /* MGTwitterSocialGraphParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGTwitterSocialGraphParser.m; sourceTree = "<group>"; };
+ D47CFCA7115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGTwitterSocialGraphLibXMLParser.h; sourceTree = "<group>"; };
+ D47CFCA8115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGTwitterSocialGraphLibXMLParser.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -217,6 +220,8 @@
44F7518C0E47DDD500858A1B /* MGTwitterMessagesLibXMLParser.m */,
44F751880E47DDD500858A1B /* MGTwitterMiscLibXMLParser.h */,
44F7518A0E47DDD500858A1B /* MGTwitterMiscLibXMLParser.m */,
+ D47CFCA7115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.h */,
+ D47CFCA8115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.m */,
);
name = "Twitter LibXML Parsers";
sourceTree = "<group>";
@@ -351,6 +356,7 @@
44F751930E47DDD500858A1B /* MGTwitterStatusesLibXMLParser.m in Sources */,
44F751A70E47DEC400858A1B /* NSData+Base64.m in Sources */,
D47CF9B511541D0A00E07931 /* MGTwitterSocialGraphParser.m in Sources */,
+ D47CFCA9115567ED00E07931 /* MGTwitterSocialGraphLibXMLParser.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,19 @@
+//
+// MGTwitterSocialGraphLibXMLParser.h
+// MGTwitterEngine
+//
+// Created by Robert McGovern on 2010/03/20.
+// Copyright 2010 Tarasis. All rights reserved.
+//
+
+#import "MGTwitterEngineGlobalHeader.h"
+
+#import "MGTwitterLibXMLParser.h"
+
+@interface MGTwitterSocialGraphLibXMLParser : MGTwitterLibXMLParser {
+ NSMutableArray * twitterIDs;
+}
+
+- (NSDictionary *)_socialGraphDictionaryForNodeWithName:(const xmlChar *)parentNodeName;
+
+@end
@@ -0,0 +1,99 @@
+//
+// MGTwitterSocialGraphLibXMLParser.m
+// MGTwitterEngine
+//
+// Created by Robert McGovern on 2010/03/20.
+// Copyright 2010 Tarasis. All rights reserved.
+//
+
+#import "MGTwitterSocialGraphLibXMLParser.h"
+
+
+@implementation MGTwitterSocialGraphLibXMLParser
+- (void)parse
+{
+ int readerResult = xmlTextReaderRead(_reader);
+ if (readerResult != 1)
+ return;
+
+ int nodeType = xmlTextReaderNodeType(_reader);
+ const xmlChar *name = xmlTextReaderConstName(_reader);
+
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
+
+ while (! (nodeType == XML_READER_TYPE_END_ELEMENT))
+ {
+ //NSLog(@"name is: %@", [NSString stringWithUTF8String:(const char *)name]);
+ if (nodeType == XML_READER_TYPE_ELEMENT)
+ {
+ if (xmlStrEqual(name, BAD_CAST "ids"))
+ {
+ [dictionary addEntriesFromDictionary:[self _socialGraphDictionaryForNodeWithName:name]];
+ }
+ else if (xmlStrEqual(BAD_CAST "previous_cursor", name) || xmlStrEqual(BAD_CAST "next_cursor", name))
+ {
+ // process element as a string -- API calls like friendships/exists.xml just return <friends>false</friends> or <friends>true</friends>
+ NSString *string = [self _nodeValueAsString];
+ if (string)
+ {
+ [dictionary setObject:string forKey:[NSString stringWithUTF8String:(const char *)name]];
+ }
+ }
+
+ }
+
+ // advance reader
+ int readerResult = xmlTextReaderRead(_reader);
+ if (readerResult != 1)
+ {
+ break;
+ }
+ nodeType = xmlTextReaderNodeType(_reader);
+ name = xmlTextReaderConstName(_reader);
+ }
+
+ [dictionary setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
+ [parsedObjects addObject:dictionary];
+}
+
+- (NSDictionary *)_socialGraphDictionaryForNodeWithName:(const xmlChar *)parentNodeName
+{
+ if (xmlTextReaderIsEmptyElement(_reader))
+ return nil;
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
+ twitterIDs = [NSMutableArray arrayWithCapacity:0];
+
+ int readerResult = xmlTextReaderRead(_reader);
+ if (readerResult != 1)
+ return nil;
+ int nodeType = xmlTextReaderNodeType(_reader);
+ const xmlChar *name = xmlTextReaderConstName(_reader);
+ while (! (nodeType == XML_READER_TYPE_END_ELEMENT && xmlStrEqual(parentNodeName, name)))
+ {
+ if (nodeType == XML_READER_TYPE_ELEMENT)
+ {
+ //NSLog(@" name is: %@", [NSString stringWithUTF8String:(const char *)name]);
+ // process element as an integer
+ NSNumber *number = [self _nodeValueAsInt];
+ if (number)
+ {
+ //[dictionary setObject:number forKey:[NSString stringWithUTF8String:(const char *)name]];
+ [twitterIDs addObject:number];
+ }
+ }
+
+ // advance reader
+ int readerResult = xmlTextReaderRead(_reader);
+ if (readerResult != 1)
+ break;
+ nodeType = xmlTextReaderNodeType(_reader);
+ name = xmlTextReaderConstName(_reader);
+ }
+
+ [dictionary setObject:twitterIDs forKey:[NSString stringWithUTF8String:(const char *)name]];
+
+ return dictionary;
+}
+
+
+@end

0 comments on commit e744c0b

Please sign in to comment.