예제 #1
0
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')
예제 #2
0
def export_roundup():
    roundup_title = btc.read_text('Enter the roundup title or "." to cancel: ')
    filename = btc.read_text('Enter the filename or "." to cancel: ')
    if roundup_title != '.':
        roundup_categories = db.get_categories()
        for category in roundup_categories:
            category.articles = db.get_articles_by_category_id(category.id)
        roundup_docx.create_complete_roundup(filename=filename, roundup_title=roundup_title, categories=roundup_categories)
예제 #3
0
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')
예제 #4
0
def get_csv_in_directory():
    file_list = glob.glob('*.csv')
    print('Files available: ')
    for item in file_list:
        print(item, end='\n')
    #try:
    importing_file_name = btc.read_text('Enter filename from the list or ". " to cancel: ')
    if importing_file_name != '.':
        filename='{0}.csv'.format(importing_file_name)
        csv_articles = create_csv_list(filename)
        print(csv_articles)
        #csv_articles = [csv_item_to_article(csv_article) for csv_article in csv_articles]
        print('Articles to import:')
        try:
            for article in csv_articles:
                try:
                    csv_article = csv_item_to_article(article)
                    try:
                        csv_article.name = Article.get_title(csv_article.link)
                    except Exception as e:
                        print(e)
                        csv_article.name = 'Not specified'
                    db.add_article_from_csv(csv_article)
                    print(csv_article.name + " was added to database.\n")
                #print('Import complete, return to main menu \n')
                except Exception as e:
                    print(e)
                    print('Article import failed.')
                    continue
            print('Import complete, return to main menu')
        except Exception as e:
            print(e)
예제 #5
0
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')
예제 #6
0
def display_articles_by_publication():
    publication_snippet = btc.read_text("Publication: ")
    publications = db.display_articles_by_publication(publication_snippet)
    if publications == None:
        print("There are no articles by that publication. article NOT found.\n")
    else:
        print()
        display_articles(publications, "PUBLICATION: " + str(publications[0].publication))
예제 #7
0
def display_articles_by_author():
    author_snippet = btc.read_text("Author Name: ")
    articles = db.display_articles_by_author(author_snippet)
    if articles == None:
        print("There are no articles by that author. article NOT found.\n")
    else:
        print()
        #display_single_article(article, str(article.id))
        display_articles(articles, "AUTHOR: " + str(articles[0].author))
예제 #8
0
def display_articles_by_name():
    title_snippet = btc.read_text('Enter article title or "." to cancel: ')
    if title_snippet != '.':
        result = db.display_article_by_name(title_snippet)
        if result == None:
            print('There is no article with that name.\n')
        else:
            display_single_article(result, str(result.id))
    else:
        print('Search cancelled, returning to main menu.')
예제 #9
0
def get_articles_by_category():
    category = btc.read_text("Enter category name or number here:  ")
    if category.isalpha() == True:
        display_articles_by_category_name(category)
    elif category.isnumeric() == True:
        try:
            category_id = int(category)
            display_articles_by_category_id(category_id)
            
        except:
            print('Article search cancelled. Return to main menu.\n')
예제 #10
0
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')
예제 #11
0
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')
예제 #12
0
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')
예제 #13
0
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)