-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
67 lines (59 loc) · 2.19 KB
/
interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from bs4 import BeautifulSoup
import re
import requests
#Auxiliary function providing option start new search
def restart_session():
print("\nSearch Again ??")
se = input("Y : yes N: no ")
if se in ('Y','y'):
return -1
elif se in ('N','n'):
return 0
else:
print("Please provide valid input")
return restart_session()
#Menu Function that controls action based on user input
def menu(df,id):
print("\nEnter next task : ","1. View Additional info","2. Open another article","3. Search other article","4. Exit",sep = "\n")
op = int(input())
if op == 1:
print("\nPublication Date : ",df["info"][id+1]['publication date'])
print("\nDescription : ",df["info"][id+1]['description'])
return menu(df, id)
elif op == 2:
return view_article(df)
elif op == 3:
return restart_session()
elif op == 4:
return 0
else:
print("\n\nPlese provide valid input!!")
return menu(df,id)
#Function to parse contents from the selected website and display it
def view_article(df):
print("\nWhich article should i open ?" , end= "")
anum = int(input("Specify Number : "))
url = df["links"][anum]
r = requests.get(url) # recieve http response object and store it
html_str = r.text
soup = BeautifulSoup(html_str,'html.parser') # create beautiful soup object and parse the html string0
# get all the text content
textcontents = re.sub(r'\n\s*\n', r'\n\n', soup.get_text().strip(), flags=re.M)
print("\n\nHere are the contents of the website : \n\n\n")
print(textcontents)
return menu(df,anum-1)
#Function to generate table of articles and respective agencies from the dataframe recieved
def select_article(df,flag):
if flag == 0:
df.index = df.index+1
print("\nhere are some of the articles :: ")
print(df[["title","agency"]])
print("\nDo you want to view any article ? ",end="")
choice = input("Y : yes N: no ")
if choice in ('N','n'):
return restart_session()
elif choice in ('Y','y'):
return view_article(df)
else :
print("\n","please provide a valid input!!",sep ="")
return select_article(df,-1)