def update_article_name(article_id):
    article = db.get_article(article_id)
    if article == None:
        print("There is no article with that ID. article NOT found.\n")
    else:
        print()
        display_single_article(article, str(article.id))
        article_choice = btc.read_int_ranged('1 to edit article title, 2 to leave as is: ' ,
                                             min_value = 1, max_value = 2)
        if article_choice == 1:
            try:
                newsItem1 = na.get_article_from_url(article.link)
                updated_title = newsItem1.title
            except Exception as e:
                print('Scrape failed because of {0}'.format(e))
                updated_title = 'Invalid'
            print('Rescraped title: {0}'.format(updated_title))
            title_choice = btc.read_int_ranged('1 - existing title, 2 - scraped title, 3 - manual input: ', 1, 3)
                                
            if title_choice == 1:
                print('Title update cancelled, article title unchanged.')
                return
            elif title_choice == 2:
                db.update_article_name(article_id, updated_title)
                print('Title update complete. Return to main menu.')
            elif title_choice == 3:
                new_title = btc.read_text('Enter new title or . to cancel: ')
                if new_title != '.':
                    db.update_article_name(article_id, new_title)
                else:
                    print('Edit cancelled, return to main menu')
                    return
        else:
            print('Edit cancelled, article title unchanged')
def scrape_article_name(article_id):
    article = db.get_article(article_id)
    if article == None:
        print('There is no article with that ID. article NOT found.\n')
    else:
        print()
        display_single_article(article, str(article.id))
        article_choice = btc.read_int_ranged('1 to rescrape title, 2 to leave as is: ',
                                             min_value = 1, max_value = 2)
        if article_choice == 1:
            try:
                new_article_news_item = na.get_article_from_url(article.link)
                new_title = new_article_news_item.title
                #new_title = na.get_article_title(article.link)
                #new_title = Article.get_title(article.link)
                print('''
New title: {0}
Old title: {1}'''.format(new_title, article.name))
            except:
                new_title = 'Title scrape failed'
            title_choice = btc.read_int_ranged('1 to replace title, 2 to keep original title: ',
                                               min_value = 1, max_value = 2)
            if title_choice == 1:
                db.update_article_name(article_id, new_title)
            elif title_choice == 2:
                print('article update cancelled')
                
        elif article_choice == 2:
            print('article update cancelled')
def export_roundup_by_year():
    roundup_title = btc.read_text('Enter the roundup title: ')
    roundup_year = btc.read_int_ranged('Enter roundup year: ', 1, 2100)
    filename = btc.read_text('Enter roundup filename: ')
    roundup_choice = btc.read_int_ranged('Enter 1 to export roundup, 2 to cancel: ', 1, 2)
    if roundup_choice == 1:
        roundup_categories = db.get_categories()
        for category in roundup_categories:
            category.articles = db.yearly_roundup_articles(roundup_year, category.id)
        roundup_docx.create_complete_roundup(filename=filename, roundup_title=roundup_title, categories=roundup_categories)
        #display_title()
    elif roundup_choice == 2:
        print('Roundup export cancelled. Return to main menu.\n')
def get_stats(command):
    del command
    stats_choice = btc.read_int_ranged('1 - monthly stats; 2 - yearly stats; 3 - main menu: ',
                                       1, 3)
    if stats_choice in range(1, 3):
        year = btc.read_int_ranged('Enter article year: ', 1, 2100)
        if stats_choice in range(1, 2):
            month = btc.read_int_ranged('Enter article month: ', 1, 12)
            get_monthly_category_stats(month, year)
        else:
            get_yearly_category_stats(year)
    else:
        time.sleep(0.25)
        print('Returning to main menu.\n')
        time.sleep(0.25)
def update_article_description(article_id):
    article = db.get_article(article_id)
    if article == None:
        print("There is no article with that ID. article NOT found.\n")
    else:
        print()
        display_single_article(article, str(article.id))
        article_choice = btc.read_int_ranged('1 to edit article description, 2 to leave as is: ' ,
                                             min_value = 1, max_value = 2)
        if article_choice == 1:
            description_choice = btc.read_text('View article description? y/n: ')
            if description_choice == 'y':
                article_summary = na.get_article_summary(article.link)
                print(article_summary)
#                article_text = Article.get_text(article.link)
#                #article_text = dm.get_cleaned_text(link)
#                article_text = article_text.split()
#                article_text = [i for i in article_text if de.isEnglish(i) == True]
#                article_text = ' '.join(article_text)
#                print(article_text)
            new_description = btc.read_text('Enter new description or "." to cancel: ')
            
            if new_description != '.':
                db.update_article_description(article_id, new_description)
                print('Article description updated.\n')
            else:
                print('Edit cancelled, article description unchanged')
        else:
            print('Edit cancelled, article description unchanged')
