• C#导入有道词典单词本到扇贝


    由于扇贝查词没有有道方便,所以很多时候添加生词都是在使用有道词典,然后顺手就保存到了有道单词本,不过在扇贝记单词可以打卡,记单词更方便,进入扇贝页面后发现扇贝单词批量导入居然一次只支持10个,查了扇贝API是可以导入生词的,于是想着何不利用c#批量导入单词?说干就干!

    准备工作

    获取扇贝token

    首先需要扇贝账号(这不废话么),然后需要在扇贝上创建自己的应用,进入http://www.shanbay.com/developer/wiki/intro/,点击我的应用创建扇贝应用,具体配置如下

    注意回调地址

    创建完应用后需要获取token

    client_id就是你的appkey,进入这个地址后会提示授权(先登录自己的用户),然后同意授权后会跳转到一个网址,你会看到access_token,复制下来备用。

    导出有道词典

    打开有道词典->单词本->浏览选项卡->管理->导出到文件->所有分类->导出为xml文件,备用。

    开始编程

     思路

    1) 扇贝单词插入方法有两个接口:一个是根据单词获取id,另一个根据ID添加单词

    2) 由于扇贝有单人账户导入限制(单用户单app 200个/小时),如果导入单词过多会被禁止访问,所以需要加个定时器自动添加剩余单词

    核心代码如下:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    namespace ImportNewWordsToShanbay
    {
       public class ShanbayImportHelper
        {
           public static int GetShanbayWordID(string word, string access_token)
           {
               try
               {
                   var result = HttpHelper.HttpGet("https://api.shanbay.com/bdc/search/", "word=" + word + "&access_token=" + access_token);
                   JObject json = JObject.Parse(result);
                   if (json.Property("status_code").Value.ToString() == "0")
                   {
                       var wordId = JObject.Parse(json.Property("data").Value.ToString()).Property("id").Value.ToString();
                       return int.Parse(wordId);
                   }
                   else
                       return -1;
               }
               catch (Exception ex)
               {
                   return 0; 
               }
           }
    
           public static bool AddNewWordToShanbay( int wordId,string access_token)
           {
               try
               {
                   var data = "{"id":" + wordId + "}";
                   var postResult = HttpHelper.HttpPost("https://api.shanbay.com/bdc/learning/", access_token, data);
                   JObject postJsonResult = JObject.Parse(postResult);
                   if (postJsonResult.Property("msg").Value.ToString().ToUpper() == "SUCCESS")
                   {
                       return true;
                   }
               }
               catch (Exception ex)
               {
     
               }
               return false;
           }
        }
    }

    Http帮助类

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Security;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ImportNewWordsToShanbay
    {
       public class HttpHelper
        {
            public static string HttpPost(string Url, string token, string postDataStr)
            {
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.ProtocolVersion = HttpVersion.Version11;
                request.Method = "POST";
                request.Headers.Add("Authorization", string.Format("Bearer {0}", token));
                request.ContentType = "application/json";
                request.Referer = null;
                request.AllowAutoRedirect = true;
                request.Accept = "*/*";
                byte[] buffer = encoding.GetBytes(postDataStr);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }

    public static string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } } }

    导入设置界面

    导入状态查看(定时自动导入剩余未导入单词)

     

    所有导入完成后查看扇贝单词本,所有的单词都加进去了。

    Enjoy!

    作者:qidong
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    面向对象的链式调用
    浅显易懂的理解JavaScript中的this关键字
    js 页面刷新location.reload和location.replace的区别小结
    JavaScript中两种类型的全局对象/函数
    event.srcElement ,event.fromElement,event.toElement
    json jsonp的区别
    createElement与createDocumentFragment的点点区别
    解决Ajax不能跨域的方法
    mysqli_fetch_assoc php的新的库函数
    500 501 502 503 504 505 服务器错误
  • 原文地址:https://www.cnblogs.com/qidong/p/8378052.html
Copyright © 2020-2023  润新知