Skip to content

Commit 2c19c29

Browse files
author
thelostmonk
committed
changes in news_parser.py
1 parent 8f35f03 commit 2c19c29

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

hackrsource/MOC/db.sqlite3

0 Bytes
Binary file not shown.
+39-29
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
1-
'''
2-
Author : Dhiraj Subramanian B
3-
This python module will be used for requesting NewsApi for fetching latest news. The latest news will be then updated to the application database.
4-
'''
5-
6-
import requests, json
1+
import json
2+
import requests
73

84

95
class NewsParser(object):
10-
# two methods, for initializing the json and pushing it to db
6+
"""
7+
This class is used for parsing news articles from newsapi and is used for inserting the values to the database
8+
9+
Usage:
10+
news_parser = NewsParser(url = "some_valid_url", source = "source_of_news")
11+
result = news_parser.parse()
12+
"""
13+
14+
def __init__(self, url, source):
15+
"""
16+
17+
:param url: valid url string
18+
:param source: valid source of the url string
19+
"""
20+
self.url = url
21+
self.source = source
22+
1123
def parse(self):
12-
jsonResult = requests.get(
13-
"https://newsapi.org/v1/articles?source=the-verge&sortBy=top&apiKey=07dcf26d0ded41ba8436fb8bd233edba")
14-
data = json.loads(jsonResult.text)
15-
finalList = []
24+
"""
25+
26+
:return: this function returns list of lists. This can be parsed to push the values to the database
27+
"""
28+
json_result = requests.get(self.url)
29+
data = json.loads(json_result.text)
30+
final_list = []
1631
for article in data["articles"]:
1732
temp = [article["author"], article["title"], article["description"], article["url"], article["urlToImage"],
1833
article["publishedAt"]]
19-
finalList.append(temp)
20-
return (finalList)
34+
final_list.append(temp)
35+
return final_list
2136

2237

2338
'''
24-
#temp function to push to the values to models
25-
26-
def parse(url, source):
27-
jsonResult = requests.get(url)
28-
data = json.loads(jsonResult.text)
29-
for article in data["articles"]:
30-
author = article["author"]
31-
title = article["title"]
32-
description = article["description"]
33-
url = article["url"]
34-
urlToImage = article["urlToImage"]
35-
publishedAt = article["publishedAt"]
36-
if publishedAt != None:
37-
publishedAt = publishedAt[:publishedAt.find("T")]
38-
tempObj = tbl_MST_NewsArticle(author = author,title = title,description = description,url = url,urlToImage = urlToImage,publishedAt = publishedAt,source = source)
39-
tempObj.save()
39+
def store(self, final_list):
40+
for value in final_list:
41+
author = value[0]
42+
title = value[1]
43+
description = value[2]
44+
url = value[3]
45+
url_to_image = value[4]
46+
published_at = value[5]
47+
tempObj = NewsArticle(author=author, title=title, description=description, url=url,
48+
url_to_image=url_to_image, published_at=published_at, source=self.source)
49+
tempObj.save()
4050
'''

0 commit comments

Comments
 (0)