2011年9月14日水曜日

メモ的: Twitterのhome_timelineの情報を無理矢理4storeに入れてみる(いろんな意味でいろいろ間違ってるとは思う)

  • OAuthConsumerフレームワーク('#import <OAuthConsumer/OAuthConsumer.h>')についてはこちら
  • 4storeのクライアントライブラリ(っぽいなにか)('#import "FourStore.h"')についてはこちら

    #import "TwTurtleAppDelegate.h"
    #import <OAuthConsumer/OAuthConsumer.h>
    #import "FourStore.h"

    @implementation TwTurtleAppDelegate

    @synthesize window;

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
    OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"XXXXXXXXXXXXXXXXXXXXXX"
    secret:@"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"];

    OAToken *accessToken= [[OAToken alloc] initWithKey:@"XXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    secret:@"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"];

    NSURL *url= [NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.xml"];
    OAMutableURLRequest *requestWithBodyParams = [[[OAMutableURLRequest alloc] initWithURL:url
    consumer:consumer
    token:accessToken
    realm:nil
    signatureProvider:nil] autorelease];

    OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];

    [fetcher fetchDataWithRequest:requestWithBodyParams
    delegate:self
    didFinishSelector:@selector(apiTicket:didFinishWithData:)
    didFailSelector:@selector(apiTicket:didFailWithError:)];

    }
    - (void)fourstoreWithTurtle:(NSString *)turtle
    {
    FourStore *store= [[FourStore alloc] initWithEndpoint:@"http://localhost:8000/sparql/"];
    [store addPrefix:@"tsp" uri:@"http://example.org/twitter/status/predicate/"];
    [store addPrefix:@"tu" uri:@"http://example.org/twitter/users/"];

    NSString *message0= [store deleteWithGraph:@"http://example.org/twitter/status/"];
    NSLog(@"message0 %@", message0);


    NSString *message1= [store addTurtle:turtle graph:@"http://example.org/twitter/status/"];
    NSLog(@"message1 %@", message1);

    NSString *message2= [store queryWithString:
    @"SELECT ?s ?p ?o \nWHERE {\nGRAPH <http://example.org/twitter/status/> {\n?s ?p ?o .\n}\n}\n"];
    NSLog(@"message2 \n%@", message2);

    }
    - (NSString *)createTurtleWithXMLData:(NSData *)data
    {
    NSMutableString *turtle= [NSMutableString string];

    NSXMLDocument *document = [[[NSXMLDocument alloc] initWithData:data
    options:NSXMLDocumentTidyHTML
    error:NULL] autorelease];
    NSError *error= nil;
    NSArray *nodes = [document nodesForXPath:@"//status" error:&error];
    NSEnumerator *nodesEnum= [nodes objectEnumerator];
    NSXMLNode *node= nil;
    while(node= [nodesEnum nextObject]) {
    NSArray *idnodes= [node nodesForXPath:@"id" error:&error];
    if ([idnodes count]==0) continue;
    NSString *idString= [[idnodes objectAtIndex:0] stringValue];
    NSEnumerator *childrenEnum= [[node children] objectEnumerator];
    NSXMLNode *child=nil;
    while(child= [childrenEnum nextObject]) {
    NSString *name= [child name];
    if ([name isEqualToString:@"id"])
    continue;
    NSString *value= nil;
    if ([name isEqualToString:@"retweeted_status"]){
    NSArray *cidnodes= [child nodesForXPath:@"id" error:&error];
    if ([cidnodes count]==0) continue;
    NSString *cidString= [[cidnodes objectAtIndex:0] stringValue];
    value= [NSString stringWithFormat:@"<%@>", cidString];
    } else if ([name isEqualToString:@"user"]){
    NSArray *cidnodes= [child nodesForXPath:@"id" error:&error];
    if ([cidnodes count]==0) continue;
    NSString *cidString= [[cidnodes objectAtIndex:0] stringValue];
    value= [NSString stringWithFormat:@"<tu:%@>", cidString];
    } else {
    if ([[child stringValue] length]==0) continue;
    value= [child stringValue];
    value= [value stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    value= [value stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
    value= [NSString stringWithFormat:@"\"%@\"", value];
    }

    [turtle appendFormat:@"<%@> tsp:%@ %@ .\n", idString, name, value];
    }
    }


    return turtle;
    }
    - (void)apiTicket:(OAServiceTicket *)ticket
    didFinishWithData:(NSData *)data
    {

    if (0)
    NSLog(@"didFinish %@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);

    NSString *turtle= [self createTurtleWithXMLData:data];

    if (0)
    NSLog(@"\n%@", turtle);

    [self fourstoreWithTurtle:turtle];
    }
    - (void)apiTicket:(OAServiceTicket *)ticket
    didFailWithError:(NSError *)error
    {

    NSLog(@"%@", error);
    }
    @end



    やってみてアレコレ悩んだ足跡.
  • 0 件のコメント: