コード例 #1
0
ファイル: Program.cs プロジェクト: inohiro/HatenaNetfxLib
        static void Main( string[] args )
        {
            User user = new User();
            user.Username = "******";
            user.Password = "******";

            BlogCollection collection = new BlogCollection();
            collection.Get( user );

            //Entry entry = new Entry( "Hello, World", "This is Sample Text. It was set in Constractor" );

            //if( entry.Create( info ) )
            //{
            //    Console.WriteLine( "Success!!" );
            //}
            //else
            //{
            //    Console.WriteLine( "Fail..." );
            //    if( entry.ErrorInformation != null )
            //    {
            //        Console.WriteLine( entry.ErrorInformation.Title );
            //    }
            //}

            Console.ReadKey();
        }
コード例 #2
0
        public List<Entry> Get( User user )
        {
            StringBuilder query = new StringBuilder();
            query.AppendFormat( "http://d.hatena.ne.jp/{0}/atom/blog", user.Username );

            this.get( query.ToString(), user.WsseHeader );
            return new List<Entry>();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: inohiro/HatenaNetfxLib
        static void Main( string[] args )
        {
            //if( args.Length < 2 )
            //{
            //    Console.WriteLine( "Invalid Arguments..." );
            //    Console.ReadKey();
            //    Environment.Exit( 0 );
            //}

            ArgProcessor ap = new ArgProcessor( args );

            User user = new User();
            user.Username = ap.UserName;
            user.Password = ap.Password;

            if( ap.IsDirectoryUpload )
            {
                string[] files = Directory.GetFiles( ap.DirectoryName );
                foreach( string file in files )
                {
                    Photo photo = new Photo();
                    if( ap.IsTitle ) { photo.Title = ap.Title; }else { photo.Title = file; }
                    if( ap.IsFolder ) { photo.FolderName = ap.FolderName; }
                    photo.Generator = GENERATOR_NAME;

                    if( Resizer.ResizeImage( file, ap.ResizeSize ) )
                    {
                        photo.ImageFilePath = "tmp_img.jpg";
                        Console.WriteLine( photo.Post( user ) );
                    }
                    else
                    {
                        Console.WriteLine( "アップロードに失敗しました" );
                    }
                }
            }
            else
            {
                Photo photo = new Photo();
                if( ap.IsTitle ) { photo.Title = ap.Title; }else { photo.Title = ap.ImageFilePath; }
                if( ap.IsFolder ) { photo.FolderName = ap.FolderName; }
                photo.Generator = GENERATOR_NAME;

                if( Resizer.ResizeImage( ap.ImageFilePath, ap.ResizeSize ) )
                {
                    photo.ImageFilePath = "tmp_img.jpg";
                    Console.WriteLine( photo.Post( user ) );
                }
                else
                {
                    Console.WriteLine( "アップロードに失敗しました" );
                }
            }

            Console.ReadKey();
        }
コード例 #4
0
ファイル: Photo.cs プロジェクト: inohiro/HatenaNetfxLib
 public string Post( User userInfo )
 {
     var result = this.post( userInfo );
     if( result )
     {
         return String.Format( "Success!!: {0}\n", this.Title );
     }
     else
     {
         return String.Format( "Faile...: {0}\n", this.Title );
     }
 }
コード例 #5
0
 public List<Entry> Get( User user, int page )
 {
     StringBuilder query = new StringBuilder();
     if( page > 0 )
     {
         query.AppendFormat( "http://d.hatena.ne.jp/{0}/atom/blog/?page={1}", user.Username, page );
     }
     else
     {
         Exception ex = new Exception( "取得するページは1以上である必要があります。" );
         throw ex;
     }
     this.get( query.ToString(), user.WsseHeader );
     return new List<Entry>();
 }
コード例 #6
0
ファイル: Photo.cs プロジェクト: inohiro/HatenaNetfxLib
        private bool post( User userInfo )
        {
            Console.WriteLine( "Uploading...: {0}", this.Title );

            // Base64 Encoding
            String encodedImg;
            if( this.ImageFilePath != null )
            {
                byte[] bytes;

                try
                {
                    using( FileStream filestream = new FileStream( this.ImageFilePath, FileMode.Open, FileAccess.Read ) )
                    {
                        bytes = new byte[filestream.Length];
                        int readBytes = filestream.Read( bytes, 0, ( Int32 )filestream.Length );
                    }
                    encodedImg = System.Convert.ToBase64String( bytes );
                }
                catch( FileNotFoundException )
                {
                    Console.WriteLine( "ファイルが見つかりませんでした\n" );
                    return false;
                }
                catch( FileLoadException )
                {
                    Console.WriteLine( "データのロードに失敗しました\n" );
                    return false;
                }

                StringBuilder xml = new StringBuilder();
                xml.Append( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" );
                xml.Append( "<entry xmlns=\"http://purl.org/atom/ns#\">" );
                xml.Append( String.Format( "<title>{0}</title>", this.Title ) );
                xml.Append( String.Format( "<content type=\"image/png\" mode=\"base64\">{0}</content>", encodedImg ) );
                xml.Append( String.Format( "<dc:subject>{0}</dc:subject>", this.FolderName ) );
                xml.Append( String.Format( "<generator>{0}</generator></entry>", this.FolderName ) );

                HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( "http://f.hatena.ne.jp/atom/post" );
                request.Method = "POST";
                request.Headers.Add( "X-WSSE", userInfo.WsseHeader );
                request.ContentType = "application/x.atom+xml";

                Stream requestream = request.GetRequestStream();
                byte[] data = Encoding.UTF8.GetBytes( xml.ToString() );
                requestream.Write( data, 0, data.Length );
                requestream.Close();

                HttpWebResponse response = ( HttpWebResponse )request.GetResponse();
                if( response.StatusCode == HttpStatusCode.Created )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else // ImageFilePath -> null
            {
                return false;
            }
        }