Guest User

Element14 EAGLE CAD libraries downloader tool

a guest
Mar 1st, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import urllib.request
  4. import urllib.parse
  5. import urllib.error
  6. import html.parser
  7. import http.cookiejar
  8. import re
  9. import time
  10.  
  11. ## CONFIG START
  12. USERNAME = ""
  13. PASSWORD = ""
  14. ## CONFIG END
  15.  
  16. SITE_ROOT = "https://www.element14.com"
  17. SITE_LOGIN = "/community/cs_login"
  18. SITE_INBOX = "/community/inbox"
  19. SITE_LIBS = "/community/community/cadsoft_eagle/eagle_cad_libraries"
  20. SITE_LIB_PREFIX = "/community/docs/DOC-"
  21. SITE_DOWNLOAD_PREFIX = "/community/servlet/JiveServlet/download/"
  22.    
  23. class LinkCollector(html.parser.HTMLParser):
  24.     STATE_INIT = 1
  25.     STATE_LINK = 2
  26.    
  27.     def __init__(self, regex):
  28.         super().__init__(convert_charrefs=True)
  29.         self.state = self.STATE_INIT
  30.         self.regex = regex
  31.         self.href = None
  32.         self.name = None
  33.         self.links = []
  34.    
  35.     def handle_starttag(self, tag, attrs):
  36.         attrs = dict(attrs)
  37.         if tag == 'a' and self.regex.search(attrs.get('href', '')):
  38.             self.state = self.STATE_LINK
  39.             self.href = SITE_ROOT + attrs.get('href', '')
  40.             self.name = None
  41.    
  42.     def handle_data(self, data):
  43.         if self.state == self.STATE_LINK:
  44.             self.name = data
  45.    
  46.     def handle_endtag(self, tag):
  47.         if tag == 'a' and self.state == self.STATE_LINK:
  48.             self.links.append((self.href, self.name))
  49.             self.state = self.STATE_INIT
  50.  
  51. #cj = None
  52.  
  53. def init():
  54.     global cj # just to debug it from main()
  55.     cj = http.cookiejar.CookieJar()
  56.     opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
  57.     return opener
  58.  
  59. def login(opener):
  60.     data = {'username': USERNAME, 'password': PASSWORD, 'autoLogin': 'true'}
  61.     request = urllib.request.Request(
  62.         SITE_ROOT + SITE_LOGIN,
  63.         data=urllib.parse.urlencode(data).encode('utf-8'),
  64.         headers={'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}
  65.     )
  66.     response = opener.open(request)
  67.     return response
  68.  
  69. def list_libs(opener):
  70.     response = opener.open(SITE_ROOT + SITE_LIBS)
  71.     parser = LinkCollector(re.compile('^' + SITE_LIB_PREFIX))
  72.     charset = response.info().get_param('charset', 'utf-8')
  73.     parser.feed(response.read().decode(charset))
  74.     return parser.links
  75.    
  76. def download_lib(opener, liburl):
  77.     libnumber = liburl.split('-')[-1]
  78.     response = opener.open(liburl)
  79.     parser = LinkCollector(re.compile('^' + SITE_DOWNLOAD_PREFIX + libnumber))
  80.     charset = response.info().get_param('charset', 'utf-8')
  81.     parser.feed(response.read().decode(charset))
  82.     processed = []
  83.     for (url, name) in parser.links:
  84.         #print(url); continue
  85.         if url in processed:
  86.             continue
  87.         print(' -> Downloading', url)
  88.         filename = url.split('/')[-1]
  89.         with opener.open(url) as resp, open(filename, 'wb') as file:
  90.             file.write(resp.read())
  91.         processed.append(url)
  92.  
  93. def main():
  94.     global cj
  95.     opener = init()
  96.     res = login(opener)
  97.     print(res.code)
  98.     print(cj)
  99.     data = res.read().decode(res.info().get_param('charset', 'utf-8'))
  100.     if SITE_INBOX in data:
  101.         print('Login succeeded')
  102.     else:
  103.         print('Login failed')
  104.         return
  105.     libs = list_libs(opener)
  106.     for (url, name) in libs:
  107.         print('Processing library', name)
  108.         download_lib(opener, url)
  109.         #break
  110.  
  111. if __name__ == '__main__':
  112.     main()
Add Comment
Please, Sign In to add comment