forked from anuragrana/Python-Scripts
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomatedTelegramChannel.py
More file actions
Latest commit
61 lines (48 loc) · 1.72 KB
/
Copy pathAutomatedTelegramChannel.py
File metadata and controls
61 lines (48 loc) · 1.72 KB
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
# Author - Anurag Rana
frombs4importBeautifulSoup
importrequests
importtelegram
fromconfigimporttelegram_token_news
defget_news_data(starting_url):
news_data= []
response=requests.get(starting_url)
soup=BeautifulSoup(response.text, 'lxml')
element=soup.find(attrs={"class": "deQdld"})
print(element)
forc_wiziinelement:
data= {}
a=c_wizi.find(attrs={"class":"nuEeue hzdq5d ME7ew"})
ifa:
try:
link=a.attrs["href"]
text=a.string.strip()
exceptExceptionase:
print(e)
iflinkandtext:
data["link"] =link
data["text"] =text
news_data.append(data)
returnnews_data
defget_msg(news_data):
msg="\n\n\n"
fornews_iteminnews_data:
text=news_item["text"]
link=news_item["link"]
msg+=text+' [<a href="'+link+'">source</a>]'
msg+="\n\n"
returnmsg
bot=telegram.Bot(token=telegram_token_news)
urls= [
'https://news.google.com/news/headlines/section/topic/NATION.hi_in/%E0%A4%AD%E0%A4%BE%E0%A4%B0%E0%A4%A4?ned=hi_in&hl=hi',
'https://news.google.com/news/headlines/section/topic/NATION.en_in/India?ned=in&hl=en-IN',
'https://news.google.com/news/headlines/section/topic/ENTERTAINMENT.en_in/Entertainment?ned=in&hl=en-IN',
]
forurlinurls:
news_data=get_news_data(url)
iflen(news_data) >0:
msg=get_msg(news_data)
status=bot.send_message(chat_id="@newsindiachannel", text=msg, parse_mode=telegram.ParseMode.HTML)
ifstatus:
print(status)
else:
print("no new news")