├── .gitignore ├── token.json ├── README.MD ├── outh_shanbay.py └── word.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.py[cod] 3 | *.sw[op] 4 | *.log 5 | -------------------------------------------------------------------------------- /token.json: -------------------------------------------------------------------------------- 1 | { 2 | "token_type": "Bearer", 3 | "state": "8wk4O77dAumERQP4GlP3q6tzHjmuSD", 4 | "access_token": "QIpqr56gm1mBffv3hmdoQqsKcXGTXh", 5 | "scope": [ 6 | "read", 7 | "write" 8 | ], 9 | "expires_in": "2592000", 10 | "expires_at": 1451798530.223116 11 | } -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | ## 欧路辞典单词本同步到扇贝工具 2 | ============ 3 | 4 | 5 | 6 | 7 | 8 | #### 首先给本应用授权,终端下执行下面的代码: 9 | 10 | ``` 11 | $ python outh_shanbay.py 12 | ``` 13 | 14 | #### 同步欧路辞典的单词本到扇贝单词本: 15 | 16 | ``` 17 | $ python word.py username password 10 18 | ``` 19 | 20 | 21 | username password 为欧路辞典的网站账户 http://dict.eudic.net/ 22 | 最后一个参数为同步的单词数 23 | 24 | 25 | #### 依赖: 26 | 27 | ``` 28 | $ pip install shanbay 29 | ``` 30 | 31 | -------------------------------------------------------------------------------- /outh_shanbay.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import json 5 | 6 | from oauthlib.oauth2 import ( 7 | FatalClientError, OAuth2Error, TokenExpiredError, MobileApplicationClient 8 | ) 9 | from requests_oauthlib import OAuth2Session, TokenUpdated 10 | 11 | 12 | client_id = '42e61b1dfa3df66c783e' 13 | client_secret = '68d90e5fe8e1af9901f0eae6d4815beeb8a9bd7a' 14 | redirect_uri = 'https://api.shanbay.com/oauth2/auth/success/' 15 | authorization_base_url = 'https://api.shanbay.com/oauth2/authorize/' 16 | token_url = 'https://api.shanbay.com/oauth2/token/' 17 | token_file = 'token.json' 18 | 19 | 20 | def userinfo(api): 21 | r = api.get('https://api.shanbay.com/account/') 22 | print r.json() 23 | 24 | 25 | def get_token(): 26 | print client_id 27 | client = MobileApplicationClient(client_id) 28 | print client 29 | api = OAuth2Session(client=client, redirect_uri=redirect_uri) 30 | authorization_url, state = api.authorization_url(authorization_base_url) 31 | 32 | print authorization_url 33 | print 'Please go here and authorize,', authorization_url 34 | 35 | redirect_response = raw_input('Paste the full redirect URL here:') 36 | 37 | token = api.token_from_fragment(redirect_response) 38 | 39 | with open(token_file, 'w') as f: 40 | f.write(json.dumps(token, indent=2)) 41 | return api 42 | 43 | try: 44 | with open(token_file) as f: 45 | token = json.loads(f.read()) 46 | api = OAuth2Session(client_id, token=token) 47 | userinfo(api) 48 | except ( 49 | TokenExpiredError, TokenUpdated, OAuth2Error, FatalClientError, ValueError 50 | ) as e: 51 | api = get_token() 52 | userinfo(api) 53 | 54 | -------------------------------------------------------------------------------- /word.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | #coding:utf-8 3 | 4 | 5 | import sys 6 | import re 7 | import urllib2 8 | import urllib 9 | import requests 10 | import cookielib 11 | import json 12 | from shanbay import API 13 | 14 | ## 这段代码是用于解决中文报错的问题 15 | reload(sys) 16 | sys.setdefaultencoding("utf8") 17 | ##################################################### 18 | loginurl = 'http://dict.eudic.net/Account/Login?returnUrl=' 19 | logindomain = 'eudic.net' 20 | 21 | class dict_app(object): 22 | 23 | def __init__(self): 24 | self.name = '' 25 | self.passwprd = '' 26 | self.domain = '' 27 | 28 | self.cj = cookielib.LWPCookieJar() 29 | self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) 30 | urllib2.install_opener(self.opener) 31 | 32 | def setLoginInfo(self,username,password): 33 | '''设置用户登录信息''' 34 | self.name = username 35 | self.pwd = password 36 | self.domain = domain 37 | 38 | def login(self): 39 | '''登录网站''' 40 | #loginparams = {'domain':self.domain,'UserName':self.name, 'Password':self.pwd} 41 | try: 42 | loginparams = {'domain':self.domain,'UserName':self.name, 'Password':self.pwd,'returnUrl':'',"RememberMe":"true"} 43 | headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'} 44 | req = urllib2.Request(loginurl, urllib.urlencode(loginparams),headers=headers) 45 | response = urllib2.urlopen(req) 46 | self.operate = self.opener.open(req) 47 | thePage = response.read() 48 | except urllib2.HTTPError: 49 | print "eudit login error pleare check your's account username and password" 50 | sys.exit() 51 | #print thePage 52 | 53 | #删除单词 54 | # 请求头部需要设置内容长度 55 | #wordid examplx aa,bb,cc,ee 56 | def delete_word(self,word_id): 57 | url="http://dict.eudic.net/StudyList/Edit" 58 | par = 'oper=del&id='+reduce(lambda x,y:x+','+y,word_id) 59 | print par 60 | headers = {'Content-Length':len(par),'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'} 61 | req = urllib2.Request(url,par, headers=headers) 62 | response = urllib2.urlopen(req) 63 | self.operate = self.opener.open(req) 64 | thePage = response.read() 65 | print "delete finish" 66 | 67 | #get all new words from service 68 | def get_word_by_num(self,num): 69 | en_words={} 70 | page,rows=1,num 71 | #eudic 这个连接是不限制单页取词数的 72 | data=urllib2.urlopen("http://dict.eudic.net/StudyList/GridData?catid=&_search=false&nd=1426850560615&rows=%s&page=%s&sidx=&sord=asc"%(str(rows),str(page))) 73 | json_data= json.loads(data.read()) 74 | #print json_data 75 | words= json_data["rows"] 76 | for word in words: 77 | w_id = word["id"] 78 | d=word["cell"][1].decode("unicode-escape") 79 | d = re.sub('<[^<]+?>', '', d) 80 | en_words[w_id]=d 81 | return en_words 82 | 83 | #shanbay class 84 | class shanbay_study(): 85 | def __init__(self,client_id): 86 | self.client_id = client_id 87 | f=open("token.json","r") 88 | self.token=json.loads(f.read()) 89 | f.close() 90 | self.api=None 91 | self.search_words=[] 92 | 93 | 94 | def connect(self): 95 | self.api = API(self.client_id,self.token) 96 | 97 | def batch_add_search_words(self,words): 98 | not_found_word=[] 99 | added_word=[] 100 | if self.api and len(words) != 0: 101 | for word in words: 102 | word_search = self.api.word(word) 103 | # there is no need add word had been added 104 | if word_search["msg"] == 'SUCCESS' and ('learning_id' not in word_search['data']): 105 | result=self.api.add_word(word_search["data"]["id"]) 106 | print result 107 | if result['msg'] == 'SUCCESS': 108 | print "%s add word success " % word 109 | else: 110 | print "%s add word faile " % word 111 | 112 | elif word_search["msg"] != 'SUCCESS' : 113 | print " %s add word faile add to no_result_word" % word 114 | not_found_word.append(word) 115 | elif word_search["msg"] == 'SUCCESS' and ('learning_id' in word_search['data']): 116 | print " %s had been added befor ,overleap it" % word 117 | added_word.append(word) 118 | 119 | else: 120 | print "api has not init\n" 121 | sys.exit() 122 | return (not_found_word,added_word) 123 | 124 | 125 | 126 | 127 | if __name__ == '__main__': 128 | if len(sys.argv) != 4: 129 | print 'need eudic account info for sync(http://dict.eudic.net/)' 130 | print 'Usage: python word.py username passwd wordnum' 131 | print 'the wordnum is the number you want sync to shanbay' 132 | print 'warring the word will be delete when the word sync succesful' 133 | exit(1) 134 | eudict_app = dict_app() 135 | username = sys.argv[1] #wrong account username will make a error 136 | password = sys.argv[2] 137 | sync_num = int(sys.argv[3]) 138 | domain = logindomain 139 | eudict_app.setLoginInfo(username,password) 140 | eudict_app.login() 141 | shanbay_app = shanbay_study("42e61b1dfa3df66c783e") # init must set shanbay clinet_Id 142 | shanbay_app.connect() 143 | 144 | #eudict_app.delete_word("1111") 145 | en_words = eudict_app.get_word_by_num(2) 146 | for i in en_words: 147 | print i +" " +en_words[i] 148 | word_not_found,word_added=shanbay_app.batch_add_search_words(en_words.values()) 149 | print word_not_found 150 | delete_word = [ x for x ,y in en_words.items() if y not in word_not_found and y in word_added] 151 | print delete_word 152 | eudict_app.delete_word(delete_word) 153 | --------------------------------------------------------------------------------