Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Improved tests and fix one bug
Browse files Browse the repository at this point in the history
  • Loading branch information
anze3db committed Sep 13, 2014
1 parent 77e1856 commit 4537222
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
64 changes: 35 additions & 29 deletions src/plugins/read_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
+ "It has been viewed %(views)s times. ",
"Title: '%(title)s', Views: %(views)s, duration: %(seconds)ss.",
"Title of that %(service)s video is '%(title)s'.",
"%(service) video is titled '%(title)s' and has %(rating)s.",
"%(service)s video is titled '%(title)s' and has %(rating)s.",
"Here is the title of that %(service)s video: '%(title)s'.",
"I found the title of that %(service)s video, here it is: '%(title)s'",
"If you click that link you will watch a video titled '%(title)s'. "
Expand Down Expand Up @@ -62,53 +62,59 @@ def _read_twitter(self, channel, msg):
self.bot.say('Sorry, I wasn\'t able to read the last tweet :(',
channel)

def _get_vimeo_info(self, id):
r = requests.get("https://vimeo.com/api/v2/video/" + id + ".json")
video = json.loads(r.text)[0]
if "stats_number_of_likes" in video:
likes = ("%d likes." % video["stats_number_of_likes"])
else:
likes = "an unknown number of likes"
return {
'service': "vimeo",
'title': video["title"].encode('utf8'),
'seconds': str(video["duration"]),
'views': str(video["stats_number_of_plays"]),
'rating': likes
}

def _read_vimeo(self, channel, msg):
res = vimeo_regex.search(msg)
if not res:
return
try:
video_id = str(res.groups()[0])
r = requests.get("https://vimeo.com/api/v2/video/"+video_id+".json")
video = json.loads(r.text)[0]
if "stats_number_of_likes" in video:
likes = ("%d likes." % video["stats_number_of_likes"])
else:
likes = "an unknown number of likes"
video_info = {
'service': "vimeo",
'title': video["title"].encode('utf8'),
'seconds': str(video["duration"]),
'views': str(video["stats_number_of_plays"]),
'rating': likes
}
video_info = self._get_vimeo_info(video_id)
self.bot.say(random_response(VIDEO_RESPONSES) % video_info, channel)
except Exception as e:
self.bot.log_error('ERROR could not get title of vimeo link from: "'
+ msg + '" the exception was: ' + str(e))
self.bot.say('For some reason I couldn\'t read the title of that '
+ 'vimeo link.', channel)

def _get_youtube_info(self, video_id):
from gdata.youtube import service
client = service.YouTubeService()
video = client.GetYouTubeVideoEntry(video_id=video_id)
if video.rating is not None:
average_rating = float(video.rating.average)
rating = ("an average rating of %.2f" % average_rating)
else:
rating = "no rating"
return {
'service': "youtube",
'title': video.title.text,
'seconds': video.media.duration.seconds,
'views': video.statistics.view_count,
'rating': rating
}

def _read_youtube(self, channel, msg):
res = yt_regex.search(msg)
if not res:
return
try:
video_id = str(res.groups()[0])
from gdata.youtube import service
client = service.YouTubeService()
video = client.GetYouTubeVideoEntry(video_id=video_id)
if video.rating is not None:
average_rating = float(video.rating.average)
rating = ("an average rating of %.2f" % average_rating)
else:
rating = "no rating"
video_info = {
'service': "youtube",
'title': video.title.text,
'seconds': video.media.duration.seconds,
'views': video.statistics.view_count,
'rating': rating
}
video_info = self._get_youtube_info(video_id)
self.bot.say(random_response(VIDEO_RESPONSES) % video_info,
channel)
except Exception as e:
Expand Down
32 changes: 22 additions & 10 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from plugins.nsfw_image_detector import NSFWImageDetectorPlugin
from plugins.read_links import ReadLinks
from plugins.read_links import VIDEO_RESPONSES, WEB_RESPONSES


BASE_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -89,6 +90,9 @@ def setUp(self):
self.line_start = ":smotko!~smotko@193.188.1.1 PRIVMSG #psywerx "
self.plugin = ReadLinks(bot=self.bot)
self.say = self.plugin.bot.say
self.video_response = {'seconds': '34227', 'rating': 'no rating',
'views': '385293', 'service': 'youtube',
'title': 'ASP 2014 J-Bay Open English Day 10'}

def handle_message(self, line):
self.plugin.handle_message('channel', 'nick',
Expand Down Expand Up @@ -116,23 +120,31 @@ def test_unicode(self, name_text):
tweet = 'https://twitter.com/Smotko/status/501844653583659010'
self._test_helper(tweet, 'Smotko')

# TODO: mock out the actual request
# TODO: run similar tests in a loop
def test_youtube(self):
msg = 'Look https://www.youtube.com/watch?v=2PUefiJBJQQ'
response = 'Jack Gleeson'
self._test_helper(msg, response)

def test_youtube_no_average(self):
@patch("plugins.read_links.ReadLinks._get_youtube_info")
def test_youtube_no_average(self, yt_info):
yt_info.return_value = self.video_response
msg = 'https://www.youtube.com/watch?v=jc8zZ9PbYVM'
response = 'ASP 2014'
self._test_helper(msg, response)

def test_vimeo(self):
@patch("plugins.read_links.ReadLinks._get_vimeo_info")
def test_vimeo(self, vimeo_info):
vimeo_info.return_value = self.video_response
msg = ':O http://vimeo.com/102825514'
response = 'Muscles'
response = 'ASP 2014'
self._test_helper(msg, response)

def test_all_video_responses(self):
for vr in VIDEO_RESPONSES:
self.bot.say(vr % self.video_response, "channel")
assert 'ASP 2014' in self.say.call_args[0][0].split('\n')[0]

def test_all_web_responses(self):
for wr in WEB_RESPONSES:
self.bot.say(wr % {'title': 'test___'}, "channel")
assert 'test___' in self.say.call_args[0][0].split('\n')[0]

# TODO: mock out the actual request
def test_web(self):
msg = 'This is a silly website http://smotko.si/'
response = 'Smotko\'s Blog'
Expand Down

0 comments on commit 4537222

Please sign in to comment.