Commits

Szymon Wróblewski  committed 4c7bf7e

updated to Python 3.3

  • Participants
  • Parent commits 78421ac

Comments (0)

Files changed (2)

 Requirements
 ------------
 
+* Python 3.3
 * `Requests <http://python-requests.org/>`_
 
 
-#!/usr/bin/env python
-# -*- coding: UTF-8 -*-
+#!/usr/bin/env python3
 ##    ytpd - youtube playlist titles downloader
-##    Copyright (C) 2013 Szymon Wroblewski
+##    Copyright (C) 2013 Szymon Wróblewski
 ##
 ##    This library is free software; you can redistribute it and/or
 ##    modify it under the terms of the GNU Library General Public
 ##    License along with this library; if not, write to the Free
 ##    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ##
-##    Szymon Wroblewski
+##    Szymon Wróblewski
 ##    bluex0@gmail.com
 
 import sys
+import logging
+from urllib.parse import urlparse, parse_qs
+from concurrent.futures import ThreadPoolExecutor
 from requests import get
-from urlparse import urlparse, parse_qs
 
 api_url = 'http://gdata.youtube.com/feeds/api'
 api_params = {
     'v': '2',
 }
 
-
-def main():
-    url = sys.argv[1] if len(sys.argv) > 1 else raw_input('URL: ')
-    try:
-        url_parts = urlparse(url)
-        path_parts = url_parts.path.split('/')
-        if path_parts[1] == 'user':
-            print 'parsing user'
-            parse_user(path_parts[2])
-        elif path_parts[1] == 'playlist':
-            print 'parsing playlist'
-            parse_playlist(parse_qs(url_parts.query)['list'][0])
-    except (KeyError, IndexError):
-        print "unknown URL format"
+class DataError(Exception):
+    pass
 
 
 def get_json(*args, **kwargs):
     r = get(*args, **kwargs)
     if r.status_code != 200:
-        print r.text
+        logging.error(r.text)
         return {}
     return r.json()
 
     params['max-results'] = ipp
     total = parse_info(url, params)['totalItems'] if total is None else total
     for current in range(start, total, ipp):
-        print 'downloading {}/{}'.format(current, total)
+        logging.info(('downloading {}/{}'.format(current, total)))
         part_params = dict(params, **{'start-index': current})
         data = get_json(url, params=part_params)['data']
         for item in data['items']:
     url = '/'.join((api_url, 'users', user_id, 'playlists'))
     try:
         items = parse_partial(url)
-        for item in items:
-            print 'playlist: {title}'.format(**item)
-            parse_playlist(item['id'])
+        with ThreadPoolExecutor(max_workers=8) as executor:
+            executor.map(parse_playlist, (item['id'] for item in items))
     except KeyError as e:
-        print 'Data error: {}'.format(e)
+        raise DataError from e
 
 
 def parse_playlist(playlist_id):
     url = '/'.join((api_url, 'playlists', playlist_id))
     try:
         info = parse_info(url)
-        f = open('{title}.txt'.format(**info), 'w')
-        items = parse_partial(url, total=info['totalItems'])
-        for item in items:
-            f.write(item['video']['title'].encode('utf8') + '\n')
+        logging.info('playlist: {title}'.format(**info))
+        with open('{title}.txt'.format(**info), 'w', encoding='utf8') as f:
+            items = parse_partial(url, total=info['totalItems'])
+            f.writelines('{position}. {video[title]}\n'.format(**item) for item
+                         in items)
     except KeyError as e:
-        print 'Data error: {}'.format(e)
-    finally:
-        f.close()
+        raise DataError from e
+
+
+def main():
+    logging.basicConfig(level=logging.INFO, style='{',
+                        format='{levelname}:{thread}:{message}')
+    logging.getLogger("requests").setLevel(logging.WARNING)
+    url = sys.argv[1] if len(sys.argv) > 1 else input('URL: ')
+    try:
+        url_parts = urlparse(url)
+        path_parts = url_parts.path.split('/')
+        if path_parts[1] == 'user':
+            logging.info('parsing user')
+            parse_user(path_parts[2])
+        elif path_parts[1] == 'playlist':
+            logging.info('parsing playlist')
+            parse_playlist(parse_qs(url_parts.query)['list'][0])
+    except DataError:
+        logging.exception('Data Error')
+    except IndexError:
+        logging.error('Invalid URL')
 
 
 if __name__ == '__main__':