def finalize_desc_month(command):
    if not command or command == '':
         new_month = btc.read_int_ranged('Enter new month: ', min_value = 1, max_value = 12)
         new_year = btc.read_int_ranged('Enter new year: ', min_value = 1, max_value = 2100)
         articles_to_finalize = db.get_articles_by_month(month=new_month, year=new_year)
         articles_remaining = len(articles_to_finalize)
         for article in articles_to_finalize:
             print('{0} unreviewed articles'.format(articles_remaining))
             
             update_article_description(article.id)
             description_choice = btc.read_int_ranged('{0} descriptions remaining. Press 1 to continue, 2 to cancel: '.format(articles_remaining),
                                                      1, 2)
             
             articles_remaining -= 1
             if description_choice == 2:
                 print('Update descriptions cancelled')
                 break
def finalize_article_descriptions(month, year=2019):
    undescribed = db.finalize_descriptions(month, year)
    undescribed_articles = len(undescribed)
    print('{0} undescribed articles'.format(undescribed_articles))
    for article in undescribed:
        print('{0} undescribed articles'.format(undescribed_articles))
        update_article_description(article.id)
        description_choice = btc.read_int_ranged('{0} descriptions remaining. Press 1 to continue, 2 to cancel: '.format(undescribed_articles), 1, 2)
        if description_choice == 2:
            print('Update descriptions cancelled')
            break
def update_article_date(article_id):
    article = db.get_article(article_id)
    if article == None:
        print("There is no article with that ID. article NOT found.\n")
    else:
        print()
        display_single_article(article, str(article.id))
        article_choice = btc.read_int_ranged('1 to edit article date, 2 to leave as is: ' ,
                                             min_value = 1, max_value = 2)
        if article_choice == 1:
            new_day = btc.read_int_ranged('Enter new day: ', min_value = 1, max_value = 31)
            new_month = btc.read_int_ranged('Enter new month: ', min_value = 1, max_value = 12)
            new_year = btc.read_int_ranged('Enter new year: ', min_value = 1, max_value = 2100)
            date_choice = btc.read_int_ranged('1 to change date to: {0}/{1}/{2}, 2 to cancel: '.format(new_month, new_day, new_year),
                                              min_value=1, max_value=2)
            if date_choice == 1:
                db.update_article_date(article_id, new_day, new_month, new_year)
                print('Update complete.\n')
            elif date_choice == 2:
                print('Edit cancelled, article date unchanged')
        else:
            print('Edit cancelled, article date unchanged')
def export_roundup_by_category():
    display_categories()
    roundup_categories = db.get_categories()
    categories_remaining = len(roundup_categories)
    categories_for_roundup = []
    for category in roundup_categories:
        print('Categories remaining: {0}'.format(categories_remaining))
        print('Include {0}'.format(category.name))
        category_choice = btc.read_int_ranged('1 to include, 2 to exclude: ', 1, 2)
        if category_choice != 1:
            categories_for_roundup.append(category)
    roundup_title = btc.read_text('Enter the roundup title: ')
    roundup_month = btc.read_int_ranged('Enter roundup month: ', 1, 12)
    roundup_year = btc.read_int_ranged('Enter roundup year: ', 1, 2100)
    filename = btc.read_text('Enter roundup filename: ')
    roundup_choice = btc.read_int_ranged('Enter 1 to export roundup, 2 to cancel: ', 1, 2)
    if roundup_choice == 1:
        for category in categories_for_roundup:
#        for category in roundup_categories:
            category.articles = db.get_articles_for_roundup(roundup_month, roundup_year, category.id)
        roundup_docx.create_complete_roundup(filename=filename, roundup_title=roundup_title, categories=categories_for_roundup)
        #display_title()
    elif roundup_choice == 2:
        print('Roundup export cancelled. Return to main menu.\n')
def update_category(category_id=0):
    if category_id == 0:
        category_id = int(input("category ID: "))
    category = db.get_category(category_id)
    articles = db.get_articles_by_category_id(category_id)
    display_articles(articles, category.name.upper())
    new_category_name = btc.read_text("Enter new category name or '.' to cancel: ")
    if new_category_name != '.':
        update_choice = btc.read_int_ranged("1 to change article name to {0}, 2 to cancel: ".format(new_category_name),
                                            1, 2)
        if update_choice == 1:
            db.update_category(category_id, new_category_name)
            print('Category update complete\n')
        elif update_choice == 2:
            print('Update cancelled.\n')
