David Chambers avatar David Chambers committed 698bbda

Increased the user-friendliness of the import scripts.

Comments (0)

Files changed (2)

extras/blogger.py

+#!/usr/bin/env python
+
 from __future__ import with_statement
 from datetime import datetime, timedelta, tzinfo
 import os
         break
     head, tail = os.path.split(head)
 else:
-    raise ImportError('Failed to locate Mango.')
+    sys.stderr.write('Failed to locate Mango.\n')
+    sys.exit(1)
 
 from settings import TIME_ZONE
 from mango import utf8
 
 try:
     blog_url = u'%s/' % sys.argv[1].rstrip('/')
-except ValueError:
-    raise ValueError('Missing argument: blog_url.')
+except IndexError:
+    sys.stderr.write('Missing argument: blog_url.\n')
+    sys.exit(1)
 
 connection = urllib2.urlopen('%sfeeds/posts/default?alt=json' % blog_url)
 data = connection.read()
 
 posts_directory() # create posts directory if necessary
 
-for entry in simplejson.loads(data)['feed']['entry']:
+entries = simplejson.loads(data)['feed']['entry']
+
+for entry in entries:
 
     lines = []
 
 
     with open(os.path.join(DOCUMENTS_PATH, '%s.text' % slug), 'w') as f:
         f.write(u'%s\n' % '\n'.join(lines))
+
+sys.stdout.write('Successfully imported %s %s.\n' % (
+        len(entries), len(entries) == 1 and 'document' or 'documents'))
+sys.exit()
+#!/usr/bin/env python
+
 from __future__ import with_statement
 from datetime import datetime
 import os
         break
     head, tail = os.path.split(head)
 else:
-    raise ImportError('Failed to locate Mango.')
+    sys.stderr.write('Failed to locate Mango.\n')
+    sys.exit(1)
 
 from settings import TIME_ZONE
 from mango.settings import DOCUMENTS_PATH, MARKDOWN_DATE_FORMAT, MARKDOWN_TIME_FORMAT
 try:
     blog_url, username, password = sys.argv[1:]
 except ValueError:
-    raise ValueError('Three arguments required: blog_url, username, password.')
+    sys.stderr.write('Three arguments required: blog_url, username, password.\n')
+    sys.exit(1)
 
 server = xmlrpclib.ServerProxy(blog_url + 'xmlrpc.php')
 
         blog_id = blog['blogid']
         break
 else:
-    raise Exception('Failed to find blog. Double-check supplied arguments.')
+    sys.stderr.write('Failed to find blog. Double-check supplied arguments.\n')
+    sys.exit(1)
 
 posts_directory() # create posts directory if necessary
 
-for page in server.wp.getPages(blog_id, username, password):
+pages = server.wp.getPages(blog_id, username, password)
+
+for page in pages:
     html2text.BODY_WIDTH = BODY_WIDTH
     with open(os.path.join(DOCUMENTS_PATH, '%s.text' % page['wp_slug']), 'w') as f:
         f.write(u'author: %s\n\n\n%s\n%s\n\n%s\n' % (
                 page['title'], '=' * len(page['title']),
                 html2text.html2text(page['description']).strip()))
 
-for post in server.metaWeblog.getRecentPosts(blog_id, username, password, 0):
+posts = server.metaWeblog.getRecentPosts(blog_id, username, password, 0)
+
+for post in posts:
 
     lines = []
 
 
     with open(os.path.join(DOCUMENTS_PATH, '%s.text' % post['wp_slug']), 'w') as f:
         f.write(u'%s\n' % '\n'.join(lines))
+
+sys.stdout.write('Successfully imported %s %s and %s %s.\n' % (
+        len(pages), len(pages) == 1 and 'page' or 'pages',
+        len(posts), len(posts) == 1 and 'post' or 'posts'))
+sys.exit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.