由于扇贝查词没有有道便利,所以很多时候添加生词都是在使用有道词典,然后顺手就生存到了有道单词本,不过在扇贝记单词可以打卡,记单词更便利,进入扇贝页面后发明扇贝单词批量导入居然一次只撑持10个,查了扇贝API是可以导入生词的,于是想着何倒霉用c#批量导入单词?说干就干!
筹备事情 获取扇贝token首先需要扇贝账号(这不空话么),然后需要在扇贝上创建本身的应用,进入,点击我的应用创建扇贝应用,,具体配置如下
注意回调地点
创建完应用后需要获取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;
}
}
}
导入设置界面
导入状态检察(按时自动导入残剩未导入单词)
所有导入完成后检察扇贝单词本,所有的单词都加进去了。