def manual_add(link=None): ''' Add a read_bool choice at the end to confirm that the user wants to add the article. ''' display_categories() try: category = btc.read_int('Enter category for article: ') except Exception as e: print(e) return category = db.cat_from_snippet(category, numeric_snippet=True) if category == None: print('Article creation cancelled') return new_article = Article.manual_add(link=link, category=category) #display the new article before the user makes the decision to confirm confirm_article = btc.read_bool(decision="Finalize the article?", yes='y', no='n', yes_option='Confirm', no_option='Cancel') if confirm_article == True: db.add_article(new_article) print(new_article.title + " was added to database.\n") else: print('Add article cancelled. Return to main menu.') return
def get_articles_for_roundup2(start_date, end_date, category_id): ''' Do not mess with this function without absolute certainty that you will not break the roundup generation process. ''' columns = [ articles_table.c.articleID, articles_table.c.name, articles_table.c.date, articles_table.c.categoryID, articles_table.c.link, articles_table.c.description, articles_table.c.publication, articles_table.c.author, categories_table.c.category_name ] s = select(columns) s = s.select_from(articles_table.join(categories_table)).where( and_(articles_table.c.date >= start_date, articles_table.c.date <= end_date, categories_table.c.categoryID == category_id)) #articles_table.c.year == roundup_year, articles_table.c.categoryID == category_id)) rp = connection.execute(s) #results = rp.fetchall() articles_for_roundup = [ Article.from_sqlalchemy(articleID=row.articleID, name=row.name, date=row.date, link=row.link, description=row.description, author=row.author, categoryID=row.categoryID, category_name=row.category_name, publication=row.publication) for row in rp ] return articles_for_roundup
def get_article(article_id): print(articles_table.c.articleID) columns = [ articles_table.c.articleID, articles_table.c.name, articles_table.c.link, articles_table.c.date, articles_table.c.description, articles_table.c.categoryID, categories_table.c.category_name, articles_table.c.author, articles_table.c.publication ] s = select(columns) s = s.select_from(articles_table.join(categories_table)).where( articles_table.c.articleID == article_id) rp = connection.execute(s).fetchone() try: #ew_article = make_article(rp) new_article = Article.from_sqlalchemy(articleID=rp.articleID, name=rp.name, date=rp.date, link=rp.link, description=rp.description, author=rp.author, categoryID=rp.categoryID, category_name=rp.category_name, publication=rp.publication) return new_article except TypeError: print('Type error') return except AttributeError: print('Attribute error') return
def display_articles_by_author(author_snippet): ''' This function is intended to display articles based on partial publication titles. ''' columns = [ articles_table.c.articleID, articles_table.c.name, articles_table.c.link, articles_table.c.date, articles_table.c.description, articles_table.c.categoryID, categories_table.c.category_name, articles_table.c.author, articles_table.c.publication ] s = select(columns) s = s.select_from(articles_table.join(categories_table)).where( articles_table.c.author.ilike("%{0}%".format(author_snippet))) rp = connection.execute(s).fetchall() articles_by_name = [] for i in rp: new_article = Article.from_sqlalchemy(articleID=i.articleID, name=i.name, date=i.date, link=i.link, description=i.description, author=i.author, categoryID=i.categoryID, category_name=i.category_name, publication=i.publication) articles_by_name.append(new_article) return articles_by_name
def display_articles_by_category_id(start_date, end_date, category_id): columns = [ articles_table.c.articleID, articles_table.c.name, articles_table.c.date, articles_table.c.categoryID, articles_table.c.link, articles_table.c.description, articles_table.c.publication, articles_table.c.author, categories_table.c.category_name ] s = select(columns) s = s.select_from(articles_table.join(categories_table)).where( and_(articles_table.c.date >= start_date, articles_table.c.date <= end_date, categories_table.c.categoryID == category_id)) #articles_table.c.year == roundup_year, articles_table.c.categoryID == category_id)) rp = connection.execute(s) #results = rp.fetchall() articles_by_categoryID = [ Article.from_sqlalchemy(articleID=row.articleID, name=row.name, date=row.date, link=row.link, description=row.description, author=row.author, categoryID=row.categoryID, category_name=row.category_name, publication=row.publication) for row in rp ] return articles_by_categoryID
def get_articles_by_date_range(start_date, end_date): columns = [ articles_table.c.articleID, articles_table.c.name, articles_table.c.link, articles_table.c.date, articles_table.c.description, articles_table.c.categoryID, categories_table.c.category_name, articles_table.c.author, articles_table.c.publication ] s = select(columns) s = s.select_from(articles_table.join(categories_table)).where( and_(articles_table.c.date >= start_date, articles_table.c.date <= end_date)) #s = s.select_from(articles_table.join(categories_table)).where(articles_table.c.date == article_date) rp = connection.execute(s).fetchall() articles_by_date_range = [ Article.from_sqlalchemy(articleID=row.articleID, name=row.name, date=row.date, link=row.link, description=row.description, author=row.author, categoryID=row.categoryID, category_name=row.category_name, publication=row.publication) for row in rp ] #articles_by_date = [make_article(row) for row in rp] return articles_by_date_range
def add_article(document, article): #print(article) try: new_paragraph = document.add_paragraph( '') #add blank paragraph that we append the text to add_hyperlink(paragraph=new_paragraph, text=article.name, url=article.link) #print(Article.get_date_formatted(article)) new_paragraph.add_run(' ({0}) '.format( Article.get_date_formatted( article))) #blank space between the link and the description new_paragraph.add_run(article.description) except Exception as e: print(e)
def csv_item_to_article(csv_list_item): new_article_news_item = na.get_article_from_url(csv_list_item[0]) new_article_link = new_article_news_item.url new_article_title = new_article_news_item.title print(new_article_title) new_article_category = get_category_id(csv_list_item[1]) new_article_datetime = parse(csv_list_item[2]) new_article_date = new_article_datetime.date() article_from_csv = Article(name=new_article_title, link=new_article_link, category=new_article_category, date=new_article_date, description='Not specified', author='Not specified', publication='Not specified') return article_from_csv
def get_articles_range(range_low, range_high=None, range_type='article_id'): #range_types = [{'date': , 'article_id'}] '''Get the articles from a range of values, such as a range of dates or a range of article IDs.''' print(dal.articles_table.c.articleID) columns = [ dal.articles_table.c.articleID, dal.articles_table.c.name, dal.articles_table.c.link, dal.articles_table.c.date, dal.articles_table.c.description, dal.articles_table.c.categoryID, dal.categories_table.c.category_name, dal.articles_table.c.author, dal.articles_table.c.publication ] s = select(columns) if range_type == 'article_id': if range_high == None: s = s.select_from(dal.articles_table.join( dal.categories_table)).where( dal.articles_table.c.articleID == range_low) else: s = s.select_from(dal.articles_table.join( dal.categories_table)).where( and_(dal.articles_table.c.articleID >= range_low, dal.articles_table.c.articleID <= range_high)) elif range_type == 'date': if range_high == None: s = s.select_from(dal.articles_table.join( dal.categories_table)).where( dal.articles_table.c.date == range_low) else: s = s.select_from(dal.articles_table.join( dal.categories_table)).where( and_(dal.articles_table.c.date >= range_low, dal.articles_table.c.date <= range_high)) rp = dal.connection.execute(s).fetchall() articles_by_range = [ Article.from_sqlalchemy(articleID=row.articleID, name=row.name, date=row.date, link=row.link, description=row.description, author=row.author, categoryID=row.categoryID, category_name=row.category_name, publication=row.publication) for row in rp ] return articles_by_range
def display_article_by_name(title_snippet): ''' This function is intended to facilitate the search for articles using partial titles ''' stmt = select([articles_table]).\ where(articles_table.c.name.ilike("%{0}%".format(title_snippet))) rp = connection.execute(stmt).fetchone() try: #article_by_name = make_article(rp) article_by_name = Article.from_sqlalchemy( articleID=rp.articleID, name=rp.name, date=rp.date, link=rp.link, description=rp.description, author=rp.author, categoryID=rp.categoryID, category_name=rp.category_name, publication=rp.publication) return article_by_name except Exception as e: print(e) return
def manual_add(link=None): category = btc.read_int('Enter category for article: ') category = db.get_category(category) new_article = Article.manual_add(link=link, category=category) db.add_article(new_article) print(new_article.title + " was added to database.\n")
def from_newspaper(link): ''' Adds an article from the newspaper module after downloading it ''' for i in tqdm.tqdm(range(1)): try: newNewsItem = na.get_article_from_url(link) except: print('Article download failed, invalid URL') print('Returning to main menu') return print(newNewsItem) try: name = newNewsItem.title #get the title for the article 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 month = newNewsItem.publish_date.month day = newNewsItem.publish_date.day new_date = datetime.date(day=day, month=month, year=year) except Exception as e: print(e) #use the new btc.read_date() function to simplify this try: new_date = btc.read_date('Enter article date MM/DD/YYYY: ') except Exception as e: print('invalid date', e) 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} - PUBLICATION {1}'.format(new_date.strftime("%m/%d/%Y"), publication)) 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: new_article = Article(name=name, date=new_date, category=category, link=link, description=description, author=author, publication=publication) display_single_article(article=new_article, title_term=new_article.name) confirm_article = btc.read_bool(decision="Finalize the article?", yes='y', no='n', yes_option='Confirm', no_option='Cancel') #This is the user's last chance to decide if they want to add the article if confirm_article == True: db.add_article(new_article) print(new_article.name + " was added to database.\n") elif confirm_article == False: print('Article add cancelled. Return to main menu.')
def get_snippet(snippet, snippet_type, start_date=None, end_date=None): columns = [ articles_table.c.articleID, articles_table.c.name, articles_table.c.link, articles_table.c.date, articles_table.c.description, articles_table.c.categoryID, categories_table.c.category_name, articles_table.c.author, articles_table.c.publication ] s = select(columns) if snippet_type == 'title': #the snippet type is named title, but the field in the articles table is called name if (start_date == None) or (end_date == None): s = s.select_from(articles_table.join(categories_table)).where( articles_table.c.name.ilike("%{0}%".format(snippet))) else: print(start_date, end_date) s = s.select_from(articles_table.join(categories_table)).where( and_(articles_table.c.date >= start_date, articles_table.c.date <= end_date, articles_table.c.name.ilike("%{0}%".format(snippet)))) elif snippet_type == 'description': if (start_date == None) or (end_date == None): s = s.select_from(articles_table.join(categories_table)).where( articles_table.c.description.ilike("%{0}%".format(snippet))) else: s = s.select_from(articles_table.join(categories_table)).where( and_( articles_table.c.date >= start_date, articles_table.c.date <= end_date, articles_table.c.description.ilike( "%{0}%".format(snippet)))) #s = s.select_from(articles_table.join(categories_table)).where(articles_table.c.description.ilike("%{0}%".format(snippet))) elif snippet_type == 'category': if (start_date == None) or (end_date == None): s = s.select_from(articles_table.join(categories_table)).where( categories_table.c.category_name.ilike( "%{0}%".format(snippet))) else: s = s.select_from(articles_table.join(categories_table)).where( and_( articles_table.c.date >= start_date, articles_table.c.date <= end_date, categories_table.c.category_name.ilike( "%{0}%".format(snippet)))) elif snippet_type == 'publication': if (start_date == None) or (end_date == None): s = s.select_from(articles_table.join(categories_table)).where( articles_table.c.publication.ilike("%{0}%".format(snippet))) else: s = s.select_from(articles_table.join(categories_table)).where( and_( articles_table.c.date >= start_date, articles_table.c.date <= end_date, articles_table.c.publication.ilike( "%{0}%".format(snippet)))) elif snippet_type == 'author': if (start_date == None) or (end_date == None): s = s.select_from(articles_table.join(categories_table)).where( articles_table.c.author.ilike("%{0}%".format(snippet))) else: s = s.select_from(articles_table.join(categories_table)).where( and_(articles_table.c.date >= start_date, articles_table.c.date <= end_date, articles_table.c.author.ilike("%{0}%".format(snippet)))) elif snippet_type == 'category_id': if (start_date == None) or (end_date == None): s = s.select_from(articles_table.join(categories_table)).where( categories_table.c.categoryID == snippet) else: s = s.select_from(articles_table.join(categories_table)).where( and_(articles_table.c.date >= start_date, articles_table.c.date <= end_date, categories_table.c.categoryID == snippet)) pass else: print('Incorrect snippet type, return to main menu') return rp = connection.execute(s).fetchall() articles_by_snippet = [ Article.from_sqlalchemy(articleID=row.articleID, name=row.name, date=row.date, link=row.link, description=row.description, author=row.author, categoryID=row.categoryID, category_name=row.category_name, publication=row.publication) for row in rp ] return articles_by_snippet