def announce_reddit(context): submission_id = context.line['regex_search'].group(1) url = 'http://www.reddit.com/comments/{}.json'.format(submission_id) submission = utils.make_request_json(url) if isinstance(submission, list): try: submission = submission[0]['data']['children'][0]['data'] except Exception: return 'Could not fetch json' else: return submission info = { 'title': submission['title'], 'up': submission['ups'], 'down': submission['downs'], 'shortlink': 'http://redd.it/' + submission['id'] } line = u'\'{title}\' - +{up}/-{down} - {shortlink}'.format(**info) if context.line['regex_search'].group(3): # Link to comment return '[Comment] ' + line else: # Link to submission return line
def reddit(context): subreddit = context.args.strip().split(' ')[0] params = {} if subreddit is '': return 'Usage: .reddit <subreddit>' elif subreddit.lower().endswith(('/new', '/new/')): # reddit occasionally returns f**k all if the query string is not added params = {'sort': 'new'} url = 'http://www.reddit.com/r/{}.json'.format(subreddit) submission = utils.make_request_json(url, params) if isinstance(submission, dict): try: submission = submission['data']['children'][0]['data'] except: return 'Could not fetch json, does that subreddit exist?' else: return submission info = { 'username': context.line['user'], 'subreddit': submission['subreddit'], 'title': submission['title'], 'up': submission['ups'], 'down': submission['downs'], 'shortlink': 'http://redd.it/' + submission['id'] } line = u'{username}: /r/{subreddit} \'{title}\' +{up}/-{down} {shortlink}'.format(**info) return line
def announce_tweet(context): ''' Announces tweets as they are posted in the channel ''' tweet_id = context.line['regex_search'].group(2) url = status_url.format(tweet_id) response = utils.make_request_json(url) if not isinstance(response, dict): return response info = {'screen_name': response['user']['screen_name'], 'tweet': response['text']} return utils.unescape_html(line.format(**info))
def twitter(context): ''' Gets the latest tweet for a username ''' username = context.args.strip().split(' ')[0] if username is '': return 'Usage: .twitter <username>' params = {'screen_name': username, 'count': 1} response = utils.make_request_json(latest_url, params) if not isinstance(response, list): return response elif response == []: return 'No results found for that name' info = {'screen_name': response[0]['user']['screen_name'], 'tweet': response[0]['text']} return utils.unescape_html(line.format(**info))
def karma(context): redditor = context.args.strip().split(' ')[0] if redditor is '': return 'Usage: .karma <redditor>' url = 'http://www.reddit.com/user/{}/about.json'.format(redditor) redditor = utils.make_request_json(url) if isinstance(redditor, dict): try: redditor = redditor['data'] except: return 'Could not fetch json, does that user exist?' else: return redditor info = { 'redditor': redditor['name'], 'link': redditor['link_karma'], 'comment': redditor['comment_karma'], } line = u'{redditor} has {link} link and {comment} comment karma'.format(**info) return line