|
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 |
7 | 3 |
|
8 | 4 |
|
9 | 5 | 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 | + |
11 | 23 | 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 = [] |
16 | 31 | for article in data["articles"]:
|
17 | 32 | temp = [article["author"], article["title"], article["description"], article["url"], article["urlToImage"],
|
18 | 33 | article["publishedAt"]]
|
19 |
| - finalList.append(temp) |
20 |
| - return (finalList) |
| 34 | + final_list.append(temp) |
| 35 | + return final_list |
21 | 36 |
|
22 | 37 |
|
23 | 38 | '''
|
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() |
40 | 50 | '''
|
0 commit comments