Guest User

Untitled

a guest
Oct 16th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #!/bin/python
  2. # -*- coding:utf-8 -*-
  3. # a script sync. wordbook from youdao dict to anki
  4. import sys
  5. import hashlib,time
  6. import httplib, urllib2, cookielib
  7. from xml.dom.minidom import parse, parseString
  8.  
  9. class Youdao:
  10.  
  11. """init login for Youdao"""
  12. def __init__(self, username,password):
  13. self.username = username
  14. self.password = hashlib.md5(password.encode('utf-8')).hexdigest()
  15. status = {'LOGIN_SUCCESS':'201'}
  16. loginHost = 'reg.163.com'
  17. loginPath = '/services/userlogin?username=%s&password=%s&type=1&product=search' \
  18. % (self.username, self.password)
  19. self.headers = {
  20. 'User-Agent':'YoudaoDictPro/3.1.0 CFNetwork/548.1.4 Darwin/11.0.0'
  21. }
  22.  
  23. conn = httplib.HTTPConnection(loginHost)
  24. conn.request('GET', loginPath,None, self.headers)
  25. response = conn.getresponse()
  26. txt = response.read().splitlines()
  27. if txt[0] == '201':
  28. self.cookies = response.getheader('Set-Cookie')
  29. print 'Login Success!'
  30. else:
  31. self.cookies = ''
  32. print '[ERROR]', [i for i in txt]
  33.  
  34. response.close()
  35.  
  36. def syncDict(self, output):
  37. dictHost = 'dict.youdao.com'
  38. dictPath = '/wordbook/api?appVer=mdict.3.1.0.iphonepro&id=961aaaebd8fb1d8c65385561e32bf244&model=iPod%20touch&deviceid=49b0d44f517212e77c74d5c67d30d932e6cae827&mid=5.1'
  39. nowtimestamp = repr(time.time() * 1000).split('.')[0]
  40. '''
  41. FORMAT like:
  42. <actionlist>
  43. <type>words</type>
  44. <localtimestamp>1337502741787</localtimestamp>
  45. <remlocaltimestamp>1337502741778</remlocaltimestamp>
  46. <action type="refresh">
  47. <localtimestamp>1337517090.037</localtimestamp>
  48. <remlocaltimestamp>0</remlocaltimestamp>
  49. </action>
  50. </actionlist>
  51. '''
  52. #expected lastest timestamp
  53. remlocaltimestamp = nowtimestamp
  54. #local last updated time
  55. localtimestamp = '1117217156809' #Sat May 28 2005 02:05:56 GMT+0800 that is OLD enough!;) @TODO :cached
  56.  
  57. data='data=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%20%3F%3E%3Cactionlist%3E%3Ctype%3Ewords%3C%2Ftype%3E%3Clocaltimestamp%3E' + localtimestamp \
  58. + '%3C%2Flocaltimestamp%3E%3Cremlocaltimestamp%3E' + remlocaltimestamp \
  59. + '%3C%2Fremlocaltimestamp%3E%3Caction%20type%3D%22refresh%22%3E%3Clocaltimestamp%3E' + localtimestamp \
  60. + '%3C%2Flocaltimestamp%3E%3Cremlocaltimestamp%3E' + remlocaltimestamp \
  61. + '%3C%2Fremlocaltimestamp%3E%3C%2Faction%3E%3C%2Factionlist%3E'
  62.  
  63. self.headers['Cookie'] = self.cookies
  64. self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  65. self.headers['User-Agent'] = 'iphonepro'
  66.  
  67. conn = httplib.HTTPConnection(dictHost)
  68. conn.request('POST',dictPath,data,self.headers)
  69. response = conn.getresponse()
  70. status,reason = response.status, response.reason
  71. if status == 200:
  72. self.dictXML = response.read()
  73. f = open('dict.xml', 'w')
  74. f.write(self.dictXML)
  75. f.close()
  76. else:
  77. print '[ERROR]',status, reason
  78.  
  79. response.close()
  80.  
  81. dom = parseString(self.dictXML)
  82.  
  83. f = open(output, 'w')
  84. for item in dom.getElementsByTagName('alter'):
  85. word = item.getElementsByTagName('word')[0].firstChild.wholeText
  86. try:
  87. trans = item.getElementsByTagName('trans')[0].firstChild.wholeText
  88. except:
  89. trans = ''
  90.  
  91. word = word.strip().encode('utf-8')
  92. trans = trans.strip().encode('utf-8')
  93.  
  94. f.write(word + ' , ' + trans)
  95. f.write('\r\n')
  96. f.close()
  97. print 'Fetch Success! check out wordbook.txt'
  98.  
  99. def main(args):
  100. if len(args) < 3 or \
  101. args[1] in ['-u', '--usage', '-h', '--help', '/h', '/help']:
  102. print 'Usage:\n\t python', args[0], 'your@email.com your_password\n'
  103. sys.exit(0)
  104.  
  105. youdao = Youdao(args[1],args[2])
  106. youdao.syncDict('wordbook.txt')
  107.  
  108. if __name__ == '__main__':
  109. main(sys.argv)
Add Comment
Please, Sign In to add comment