# -*- coding: utf-8 -*-
from __future__ import print_function
import click
import os
import sys
import json
import first_timers as FT

def updateDB(all_issues, db_path):
    """Truncate and write the new list of issues in the DB."""
    with open(db_path, 'w') as dbFile:
        json.dump(FT.limit_issues(all_issues), dbFile)

@click.command()
@click.option('--only-save',
        is_flag=True,
        help='Do not post any tweets, just populate the DB.')
@click.option('--db-path',
        prompt='Database file',
        default='data/db.json',
        help='Old issues in a JSON list.')
@click.option('--create',
        is_flag=True,
        help='Pass if the DB file should be created.')
@click.option('--creds-path',
        prompt='Credentials file',
        default='',
        help='File which contains Twitter account credentials.'
             'Not needed if only saving to DB.')
@click.option('--debug',
        is_flag=True,
        help='Run in debug mode (does not tweet).')
def run(only_save, db_path, create, creds_path, debug):
    dbExists = os.path.exists(db_path)
    if not dbExists and not create:
        click.echo('DB file does not exist and argument'
                   '--create was not passed.', err=True)
        sys.exit(-1)
    elif dbExists and not create:
        with open(db_path, 'rb') as dbFile:
            old_issues = json.load(dbFile)
    elif not dbExists and create:
        old_issues = []
    else:
        click.echo('DB file exists but --create was passed.', err=True)
        sys.exit(-1)

    # Getting the latest list of issues from Github
    new_issues = FT.get_first_timer_issues()
    fresh_issues = FT.get_fresh(old_issues, new_issues)
    all_issues = fresh_issues + old_issues

    if not only_save:
        try:
            if not os.path.exists(creds_path):
                print('Credentials file does not exist.', file=sys.stdout)
                sys.exit(-1)

            with open(creds_path, 'r') as credsFile:
                creds = json.load(credsFile)

            click.echo('Tweeting {} tweet(s).'.format(len(fresh_issues)))
            for issue in fresh_issues:
                click.echo('\t URL: ' + issue['url'])

            tweets = FT.tweet_issues(fresh_issues, creds, debug)

            for tweet in tweets:
                click.echo('\t' + tweet)

        except UnicodeEncodeError as e:
            click.echo('Unable to post tweets because: ' + str(e))

    updateDB(all_issues, db_path)

    if len(fresh_issues) > 0:
        click.echo('Database updated.')


if __name__ == '__main__':
    run()


@@ -835,16 +835,17 @@
 entials.
+ 
 '%0A      
@@ -1123,32 +1123,33 @@
 :%0A        click.
+s
 echo('DB file do
@@ -1194,16 +1194,17 @@
         
+ 
 '--creat
@@ -1223,32 +1223,42 @@
 ssed.', err=True
+, fg='red'
 )%0A        sys.ex
@@ -1463,24 +1463,25 @@
       click.
+s
 echo('DB fil
@@ -1515,25 +1515,55 @@
 assed.',
- err=True
+%0A                    err=True, fg='red'
 )%0A      
@@ -2075,24 +2075,25 @@
       click.
+s
 echo('Tweeti
@@ -2189,32 +2189,33 @@
           click.
+s
 echo('%5Ct URL: ' 
@@ -2228,16 +2228,27 @@
 e%5B'url'%5D
+, fg='blue'
 )%0A%0A     
@@ -2437,24 +2437,25 @@
       click.
+s
 echo('Unable
@@ -2489,16 +2489,26 @@
 + str(e)
+, fg='red'
 )%0A%0A    u

Use click's styles for better console output