Created a script for animating loading when fetching location and weather information.

This commit is contained in:
2017-07-30 13:54:26 +02:00
parent b00f0c1637
commit a9cbc124e4
2 changed files with 62 additions and 10 deletions

40
loadingAnimation.py Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-07-30 13:53:38
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-07-30 13:53:46
import itertools
from threading import Thread
from time import sleep
from sys import stdout
class LoadingAnimation(object):
def __init__(self):
self.done = False
def start(self):
t = Thread(target=self.animate)
t.start()
def animate(self):
for c in itertools.cycle(['|', '/', '-', '\\']):
if self.done:
break
stdout.write('\rFetching ' + c)
stdout.flush()
sleep(0.1)
def stop(self):
self.done = True
def main():
loadingAnimation = LoadingAnimation()
loadingAnimation.start()
sleep(2)
loadingAnimation.stop()
stdout.write('\rTemp \n')
if __name__ == '__main__':
main()

View File

@@ -3,7 +3,7 @@
# @Author: KevinMidboe # @Author: KevinMidboe
# @Date: 2017-07-27 21:26:53 # @Date: 2017-07-27 21:26:53
# @Last Modified by: KevinMidboe # @Last Modified by: KevinMidboe
# @Last Modified time: 2017-07-30 10:34:41 # @Last Modified time: 2017-07-30 13:16:18
# TODO LIST # TODO LIST
# Get coordinates from IP ✔ # Get coordinates from IP ✔
@@ -13,16 +13,24 @@
# Match weather description to icons ✔ # Match weather description to icons ✔
# Check internet connection in a strict way # Check internet connection in a strict way
# Add table for time periode # Add table for time periode
# Add cache for quicker location for same ip
import fire, json, geoip2.database, ssl import fire, json, geoip2.database, ssl, os
from yr.libyr import Yr from yr.libyr import Yr
from requests import get from requests import get
from pprint import pprint from pprint import pprint
from sys import stdout
from emojiParser import EmojiParser from emojiParser import EmojiParser
from waiting_animation import LoadingAnimation
class Location(object): class Location(object):
def __init__(self): def __init__(self):
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
self.reader = geoip2.database.Reader('conf/GeoLite2-City.mmdb') self.reader = geoip2.database.Reader('conf/GeoLite2-City.mmdb')
self.getIP() self.getIP()
@@ -61,7 +69,7 @@ class Location(object):
return area return area
class WeatherForcast(object): class WeatherForecast(object):
def __init__(self, area=None): def __init__(self, area=None):
# TODO search for area coordinates in a map # TODO search for area coordinates in a map
self.area = area self.area = area
@@ -84,7 +92,7 @@ class WeatherForcast(object):
# Create seperate function for formatting # Create seperate function for formatting
locationName = '/'.join([self.area['country'], self.area['municipality'], self.area['town'], self.area['street']]) locationName = '/'.join([self.area['country'], self.area['municipality'], self.area['town'], self.area['street']])
# Use the defined location name with yr API for location based weather information # Use the defined location name with yr API for location based weather information
weather = Yr(location_name=locationName) weather = Yr(location_name=locationName)
now = json.loads(weather.now(as_json=True)) now = json.loads(weather.now(as_json=True))
@@ -95,18 +103,22 @@ class WeatherForcast(object):
emojiParser = EmojiParser(now['symbol']['@name']) emojiParser = EmojiParser(now['symbol']['@name'])
weatherIcon_output = emojiParser.convertSematicsToEmoji() weatherIcon_output = emojiParser.convertSematicsToEmoji()
print('%s %s' % (temperature_output, weatherIcon_output)) return ('%s %s' % (temperature_output, weatherIcon_output))
class TermWeather(object): class TermWeather(object):
# Add now, forcast as args # Add now, forecast as args
def auto(self): def auto(self):
weatherForcast = WeatherForcast() loadingAnimation = LoadingAnimation()
weatherForcast.now() loadingAnimation.start()
weatherForecast = WeatherForecast()
forecast = weatherForecast.now()
loadingAnimation.stop()
stdout.write('\r%s \n' % forecast)
def fetch(self, area=None): def fetch(self, area=None):
weatherForcast = WeatherForcast(area) weatherForecast = WeatherForcast(area)
weatherForcast.now() weatherForecast.now()
if __name__ == '__main__': if __name__ == '__main__':