夢見るポテト

色々きになったことを書いてこうと思う

ニコニコ生放送の開始お知らせアプリを作りたくて

※注意これは書きかけのプログラムです動かないです※

久しぶりに少しニコニコ生放送を見てたら、そういや放送開始が通知センターに出てくれたら
ありがたいなぁと、ふと思い(なぜか)作りたいとポチポチコードを打っていたのだが
一応メモ代わりに残しとこうと思います。

ログインまではできるようになってチケットや接続URLとポートとスレッドを取り出すことは
できたんだけど、それらをどのように送信するかはXMLSocketでやらなくちゃいけないそうなので
まだわからないが頑張ってみよう

//
//  main.m
//  niconama
//
//  Created by heart on 2013/12/10.
//  Copyright (c) 2013年 heart. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    NSString* username;
    NSString* password;
    
    username = @"メールアドレス";
    password = @"パスワード";
    
    
    //NSURLConnection* connection;
    NSURL* url = [NSURL URLWithString:@"https://secure.nicovideo.jp/secure/login"];
    NSMutableURLRequest* req = [[NSMutableURLRequest alloc] initWithURL:url];
    
    NSString* data = [[NSString alloc] initWithFormat:@"site=nicolive_antenna&mail=%@&password=%@",username,password];
    
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSHTTPURLResponse* httpresponse;
    NSError *err;
    
    NSData *contents = [NSURLConnection sendSynchronousRequest:req returningResponse:&httpresponse error:&err];
    
    //connection = [[NSURLConnection alloc] initWithRequest:req delegate:0];
    
    if (contents)
    {
       // NSLog(@"Login...OK!");
        
        //NSString *responseXML =[[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
        
        
        NSXMLDocument *doc;
        doc = [[NSXMLDocument alloc]initWithData:contents options:0 error:NULL];
        
        
        NSArray *nodes;
        //nodes = [doc nodesForXPath:@"nicovideo_user_response" error:NULL];
        
        
        
            if ((nodes = [doc nodesForXPath:@"nicovideo_user_response/ticket" error:NULL])){
            
            
                NSXMLNode* snode;
                snode = [nodes objectAtIndex:0];
                
                NSString* ticket;
                ticket = [snode stringValue];
                
                //NSLog(@"%@",ticket);
                
                
                NSURL* url2 = [NSURL URLWithString:@"http://live.nicovideo.jp/api/getalertstatus"];
                NSMutableURLRequest* req2 = [[NSMutableURLRequest alloc] initWithURL:url2];
                
                NSString* data2 = [[NSString alloc] initWithFormat:@"ticket=%@",ticket];
                
                [req2 setHTTPMethod:@"POST"];
                [req2 setHTTPBody:[data2 dataUsingEncoding:NSUTF8StringEncoding]];
                
                NSHTTPURLResponse* httpresponse2;
                NSError *err2;
                
                NSData *contents2 = [NSURLConnection sendSynchronousRequest:req2 returningResponse:&httpresponse2 error:&err2];
                
                
                if (contents2) {
                    
                    //NSString *str = [[NSString alloc] initWithData:contents2 encoding:NSUTF8StringEncoding];
                    
                    //NSLog(@"%@",str);
                    
                    NSArray* node3;
                    
                    NSXMLDocument* doc3 = [[NSXMLDocument alloc] initWithData:contents2 options:0 error:NULL];
                    
                    if ((node3 = [doc3 nodesForXPath:@"getalertstatus/ms/addr" error:NULL])){
                        
                        NSXMLNode* snode3;
                        snode3 = [node3 objectAtIndex:0];
                        NSString* addr;
                        addr = [snode3 stringValue];
                        
                        if ((node3 = [doc3 nodesForXPath:@"getalertstatus/ms/port" error:NULL])){
                            
                        
                        
                            NSXMLNode* snode4;
                            snode4 = [node3 objectAtIndex:0];
                            NSString* port;
                            port = [snode4 stringValue];
                            
                            
                        
                        
                            
                        
                            if ((node3 = [doc3 nodesForXPath:@"getalertstatus/ms/thread" error:NULL])) {
                                
                                NSXMLNode* snode5;
                                snode5 = [node3 objectAtIndex:0];
                                NSString* thread;
                                thread = [snode5 stringValue];
                            
                               //NSLog(@"%@",addr);
                                //NSLog(@"%@",port);
                                //NSLog(@"%@",thread);
                                
                                
                                NSString *str = [NSString stringWithFormat:@"<thread thread=\"%@\" version=\"20061206\" res_from=\"-1000\" />",thread];
                                
                                NSHost* host = [NSHost hostWithName:addr];
                                
                                NSInputStream* indata;
                                NSOutputStream* outdata;
                                indata = [[NSInputStream alloc]
                                
                                [NSInputStream retain];
                                [NSOutputStream retain];
                                
                                
                                [NSInputStream setDelegate:self];
                                [NSOutputStream setDelegate:self];
                                [NSInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                                [NSOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                                
                                [NSInputStream open];
                                [NSOutputStream open];
                                
                            }
                        }
                    
                    
                    
                    
                    }else{
                        NSLog(@"失敗2");
                    }
                    
                    
                } else {
                    NSLog(@"失敗");
                }
                
                
            
            }else{
                NSLog(@"not find");
            }
        
    
        

    
        
        
        //NSLog(@"%@",responseXML);
        
        
    
    }
    else
    {
        NSLog(@"connection is error");
        
    }
}