Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Test cases added for config reader #59

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A simple python script to fetch cricket scores and send notifications.
* beautifulSoup4
* requests
* python2 only
* mock

## Installation ##
``sudo python setup.py install``
Expand Down
25 changes: 7 additions & 18 deletions scorer/app.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import logging
from time import sleep

import scorer.logger as logger
import scorer.fetch_scores as fs
import scorer.notification as notify
from scorer.system import exitApp
from scorer.ui import getUserInput
from scorer import config_reader

logger = logging.getLogger("scorer.app")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("scorer.log")
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -\
%(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger = logger.get_logger('cricket-scores-api')

NO_LIVE_MATCHES = "No Match in progress"
SLEEP_INTERVAL = 60
no_live_matches = config_reader.NO_LIVE_MATCHES
sleep_interval = config_reader.SLEEP_INTERVAL


def main():
while True:
logger.debug("Getting the xml and matches list")
xml, matches = fs.findMatchesAvailable()
if matches[0] == NO_LIVE_MATCHES:
if matches[0] == no_live_matches:
print "No Live matches are available now:"
exitApp()
matches.append("Quit the scorer app")
Expand All @@ -50,7 +39,7 @@ def main():
logger.debug("Sending notification for: title:{} score:\
{}".format(title, score))
notify.popUpMessage(title, score)
sleep(SLEEP_INTERVAL)
sleep(sleep_interval)
except KeyboardInterrupt:
break

Expand Down
5 changes: 5 additions & 0 deletions scorer/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"NO_LIVE_MATCHES": "No Match in progress",
"SLEEP_INTERVAL": 60,
"WON_STATUS": "won by"
}
21 changes: 21 additions & 0 deletions scorer/config_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json
import scorer.logger as logger
from scorer.system import exitApp

logger = logger.get_logger("config-reader")


def read_json(file_path):
try:
with open(file_path.lower()) as json_data:
data = json.load(json_data)
return data
except FileNotFoundError as e:
logger.error("config file not found!!")
exitApp()


config_data = read_json("../scorer/config.json")
NO_LIVE_MATCHES = config_data["NO_LIVE_MATCHES"]
SLEEP_INTERVAL = config_data["SLEEP_INTERVAL"]
WON_STATUS = config_data["WON_STATUS"]
40 changes: 28 additions & 12 deletions scorer/fetch_scores.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging
import scorer.logger as logger
import re

import requests
from bs4 import BeautifulSoup
from scorer.system import exitApp
from scorer import config_reader

logger = logging.getLogger('scorer.fetch_scores')
logger = logger.get_logger('scorer.fetch_scores')

WON_STATUS = "won by"
won_status = config_reader.WON_STATUS


def getJsonURL(matchId):
Expand All @@ -30,30 +31,40 @@ def getPlayingTeamNames(jsonurl):
:return: teams playing
"""
logger.info("Url to get the json from {}".format(jsonurl))
r = requests.get(jsonurl)
try:
r = requests.get(jsonurl)
except:
logger.error("not able to reach the site to get the match info!!")
exitApp()

jsonData = r.json()
playingTeams = {team.get("team_id"): team.get("team_name\
") for team in jsonData.get("team")}
logging.debug("playingTeams: {}".format(playingTeams))
logger.debug("playingTeams: {}".format(playingTeams))
return playingTeams


def getLastestScore(jsonurl, playingTeams):
logger.info("Entry Point for getLastestScore")
logger.debug("Url to get the latest json is: {}" . format(jsonurl))
r = requests.get(jsonurl)
logger.debug("Url to get the latest json is: {}".format(jsonurl))
try:
r = requests.get(jsonurl)
except:
logger.error("not able to reach the site to get the match info!!")
exitApp()

jsonData = r.json()
matchStatus = jsonData.get("live").get("status")
logger.info("matchStatus: {}".format(matchStatus))
titleToDisplay = matchStatus
scoreToDisplay = ""
# Check if match Started
if(not jsonData.get("live").get("innings")):
if (not jsonData.get("live").get("innings")):
logger.info("Match not started")
return (titleToDisplay, scoreToDisplay)

# Check if match over
if(WON_STATUS in matchStatus):
if (won_status in matchStatus):
logger.info("Match over")
return (titleToDisplay, scoreToDisplay)
innings = jsonData.get("live").get("innings")
Expand Down Expand Up @@ -105,9 +116,14 @@ def findMatchesAvailable(url="http://static.cricinfo.com/rss/livescores.xml"):
:return: a tuple of xml and matches.
"""
logger.info("Entry point for findMatchesAvailable")
r = requests.get(url)
try:
r = requests.get(url)
except:
logger.error("not able to reach the site to get the match info!!")
exitApp()

