-
Notifications
You must be signed in to change notification settings - Fork 986
Added new feature of Cricket #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shubhsherl
wants to merge
8
commits into
swapagarwal:master
Choose a base branch
from
shubhsherl:new-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
48fbb90
added cricket feature
shubhsherl 93e8c52
updated requirement.txt
shubhsherl daa4c5d
updated wit.json
shubhsherl 155f8b8
updated wit.json
shubhsherl d244c42
updated cricket.py
shubhsherl c022402
updated cricket.py
shubhsherl dce5a61
removed unnecessary files
shubhsherl c111016
updated cricket.py
shubhsherl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import json | ||
import os | ||
import sys | ||
|
||
import keen | ||
import requests | ||
|
||
import config | ||
import modules | ||
from src import * | ||
from templates.quick_replies import add_quick_reply | ||
from templates.text import TextTemplate | ||
|
||
WIT_AI_ACCESS_TOKEN = os.environ.get('WIT_AI_ACCESS_TOKEN', config.WIT_AI_ACCESS_TOKEN) | ||
|
||
|
||
def generate_postback(module): | ||
return { | ||
'intent': module, | ||
'entities': None | ||
} | ||
|
||
|
||
def process_query(input): | ||
# For local testing, mock the response from Wit | ||
with open(config.WIT_LOCAL_DATA) as wit_file: | ||
wit_local_data = json.load(wit_file) | ||
if input.lower() in wit_local_data: | ||
return wit_local_data[input.lower()]['intent'], wit_local_data[input.lower()]['entities'] | ||
try: | ||
r = requests.get('https://api.wit.ai/message?v=20160420&q=' + input, headers={ | ||
'Authorization': 'Bearer %s' % WIT_AI_ACCESS_TOKEN | ||
}) | ||
data = r.json() | ||
intent = data['outcomes'][0]['intent'] | ||
entities = data['outcomes'][0]['entities'] | ||
confidence = data['outcomes'][0]['confidence'] | ||
if intent in src.__all__ and confidence > 0.5: | ||
return intent, entities | ||
else: | ||
return None, {} | ||
except: | ||
return None, {} | ||
|
||
|
||
def search(input, sender=None, postback=False): | ||
if postback: | ||
payload = json.loads(input) | ||
intent = payload['intent'] | ||
entities = payload['entities'] | ||
else: | ||
intent, entities = process_query(input) | ||
# TODO: Needs to be refactored out | ||
try: | ||
keen.project_id = os.environ.get('KEEN_PROJECT_ID', config.KEEN_PROJECT_ID) | ||
keen.write_key = os.environ.get('KEEN_WRITE_KEY', config.KEEN_WRITE_KEY) | ||
keen.add_event('logs', { | ||
'intent': intent, | ||
'entities': entities, | ||
'input': input, | ||
'sender': sender, | ||
'postback': postback | ||
}) | ||
except: | ||
pass # Could not stream data for analytics | ||
if intent is not None: | ||
if intent in src.__personalized__ and sender is not None: | ||
r = requests.get('https://graph.facebook.com/v2.6/' + str(sender), params={ | ||
'fields': 'first_name', | ||
'access_token': os.environ.get('ACCESS_TOKEN', config.ACCESS_TOKEN) | ||
}) | ||
if entities is None: | ||
entities = {} | ||
entities['sender'] = r.json() | ||
data = sys.modules['modules.src.' + intent].process(input, entities) | ||
if data['success']: | ||
return data['output'] | ||
else: | ||
if 'error_msg' in data: | ||
return data['error_msg'] | ||
else: | ||
return TextTemplate('Something didn\'t work as expected! I\'ll report this to my master.').get_message() | ||
else: | ||
message = TextTemplate( | ||
'I\'m sorry; I\'m not sure I understand what you\'re trying to say.\nTry typing "help" or "request"').get_message() | ||
message = add_quick_reply(message, 'Help', modules.generate_postback('help')) | ||
message = add_quick_reply(message, 'Request', modules.generate_postback('request')) | ||
return message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,9 @@ | |
"test": { | ||
"intent": "xkcd", | ||
"entities": [] | ||
}, | ||
"cricket": { | ||
"intent": "cricket", | ||
"entities": [] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
'book', | ||
'bye', | ||
'coin', | ||
'cricket', | ||
'currency', | ||
'dice', | ||
'dictionary', | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pycricbuzz import Cricbuzz | ||
import requests | ||
import requests_cache | ||
from templates.button import * | ||
|
||
|
||
def process(input, entities=None): | ||
output = {} | ||
try: | ||
cricket = Cricbuzz() | ||
matches = cricket.matches() | ||
text_data="" | ||
template = TextTemplate() | ||
for match in matches: | ||
cric_data=cricket.livescore(match['id']) | ||
text_data+=cric_data['matchinfo']['mnum']+"\n"+cric_data['matchinfo']['mchdesc']+"\n" | ||
text_data+=cric_data['batting']['team']+"\t"+cric_data['batting']['score'][0]['runs']+"/"+cric_data['batting']['score'][0]['wickets']+"\n" | ||
text_data+=cric_data['bowling']['team']+"\t"+cric_data['bowling']['score'][0]['runs']+"/"+cric_data['bowling']['score'][0]['wickets']+"\n" | ||
text_data+=cric_data['matchinfo']['status']+"\n"+"*"*10+"\n" | ||
template.set_text(text_data) | ||
text = template.get_text() | ||
template = ButtonTemplate(text) | ||
template.add_web_url('CricBuzz Live', 'http://www.cricbuzz.com/cricket-match/live-scores') | ||
|
||
output['input'] = input | ||
output['output'] = template.get_message() | ||
output['success'] = True | ||
except: | ||
error_message = 'There was some error while retrieving data from Cricbuzz' | ||
output['error_msg'] = TextTemplate(error_message).get_message() | ||
output['success'] = False | ||
return output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import modules | ||
|
||
def test_cricket(): | ||
assert ('cricket' == modules.process_query('cricket')[0]) | ||
assert ('cricket' == modules.process_query('live cricket')[0]) | ||
assert ('cricket' == modules.process_query('cricket livescores')[0]) | ||
assert ('cricket' != modules.process_query('something random')[0]) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this file needed?