LoginSignup
0
1

More than 5 years have passed since last update.

C#でニコニコ動画の動画説明文を編集できるようにする

Last updated at Posted at 2016-12-23

はじめに

次の記事でニコニコ動画から情報を取得する方法について記載しました。
C#で任意のサイトの情報を抜き取る - ウェブスクレイピングはじめの一歩

今回は引き続き同じ題材で、編集を行えるようにしたいと思います。

ソースコード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RniconicoEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.nicovideo.jp/my/video");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            System.Diagnostics.Debug.Print(e.Url.ToString());

            if (e.Url.Equals("http://www.nicovideo.jp/my/video"))
            {
                HtmlElement el = webBrowser1.Document.GetElementById("video").Children[5];
                if (el != null)
                {
                    for (int i = 1; i < el.Children.Count; i++)
                    {
                        HtmlElement elm = el.Children[i].Children[1].Children[0].Children[0];
                        if (elm != null)
                        {
                            string key = elm.GetAttribute("href").Replace("http://www.nicovideo.jp/watch/", "");
                            if (!treeView1.Nodes.ContainsKey(key))
                                treeView1.Nodes.Add(key, elm.InnerText).Tag = key.Replace("sm", "");
                            else
                                treeView1.Nodes[key].Text = elm.InnerText;
                        }
                    }
                }
            }
            else if (0 <= e.Url.ToString().IndexOf("http://www.upload.nicovideo.jp/edit?f="))
            {
                textBox1.Text = webBrowser1.Document.GetElementById("movie_title").GetAttribute("value");
                textBox2.Text = webBrowser1.Document.GetElementById("movie_detail").OuterText;
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            webBrowser1.Navigate($"http://www.upload.nicovideo.jp/edit?f={e.Node.Tag}");
            textBox3.Text = "sm" + e.Node.Tag;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Document.GetElementById("movie_title").InnerText = textBox1.Text;
            webBrowser1.Document.GetElementById("movie_detail").InnerText = textBox2.Text;
            webBrowser1.Document.GetElementById("submitbtn").InvokeMember("click");
        }
    }
}

前回から付け加えたのは次の点です。

  • Buttonコントロールを追加しbutton1_Clickイベントを作成
  • button1_Clickイベントにて"movie_title""movie_detail"エレメントへInnertTextで編集結果を設定
  • ニコニコ動画側の更新ボタンをDeveloper ToolsでIdを調べて、clickを呼び出し

これだけです。
前回取得が出来ているので、取得した項目を上書きし、実行させるだけです。

以下、十差に作成したツールの動作の様子を動画にしましたのでご覧ください。
ニコニコ動画の動画説明文をすばやく編集するためのツールを作ってみた

以上

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1