soup = BeautifulSoup(r.text)
xml = soup.find_all("item")
matches = map(lambda item: re.sub(r'\s+', " ", re.sub('\
[^A-Za-z ]+', '', item.title.text)), xml)
[^A-Za-z ]+', '', item.title.text)), xml)
return (xml, matches)
15 changes: 15 additions & 0 deletions scorer/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys
import logging

def get_logger(name, level=logging.INFO):
logger = logging.getLogger(name)
logger.setLevel(level)
if logger.handlers:
pass
else:
ch = logging.StreamHandler(sys.stderr)
ch.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
18 changes: 7 additions & 11 deletions scorer/notification.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import logging

import pynotify

import scorer.logger as logger
from sys import version_info
from scorer.system import exitApp
import subprocess as sp

logger = logging.getLogger('scorer.notification')
logger = logger.get_logger('scorer.notification')


def popUpMessage(title, message):
logger.info("Initializing pynotify")
try:
pynotify.init("Scorer")
pynotify.Notification(title, message, "dialog-information").show()
command = ['notify-send', title, message]
pipe = sp.call(command)
logger.info("pop up message sent!!")
except Exception as e:
logger.error("Error initializing pynotify")
logger.debug(e)
logger.info("Unable to initialize pynotify: Connection Refused")
logger.info("Quitting the app")
exitApp()
10 changes: 5 additions & 5 deletions scorer/ui.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from curses import wrapper
import curses
import logging
import scorer.logger as logger

logger = logging.getLogger('scorer.ui')
logger = logger.get_logger('scorer.ui')


def printGames(stdscr, matches, selected):
Expand All @@ -26,15 +26,15 @@ def main(stdscr, matches):
printGames(stdscr, matches, selected)
event = stdscr.getch()
if event == ord("\n"):
logging.info("Enter key pressed")
logger.info("Enter key pressed")
return selected
elif event == curses.KEY_UP:
logging.info("Up key pressed")
logger.info("Up key pressed")
if selected != 0:
selected -= 1
printGames(stdscr, matches, selected)
elif event == curses.KEY_DOWN:
logging.info("Down key pressed")
logger.info("Down key pressed")
if selected != len(matches) - 1:
selected += 1
printGames(stdscr, matches, selected)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def readme():
install_requires=[
'requests',
'beautifulsoup4',
'pynotify'
'mock'
],
entry_points={
'console_scripts': [
Expand Down
25 changes: 25 additions & 0 deletions test/test_config_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# export the PYTHONPATH till the project folder as I added another folder test for writing the test cases and Python
# wouldn't be able to recognize the other packages.
# export PYTHONPATH=$PYTHONPATH:/Users/hmandadi/Documents/harsh/scorer.py/
# Run test case from scorer/ folder
# python -m unittest discover ../test/

import unittest
from mock import MagicMock
import scorer.config_reader as config_reader


class ConfigReaderTestCases(unittest.TestCase):

def test_wrong_json_path(self):
config_reader.read_json = MagicMock(return_value=str('No such file or directory'))
self.assertEqual(config_reader.read_json('/path/not/found/'), 'No such file or directory')

def test_json_file_unable_to_open(self):
config_reader.read_json = MagicMock(return_value=str('No JSON object could be decoded'))
self.assertEqual(config_reader.read_json('/filenotabletoberead/sample-template.abc'),
'No JSON object could be decoded')


if __name__ == '__main__':
unittest.main()