def update_article_author(article_id):
    article = db.get_article(article_id)
    if article == None:
        print("There is no article with that ID. article NOT found.\n")
    else:
        print()
        display_single_article(article, str(article.id))
        article_choice = btc.read_int_ranged('1 to edit article author, 2 to leave as is: ' ,
                                             min_value = 1, max_value = 2)
        if article_choice == 1:
            new_author = btc.read_text('Enter new author name or . to cancel: ')
            if new_author != '.':
                db.update_article_author(article_id, new_author)
        else:
            print('Edit cancelled, article title unchanged')
def finalize_title_updates(month, year):
    articles = db.get_articles_by_month(month=month, year=year)
    articles_remaining = len(articles)
    for article in articles:
        print('{0} articles remaining'.format(articles_remaining))
        display_single_article(article, title_term = article.id)
        strip_choice = btc.read_int_ranged('1 to update title, 2 to skip, 3 to return to main menu: ', 1, 3)
        if strip_choice == 1:
            update_article_name(article.id)
            articles_remaining -= 1
        if strip_choice == 2:
            articles_remaining -= 1
            print('Article title unchanged.')
        if strip_choice == 3:
            print('strip titles cancelled')
            #display_title()
            break
def update_article_category(article_id):
    article = db.get_article(article_id)
    if article == None:
        print("There is no article with that ID. article NOT found.\n")
    else:
        print()
        display_single_article(article, str(article.id))
        article_choice = btc.read_int_ranged('1 to edit article category, 2 to leave as is: ' ,
                                             min_value = 1, max_value = 2)
        if article_choice == 1:
            new_category_id = btc.read_int('Enter new category_id: ')
            result = db.get_category(new_category_id)
            if result == None:
                print('There is no category with that ID, article category NOT updated.\n')
            else:
                db.update_article_category(article_id, new_category_id)
        else:
            print('Edit cancelled, article title unchanged')
def add_article_from_newspaper(link):
    '''
    Adds an article from the newspaper module after downloading it
    '''
    try:
        #link = btc.read_text('Link or "." to cancel: ')
        #try:
        for i in tqdm.tqdm(range(1)):
            newNewsItem = na.get_article_from_url(link)
        print(newNewsItem)
        #except Exception as e:
            #print(e)
            #get article title
        try:
            name = newNewsItem.title #get the title for the article
            print('NameTest {0}'.format(name))
        except Exception as e:
            print(e)
            name = btc.read_text('Please enter title: ')
            #get article author
        try:
            author = ' '.join(newNewsItem.authors)
            #get article publication
        except Exception as e:
            print(e)
            author = btc.read_text('Please enter author: ')
        try:
            #works for most websites, but not Sudan Tribune
            publication = newNewsItem.meta_data['og']['site_name']
        except Exception as e:
            print(e)
            publication = btc.read_text('Please enter publication: ')
        try:
            year = newNewsItem.publish_date.year
        except Exception as e:
            print(e)
            year = btc.read_int_ranged('Please enter year: ', 1, 2200)
        try:
            month = newNewsItem.publish_date.month
        except Exception as e:
            print(e)
            month = btc.read_int_ranged('Please enter month: ', 1, 12)
        try:
            day = newNewsItem.publish_date.day
        except Exception as e:
            print(e)
            day = btc.read_int_ranged('Please enter day: ', 1, 31)
        try:
            summary = newNewsItem.summary
        except Exception as e:
            print(e)
            print('Summary download failed')
            summary = 'Summary not found'
        try:
            keywords = ', '.join(newNewsItem.keywords)
        except Exception as e:
            print(e)
            print('Keyword download failed')
            keywords= 'keywords not found'
        print('TITLE - {0} - AUTHOR {1}'.format(name, author))
        print('DATE - {0}/{1}/{2} - PUBLICATION {3}'.format(month, day, year, publication))
        #print(author)
        #print(publication)
        #print('{0}/{1}/{2}'.format(month, day, year))
        #print(summary)
        print('KEYWORDS: ', keywords)
        display_categories()
        category_id = btc.read_text("Category ID: ")
        category = db.get_category(category_id)
        if category == None:
            print('There is no category with that ID. article NOT added.\n')
            return
        description_choice = btc.read_text('View article description? y/n: ')
        if description_choice == 'y':
            print('Title: {0}'.format(name))
            print('Summary: {0}'.format(summary))
            print('Keywords: {0}'.format(keywords))
        description = btc.read_text("Description or '.' to cancel: ")
        if description == ".":
            return
        else:
            article = Article(name=name, year=year, month=month,day=day,
                      category=category, link=link, description=description,
                      author=author, publication=publication)
        db.add_article(article)    
        print(name + " was added to database.\n")
    except Exception as e:
        print('Article download failed.', e)