Beispiel #1
0
        static public Newtonsoft.Json.Linq.JContainer ReadBook(string isbncode)
        {
            /* API詳細 http://blog.livedoor.jp/dankogai/archives/51227901.html
             * 個人サービスなので接続は保障しかねる
             * 楽天APIの方が楽そう
             */
            string url = "http://api.dan.co.jp/asin/" + Amazon.isbn2asin(isbncode) + "/amazon";

            string jsondata = "";

            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                System.IO.Stream     st = wc.OpenRead(url);

                System.IO.StreamReader sr = new System.IO.StreamReader(st, System.Text.Encoding.GetEncoding("UTF-8"));

                jsondata = sr.ReadToEnd();

                st.Close();
                wc.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("書籍情報取得エラー " + ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Application.Exit();
            }

            /* 厳密にはJSONデータではなくJSONPデータなので
             * "amazon(JSONデータ);\n" の形式がjsondata変数に入ってる
             * よって不要な部分をsubstring.
             */
            jsondata = jsondata.Substring(0, jsondata.Length - 3).Substring(7);

            Newtonsoft.Json.Linq.JContainer jobj = Newtonsoft.Json.JsonConvert.DeserializeObject <Newtonsoft.Json.Linq.JContainer>(jsondata);

            return(jobj);
        }
Beispiel #2
0
        private void BarcodeForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 2)
            {
                //2がバーコード読み取り開始
                this.isReading  = true;
                this.strBarcode = "";    //初期化
            }
            else if (e.KeyChar == 3)
            {
                //3がバーコード読み取り終了
                this.isReading = false;

                if (strBarcode.Length != 13)
                {
                    MessageBox.Show("ISBNコードではないようです。", "新規登録/編集", MessageBoxButtons.OK);
                    return;
                }

                if (strBarcode.Substring(0, 2) == "19")
                {
                    MessageBox.Show("本上段のバーコードを読み取ってください。", "新規登録/編集", MessageBoxButtons.OK);
                    return;
                }

                if (((BookForm1)this.Owner).dataTable.Rows.Find(strBarcode) != null)
                {
                    MessageBox.Show("すでにその本は登録されてます。", "新規登録/編集", MessageBoxButtons.OK);
                    return;
                }

                if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
                {
                    MessageBox.Show("ネットワークに接続されてないので本情報を取得できません", "新規登録/編集", MessageBoxButtons.OK);
                    return;
                }

                //Amazon APIに問い合わせ
                Newtonsoft.Json.Linq.JContainer d = Amazon.ReadBook(strBarcode);

                if (d["Error"] != null)
                {
                    MessageBox.Show("書籍情報がネットワークから取得できませんでした。", "新規登録/編集", MessageBoxButtons.OK);
                    return;
                }


                string isbn      = d["ItemAttributes"]["EANList"]["EANListElement"].ToString();
                string bookname  = d["ItemAttributes"]["Title"].ToString();
                string onsale    = d["ItemAttributes"]["PublicationDate"].ToString();
                string publisher = d["ItemAttributes"]["Publisher"].ToString();
                string url       = "http://www.amazon.co.jp/dp/" + d["ASIN"].ToString() + "/";
                string tag       = "";
                string author;

                //著者が二人以上なら最初の一人のみ登録
                if (d["ItemAttributes"]["Author"].Count() > 1)
                {
                    author = d["ItemAttributes"]["Author"][0].ToString();
                }
                else
                {
                    author = d["ItemAttributes"]["Author"].ToString();
                }

                DataRow workRow = ((BookForm1)this.Owner).dataTable.NewRow();

                workRow["BOOK_ISBN"]          = isbn;
                workRow["BOOK_NAME"]          = bookname;
                workRow["BOOK_AUTHOR"]        = author;
                workRow["BOOK_ONSALE"]        = onsale;
                workRow["BOOK_PUBLISHER"]     = publisher;
                workRow["BOOK_URL"]           = url;
                workRow["BOOK_TAG"]           = tag;
                workRow["BOOK_LENDINGPERIOD"] = Settings.LendingPeriod;
                ((BookForm1)this.Owner).dataTable.Rows.Add(workRow);

                ((BookForm1)this.Owner).editFlag = true;


                //本タイトルがフォームラベルに収まるように15文字以上は省略する
                if (bookname.Length > 15)
                {
                    bookname = bookname.Substring(0, 15) + "...";
                }

                this.ResultLabel.Text = "「" + bookname + "」を読み取りました";

                //Amazon APIリクエスト制限を守る
                System.Threading.Thread.Sleep(Settings.AmazonRequestInterval);
            }
            else
            {
                //読み取り途中の場合

                if (this.isReading == true)
                {
                    this.strBarcode += e.KeyChar.ToString();
                    e.Handled        = true;
                }
            }
        }