-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
168 lines (152 loc) · 4.88 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
import asyncio
from cgitb import text
from os import stat
import telepot
import telepot.aio
from telepot.aio.loop import MessageLoop
from bs4 import BeautifulSoup
import requests
async def handle(msg):
global chat_id
global state, city, crop
# These are some useful variables
content_type, chat_type, chat_id = telepot.glance(msg)
# Log variables
print(content_type, chat_type, chat_id)
text = msg['text'].split(' ')
print(text)
command = text[0]
username = msg['chat']['first_name']
# Check that the content type is text and not the starting
if content_type == 'text':
if command == '/start':
await bot.sendMessage(chat_id, 'enter name of state city to get rates via /state, /city, /crop commands')
# await bot.getUpdates()
if command == '/state':
state = text[1]
# await bot.sendMessage(chat_id, 'enter name of state')
# await bot.getUpdates()
if command == '/city':
city = text[1]
# await bot.sendMessage(chat_id, 'enter name of city')
# await bot.getUpdates()
if command == '/crop':
crop = text[1]
# await bot.sendMessage(chat_id, 'rates found!')
if command == '/getrates':
await getrates(state+'/'+city+'/'+crop)
if command == '/weather':
await weather(state+'/'+city)
async def getrates(params):
# create url
url = 'https://agriplus.in/mandi/' + params
# define headers
headers = {'User-Agent': 'Generic user agent'}
# get page
page = requests.get(url, headers=headers)
# let's soup the page
soup = BeautifulSoup(page.text, 'html.parser')
# print(soup)
try:
try:
# get definition
title = soup.find(
'thead', {'th': ''}).text
print(title)
rates = soup.find(
'tbody', {'tr': ''}).text
print(rates)
data = title+'\n'+rates
# send message
await bot.sendMessage(chat_id, data)
except:
await bot.sendMessage(chat_id, 'rates not found!')
except:
await bot.sendMessage(chat_id, 'Something went wrong...')
async def weather(params):
# create url
url = 'https://www.theweathernetwork.com/in/weather/' + params
# define headers
headers = {'User-Agent': 'Generic user agent'}
# get page
page = requests.get(url, headers=headers)
# let's soup the page
soup = BeautifulSoup(page.text, 'html.parser')
# print(soup)
try:
try:
# get definition
data = soup.find(
'div', attrs={'class': 'obs-area'}).text
print(data)
# send message
await bot.sendMessage(chat_id, data)
except:
await bot.sendMessage(chat_id, 'forecast not found!')
except:
await bot.sendMessage(chat_id, 'Something went wrong...')
# Program startup
TOKEN = 'api token'
bot = telepot.aio.Bot(TOKEN)
loop = asyncio.get_event_loop()
loop.create_task(MessageLoop(bot, handle).run_forever())
print('Listening ...')
# Keep the program running
loop.run_forever()
'''
import asyncio
import telepot
import telepot.aio
from telepot.aio.loop import MessageLoop
from bs4 import BeautifulSoup
import requests
async def handle(msg):
global chat_id
# These are some useful variables
content_type, chat_type, chat_id = telepot.glance(msg)
# Log variables
print(content_type, chat_type, chat_id)
print(msg)
username = msg['chat']['first_name']
# Check that the content type is text and not the starting
if content_type == 'text':
if msg['text'] != '/start':
text = msg['text']
# it's better to strip and lower the input
text = text.strip()
await getrates(text.lower())
async def getrates(text):
# create url
url = 'https://agriplus.in/mandi/haryana/shahbad/' + text
# define headers
headers = {'User-Agent': 'Generic user agent'}
# get page
page = requests.get(url, headers=headers)
# let's soup the page
soup = BeautifulSoup(page.text, 'html.parser')
# print(soup)
try:
try:
# get definition
title = soup.find(
'thead', {'th': ''}).text
print(title)
rates = soup.find(
'tbody', {'tr': ''}).text
print(rates)
data = title+'\n'+rates
# send message
await bot.sendMessage(chat_id, data)
except:
await bot.sendMessage(chat_id, 'rates not found!')
except:
await bot.sendMessage(chat_id, 'Something went wrong...')
# Program startup
TOKEN = 'api token'
bot = telepot.aio.Bot(TOKEN)
loop = asyncio.get_event_loop()
loop.create_task(MessageLoop(bot, handle).run_forever())
print('Listening ...')
# Keep the program running
loop.run_forever()
'''