-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
183 lines (133 loc) · 5.73 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from flask import Flask, render_template
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
@app.route("/")
def index() :
return render_template("index.html")
def generateNewsdiv(pageLink, category, div, divClass, image, imageClass, header, headerClass, para, paraClass, link, linkClass, categoryType) :
url = pageLink + category
page = requests.get(url)
htmlContent = page.content
soup = BeautifulSoup(htmlContent, "html.parser")
newsDiv = soup.find_all(div, {"class" : divClass})
mainNewsList, counter = [], 0
for div in newsDiv :
if categoryType :
if counter == 4 :
break
else :
if counter == 1 :
break
subNewsList = []
try :
imageDiv = div.find(image, {"class" : imageClass} )
image = div.find("img")
subNewsList.append(image.get('src'))
except :
image = div.find("img", {"class" : imageClass})
subNewsList.append(image.get('src'))
try :
headData = div.find(header, {"class" : headerClass})
subNewsList.append(headData.text)
except :
headData = div.find(header)
subNewsList.append(headData.text)
paraData = div.find(para)
subNewsList.append(paraData.text)
linkData = div.find(link)["href"]
subNewsList.append(url+linkData)
mainNewsList.append(subNewsList)
counter += 1
return mainNewsList
def categoryNews(category) :
linkList = ["https://www.indiatvnews.com/", "https://indianexpress.com/section/",
"https://www.firstpost.com/category/", "https://www.indiatoday.in/"]
main_news = []
for i in range(len(linkList)) :
if i == 0 :
sub_news = generateNewsdiv(linkList[i], category,
"li", "p_news", "a", "thumb",
"h3", "title", "p", "dic", "a", None, False)
main_news.append(sub_news)
if i == 1 and (category == 'india' or category == 'sports'):
sub_news = generateNewsdiv(linkList[i], category,
"div", "articles", "div", "snaps",
"h2", "title", "p", None, "a", None, False)
main_news.append(sub_news)
if i == 2 :
sub_news = generateNewsdiv(linkList[i], category,
"div", "big-thumb", "a", "thumb-img",
"h3", "main-title", "p", "copy", "a", None, False)
main_news.append(sub_news)
if i == 3 and category != 'sports' :
sub_news = generateNewsdiv(linkList[i], category,
"div", "catagory-listing", "div", "pic",
"h2", None, "p", None, "a", None, False)
main_news.append(sub_news)
return main_news
@app.route("/indiatoday")
def indiatoday() :
newsList = ["india", "world"]
main_news = []
for i in range(len(newsList)) :
sub_news = generateNewsdiv("https://www.indiatoday.in/", newsList[i],
"div", "catagory-listing", "div", "pic",
"h2", None, "p", None, "a", None, True)
main_news.append(sub_news)
main_news.append('India Today')
main_news.append('static\images\indiatoday-logo.jpg')
return render_template('indiatoday.html', News = main_news)
@app.route("/firstpost")
def firstpost() :
newsList = ["india", "world"]
main_news = []
for i in range(len(newsList)) :
sub_news = generateNewsdiv("https://www.firstpost.com/category/", newsList[i],
"div", "big-thumb", "a", "thumb-img",
"h3", "main-title", "p", "copy", "a", None, True)
main_news.append(sub_news)
main_news.append('First Post')
main_news.append('static\images\Firstpost-logo-vector.png')
return render_template('indiatoday.html', News = main_news)
@app.route("/the-indian-express")
def theIndianExpress() :
newsList = ["india", "cities"]
main_news = []
for i in range(len(newsList)) :
sub_news = generateNewsdiv("https://indianexpress.com/section/", newsList[i],
"div", "articles", "div", "snaps",
"h2", "title", "p", None, "a", None, True)
main_news.append(sub_news)
main_news.append('The Indian Express')
main_news.append('static\images\TIE.jpg')
return render_template('indiatoday.html', News = main_news)
@app.route("/indiatv")
def indiatv() :
newsList = ["india", "world"]
main_news = []
for i in range(len(newsList)) :
sub_news = generateNewsdiv("https://www.indiatvnews.com/", newsList[i],
"li", "p_news", "a", "thumb",
"h3", "title", "p", "dic", "a", None, True)
main_news.append(sub_news)
main_news.append('India TV')
main_news.append('static\images\indiatv.jpg')
return render_template('indiatoday.html', News = main_news)
@app.route("/india")
def india() :
subList = categoryNews("india")
subList.append('India')
return render_template('india.html', News = subList)
@app.route("/world")
def world() :
subList = categoryNews("world")
subList.append('World')
return render_template('india.html', News = subList)
@app.route("/Sports")
def Sports() :
subList = categoryNews("sports")
subList.append('Sports')
return render_template('india.html', News = subList)
if __name__ == "__main__" :
app.run(debug= True)