I am creating a GUI program using Tkinter wherein I utilise the Feedparser module.
My code executes in the following way:
Get the stock ticker to insert into the URL from the user. Parse the URL and append key data to a text file. Read the file and output it to a message box. At the moment, the order that the items are appended to the list are random, and when I output them, they are in a random order and have apostrophes and brackets. I want to output the title first, then the link, then the publishing date, and without any extra characters.
I feel like my code is inefficient, and that some external opinions and criticisms will assist in creating a better program.
Thank you to anyone who takes the time to look over my sub-par code, and I am terribly sorry if this is undecipherable.
Here is the code:
def news_feed(event):
''' This function creates a new window within the main window, passes an event(left mouse click), and creates a text heading'''
root = Toplevel(window)
# Create a text heading and define its placement within the grid
menu_heading = Label(root, text = 'News feed', font = 'bold')
menu_heading.grid(row = 0, column = 0, columnspan = 3, pady = 4)
# Create a variable of the selected radio button
button_choice = IntVar()
def selection():
''' This function gets the activated radio button and calls its corresponding function.'''
# Get the value of the activated radio button, and call its corresponding function
news_choice = button_choice.get()
# If the user's choice is industry news, ask them which feed they would like (E.g. Stock market),
if news_choice == 0:
# grab the corresponding url segment to the user's feed choice from the dictionary,
news_choice_url = news_areas[news_feed]
# set the url variable using by inserting this segment into the API url,
rss_url = feedparser.parse('https://au.finance.yahoo.com/news/' + news_choice_url + '/?format=rss')
# and call the feed parsing function.
parse_feed()
# If the user's choice is the second button, call the company news function
elif news_choice == 1:
company_news()
def read_news_file(news_feed_message):
'''This function opens the companyNews text file and reads its contents, line by line'''
with open('C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Text Files\\companyNews.txt', mode='r') as inFile:
news_data_read = inFile.read()
print('\n')
news_feed_message.configure(text=news_data_read)
def parse_feed(news_feed_message, rss_url):
''' This function parses the Yahoo! RSS API for data of the latest five articles, and writes it to the company news text file'''
# Define the RSS feed to parse from, as the url passed in of the company the user chose
feed = feedparser.parse(rss_url)
# Define the file to write the news data to the company news text file
outFile = open('C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Text Files\\companyNews.txt', mode='w')
# Create a list to store the news data parsed from the Yahoo! RSS
news_data_write = []
# Initialise a count
count = 0
# For the number of articles to append to the file, append the article's title, link, and published date to the news_elements list
for count in range(5):
news_data_write.append({feed['entries'][count].title,
feed['entries'][count].published})
# Add one to the count, so that the next article is parsed
count+=1
# For each item in the news_elements list, convert it to a string and write it to the company news text file
for item in news_data_write:
item = str(item)
outFile.write(item+'\n')
# For each article, write a new line to the company news text file, so that each article's data is on its own line
outFile.write('\n')
# Clear the news_elements list so that data is not written to the file more than once
del(news_data_write[:])
outFile.close()
read_news_file(news_feed_message)
def industry_news():
''' This function creates a new window within the main window, and displays industry news'''
industry_window = Toplevel(root)
Label(industry_window, text = 'Industry news').grid()
def company_news():
''' This function creates a new window within the main window, and displays company news'''
company_window = Toplevel(root)
company_label = Label(company_window, text = 'Company news')
company_label.grid(row = 0, column = 0, columnspan = 6)
def company_news_handling(company_ticker):
''' This function gets the input from the entry widget (stock ticker) to be graphed.'''
# set the url variable by inserting the stock ticker into the API url,
rss_url = ('http://finance.yahoo.com/rss/headline?s={0}'.format(company_ticker))
# and call the feed parsing function.
parse_feed(news_feed_message, rss_url)
# Create the entry widget where the user enters a stock ticker, and define its location within the grid
company_ticker_entry = Entry(company_window)
company_ticker_entry.grid(row = 1, column = 0, columnspan = 6, padx = 10)
def entry_handling():
'''This function validates the input of the entry box, and if there is nothing entered, an error is outputted until a value is'''
# Create a variable that equals the input from the entry widget
company_ticker = company_ticker_entry.get()
# Convert the input into a string
company_ticker = str(company_ticker)
if company_ticker == '':
news_feed_message.configure(text = 'Please input a stock ticker in the entry box.')
else:
company_news_handling(company_ticker)
# Create the button that the user presses when they wish to graph the data of the stock ticker they inputted in the entry widget
graph_button = Button(company_window, text = 'SHOW', command = entry_handling, width = 10).grid(row = 2, column = 0, columnspan = 6)
news_feed_message = Message(company_window, text='', width=500, borderwidth=5, justify=LEFT, relief=RAISED)
news_feed_message.grid(row=3, column=0, columnspan=6)
# Create a radio button for the industry news option and define its placement within the grid
industry_button = Radiobutton(root, text = 'Industry news', variable = button_choice, value = 0, indicatoron = 0, width = 20)
industry_button.grid(row = 1, column = 0, columnspan = 3)
# Create a radio button for the company news option and define its placement within the grid
company_button = Radiobutton(root, text = 'Company news', variable = button_choice, value = 1, indicatoron = 0, width = 20)
company_button.grid(row = 2, column = 0, columnspan = 3)
# Create a button that the user presses to finalise their choice and that calls the selection function
selection_button = Button(root, text="Select", command = selection, width = 15).grid(row = 3, column = 0, columnspan = 3, padx = 5, pady = 5)
# Create a variable assigned to the location of the main image, and then change its proportions (the higher the number/s, the smaller)
finance_news_image1 = PhotoImage(file = 'C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Images\\finance news.gif')
finance_news_image2 = finance_news_image1.subsample(3, 3)
# Create a variable for the main image of the menu, and define its placement within the grid
main_photo = Label(root, image = finance_news_image2)
main_photo.image = finance_news_image2
main_photo.grid(row = 4, column = 0)
# Create a variable assigned to the location of an image, and then change its proportions (the higher the number/s, the smaller)
news_feed_photo1 = PhotoImage(file = 'C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Images\\news feed.gif')
news_feed_photo2 = news_feed_photo1.subsample(8, 8)
'''
Create a button and set its parameters,
define its placement within the grid,
and bind the button to an event (left mouse click) that calls the function to run the news feed option.
'''
news_feed_button = Button(window, image = news_feed_photo2, compound = LEFT, text = 'News feed', width = 180)
news_feed_button.grid(row = 3, columnspan=2, pady = 1)
news_feed_button.bind('<Button-1>', news_feed)