C#导入有道词典单词本到扇贝
Posted qidong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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!
以上是关于C#导入有道词典单词本到扇贝的主要内容,如果未能解决你的问题,请参考以下文章
在ubuntu12.04中,怎么成功安装有道词典啊????具体操作