44from .utils import editionMap , topicMap , langMap
55from .userexception import NotFound
66
7+
78class gnewsclient :
8-
9- def __init__ (self , edition = 'United States (English)' ,
10- topic = 'top stories' , location = None ,
11- query = None , language = 'english' ):
9+
10+ def __init__ (self , edition = 'United States (English)' ,
11+ topic = 'top stories' , location = None ,
12+ query = None , language = 'english' ):
1213 '''
1314 constructor function
1415 '''
1516 # list of editions and topics
1617 self .editions = list (editionMap )
1718 self .topics = list (topicMap )
1819 self .languages = list (langMap )
19-
20+
2021 # default parameter values
2122 self .edition = edition
2223 self .topic = topic
2324 self .location = location
2425 self .query = query
2526 self .language = language
26-
27+
2728 # parameters to be passed in HTTP request
2829 self .params = {'output' : 'atom' ,
2930 'ned' : self .edition ,
3031 'topic' : self .topic ,
3132 'geo' : self .location ,
3233 'q' : self .query ,
3334 'hl' : self .language }
34-
35-
35+
3636 def get_config (self ):
3737 '''
3838 function to get current configuration
@@ -55,74 +55,73 @@ def reset(self):
5555 self .location = None
5656 self .query = None
5757 self .topic = 'top stories'
58-
59-
58+
6059 def get_news (self ):
6160 '''
6261 function to fetch news articles
6362 '''
6463 status = self .set_params ()
6564 # params not set properly
66- if status == False :
65+ if status is False :
6766 return
6867
6968 soup = self .load_feed ()
7069 articles = self .scrape_feed (soup )
7170 return articles
72-
73-
71+
7472 def set_params (self ):
7573 '''
7674 function to set params for HTTP request
7775 '''
78-
76+
7977 # setting edition
8078 try :
8179 self .params ['ned' ] = editionMap [self .edition ]
8280 except KeyError :
83- print ("{} edition not found.\n Use editions attribute to get list of editions." .format (self .edition ))
81+ print (f"{ self .edition } edition not found.\n "
82+ f"Use editions attribute to get list of editions." )
8483 return False
85-
84+
8685 # setting topic
8786 try :
8887 self .params ['topic' ] = topicMap [self .topic ]
8988 except KeyError :
90- print ("{} topic not found.\n Use topics attribute to get list of topics." .format (self .topic ))
89+ print (f"{ self .topic } topic not found.\n "
90+ f"Use topics attribute to get list of topics." )
9191 return False
92-
92+
9393 # setting language
9494 try :
9595 self .params ['hl' ] = langMap [self .language ]
9696 except KeyError :
97- print ("{} language not found.\n Use langugaes attribute to get list of languages." .format (self .language ))
97+ print (f"{ self .language } language not found.\n "
98+ f"Use langugaes attribute to get list of languages." )
9899 return False
99-
100+
100101 # setting query
101- if self .query != None :
102+ if self .query is not None :
102103 self .params ['q' ] = self .query
103104 # topic overrides query parameter. So, clearing it.
104105 self .params ['topic' ] = None
105-
106+
106107 # setting location
107- if self .location != None :
108+ if self .location is not None :
108109 self .params ['geo' ] = self .location
109110 # topic overrides location parameter. So, overriding it.
110111 self .params ['topic' ] = None
111112
112113 # params setting successful
113114 return True
114-
115-
115+
116116 def load_feed (self ):
117117 '''
118118 function to load atom feed
119119 '''
120120 url = "https://news.google.com/news"
121- resp = requests .get (url , params = self .params )
121+ resp = requests .get (url , params = self .params )
122122 soup = BeautifulSoup (resp .content , 'html5lib' )
123123 return soup
124-
125-
124+
126125 def scrape_feed (self , soup ):
127126 '''
128127 function to scrape atom feed
@@ -134,16 +133,19 @@ def scrape_feed(self, soup):
134133 article = {}
135134 article ['title' ] = entry .title .text
136135 article ['link' ] = entry .link ['href' ].split ('&url=' )[1 ]
136+ article ['releasedAt' ] = entry .updated .text
137+
137138 try :
138- article ['img' ] = "https:" + entry .content .text .split ('src=\" ' )[1 ].split ('\" ' )[0 ]
139- except :
139+ string = entry .content .text .split ('src=\" ' )[1 ].split ('\" ' )[0 ]
140+ article ['img' ] = "https:" + string
141+ except Exception :
140142 article ['img' ] = None
141143 pass
142144 articles .append (article )
143145 try :
144- if len (articles )== 0 :
146+ if len (articles ) == 0 :
145147 raise NotFound
146148 except NotFound :
147149 print ("The articles for the given response are not found." )
148150 return
149- return articles
151+ return articles
0 commit comments