Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.8
- # -*- coding: utf-8 -*-
- import time
- start_time = time.time()
- import warnings
- warnings.filterwarnings("ignore")
- import locale
- locale.setlocale(locale.LC_ALL, '')
- import math
- import json
- import re
- import sys
- import requests
- import datetime
- from datetime import date
- from math import radians as rad, degrees as deg
- from grab import Grab
- import optparse
- first_re = re.compile(r'^\d{3}$')
- parser = optparse.OptionParser()
- parser.set_defaults(temperature=False, pressures=False, clouds=False)
- parser.add_option('-t',
- '--temperature',
- action='store_true',
- dest='temperature')
- parser.add_option('-p', '--pressures', action='store_true', dest='pressures')
- parser.add_option('-c', '--clouds', action='store_true', dest='clouds')
- parser.add_option('-w', '--wind ', action='store_true', dest='winds')
- parser.add_option('-s', '--wspeed', action='store_true', dest='wind speed ')
- (options, args) = parser.parse_args()
- localtime = time.localtime(time.time())
- hour = localtime[3]
- minute = localtime[4]
- headers = {
- 'User-Agent': ('Mozilla/5.0 (Windows NT 6.0; rv:14.0) Gecko/20100101 '
- 'Firefox/14.0.1'),
- 'Accept':
- 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
- 'Accept-Language':
- 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3',
- 'Accept-Encoding':
- 'gzip, deflate',
- 'Connection':
- 'keep-alive',
- 'DNT':
- '1'
- }
- cmd = 'sensorsNearby'
- uuid = 'you_uuid' # narodmon.ru/api
- api_key = 'you_apikey' # narodmon.ru/api
- appid = 'you_appid' # narodmon.ru/api
- # Ленинск-Кузнецкий lat=54&lon=86
- lat = '54.65' # Home 9 86.184826%2C54.681399
- lng = '86.18' # Home 9 lat=54.643689&lon=86.199094
- lang = 'ru'
- radius = 50
- types = '1,2'
- geturi = f'http://narodmon.ru/api/sensorsNearby?lat={lat}&lng={lng}&radius={radius}&uuid={uuid}&api_key={api_key}&lang={lang}'
- #print (geturi)
- windy = Grab(timeout=300)
- windy.go("https://yandex.ru/pogoda/leninsk-kuznetskiy/details")
- davl = ''
- def pressave():
- my_file = open("/home/pressure.txt", "r")
- pressave = float(my_file.read())
- my_file.close()
- return pressave
- def windspeed():
- try:
- speed = windy.xpath_text('//*[@class="wind-speed"]')
- speed = re.sub(r',', '.', speed)
- speed = round(int(float(speed)), 0)
- except IndexError:
- speed = 0
- return speed
- def pole():
- try:
- pole = windy.xpath_list(
- '//*[@class="forecast-fields__value"]')[1].text_content()
- except IndexError:
- pole = "Нет данных"
- return pole
- def wind():
- try:
- direction = windy.xpath_text('//*[@class=" icon-abbr"]')
- except IndexError:
- direction = "Штиль"
- if direction == "С":
- direction = "Северный"
- elif direction == "СВ":
- direction = "Северо-восточный"
- elif direction == "В":
- direction = "Восточный"
- elif direction == "ЮВ":
- direction = "Юго-восточный"
- elif direction == "Ю":
- direction = "Южный"
- elif direction == "ЮЗ":
- direction = "Юго-западный"
- elif direction == "З":
- direction = "Западный"
- elif direction == "СЗ":
- direction = "Северо-западный"
- return direction
- def cloudy():
- cloud = Grab(timeout=300)
- cloud.go("https://yandex.ru/pogoda/leninsk-kuznetskiy")
- cloud = cloud.xpath_text('//*[@class="link__feelings fact__feelings"]')
- cloud = re.sub(r"Ощущается как.*", "", cloud)
- #print(cloud)
- return cloud
- def pressure():
- WeHtm = requests.post(geturi, headers=headers).text
- fact = json.loads(WeHtm)
- devd = 2
- sensd = 1
- try:
- davl1 = fact['devices'][devd]['sensors'][sensd]['value']
- locat = fact['devices'][devd]['location']
- except IndexError:
- devd = 4
- davl1 = fact['devices'][devd]['sensors'][0]['value']
- locat = fact['devices'][devd]['location']
- except IndexError:
- devd = 3
- davl1 = fact['devices'][devd]['sensors'][sensd]['value']
- locat = fact['devices'][devd]['location']
- except IndexError:
- devd = 2
- davl1 = fact['devices'][devd]['sensors'][sensd]['value']
- locat = fact['devices'][devd]['location']
- except IndexError:
- devd = 1
- davl1 = fact['devices'][devd]['sensors'][sensd]['value']
- locat = fact['devices'][devd]['location']
- davl = davl1
- return davl, locat
- def temper():
- WeHtm = requests.post(geturi, headers=headers).text
- devt = -1
- tra = 0
- senst = 0
- fact = json.loads(WeHtm)
- unit = fact['devices'][devt]['sensors'][senst]['unit']
- if unit == '°':
- tra = 0
- else:
- tra = 1
- temp1 = fact['devices'][devt]['sensors'][tra]['value']
- if temp1 > 40:
- devt = 3
- temp1 = fact['devices'][devt]['sensors'][tra]['value']
- if temp1 > 40:
- devt = 2
- temp1 = fact['devices'][devt]['sensors'][tra]['value']
- if temp1 > 40:
- devt = 1
- temp1 = fact['devices'][devt]['sensors'][tra]['value']
- if temp1 > 40:
- devt = 0
- temp1 = fact['devices'][devt]['sensors'][tra]['value']
- return temp1
- # ⇑⇓●
- def comparison():
- if pressave() > pressure()[0]:
- comp = "Понижение ▼"
- if pressave() == pressure()[0]:
- comp = "Неизменно ●"
- if pressave() < pressure()[0]:
- comp = "Повышение ▲"
- return comp
- locate = (pressure()[1])
- locate = re.sub(r'Ленинск-Кузнецкий, ', '', locate)
- if len(str(minute)) < 2:
- minute = '0' + str(minute)
- with open("/home/pressure.txt", "w") as file:
- file.write(str(pressure()[0]))
- if options.temperature:
- print(temper(), "°C")
- if options.pressures:
- print(f"{pressure()[0]} мм.р.с., {locate}")
- if options.clouds:
- print(f"{cloudy()}")
- '''print (f"wind {wind()}")
- print(f"windspeed {windspeed()} m.c.")
- print(f"comparison {comparison()}")
- print("Магнитное поле", pole())'''
- #print(temper(), "°C")
- #print(f"{pressure()[0]} мм.р.с., {locate}")
- end = round(time.time() - start_time, 2)
- print("\n--- %s seconds ---" % (end))
Add Comment
Please, Sign In to add comment