Skip to content

Commit

Permalink
Commit all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmalina committed Mar 18, 2013
0 parents commit 249d7c5
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
@@ -0,0 +1,17 @@
# GudGuy

My IRC bot built with [irctk](https://github.com/maxcountryman/irctk) that sits in #tech@nyu on irc.freenode.net and searches the internets for the gud stuff.
Currently searches [Giphy](http://giphy.com), [SoundCloud](http://soundcloud.com), and [YouTube](http://youtube.com).

* How to add a bot command:

from gudguy import bot

@bot.command('mc') # shortcut
@bot.command
def my_command(context):
command = context.args # the command that was issued
return 'Hi.'

* Then in `gudguy/__init__.py` make sure the module your command was defined in is imported.
* All command must be prefixed with `'.'`.
4 changes: 4 additions & 0 deletions TODO.txt
@@ -0,0 +1,4 @@
Stuff to implement
------------------
/r/gifs search
gifbin search
11 changes: 11 additions & 0 deletions gudguy/__init__.py
@@ -0,0 +1,11 @@
from irctk import Bot

from gudguy.config import Config

# initialize the bot
bot = Bot()
bot.config.from_object(Config)

# load the plugins
from gudguy import giphy, sc, youtube

Binary file added gudguy/__init__.pyc
Binary file not shown.
11 changes: 11 additions & 0 deletions gudguy/config.py
@@ -0,0 +1,11 @@

class Config(object):
SERVER = 'irc.freenode.net'
PORT = 6667
SSL = False
TIMEOUT = 300
NICK = 'GudGuy'
REALNAME = 'The gud guy.'
CHANNELS = ['#tech@nyu']
ADMINS = []
SOUNDCLOUD_CLIENT_ID = '643405e1e13dd4cde125f3379ef36486'
Binary file added gudguy/config.pyc
Binary file not shown.
48 changes: 48 additions & 0 deletions gudguy/giphy.py
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-

"""
Consume the Giphy API -- http://giphy.com/
i.e. 'http://giphy.com/api/gifs?tag=cat'
"""
import random
import requests
import urllib

from gudguy import bot
from gudguy.utils import timestamp_utcnow

GIPHY_API = 'http://giphy.com/api'


def search_api_url(**kwargs):
endpoint = '/gifs?%s' % (urllib.urlencode(kwargs))
return GIPHY_API + endpoint


def search_api_request(query):
res = requests.get(search_api_url(tag=query))
return res.json() if res.status_code == 200 else None


def search_gifs(query):
content = search_api_request(query)
return content['data'] if content is not None else None


def get_gif(gifs, random_gif=False):
if random_gif:
return random.choice(gifs)
return gifs[0]


@bot.command('g')
@bot.command
def giphy(context):
query = context.args
opts = {'random_gif': 'random ' in query}
query = query.replace('random ', '') if opts['random_gif'] else query
query = query.replace(' ', '-')
gifs = search_gifs(query)
if not gifs:
return 'Welp.'
return get_gif(gifs, **opts)['thumbnail_url']
Binary file added gudguy/giphy.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions gudguy/sc.py
@@ -0,0 +1,16 @@
from gudguy import bot

import soundcloud

client = soundcloud.Client(client_id=bot.config['SOUNDCLOUD_CLIENT_ID'])


@bot.command('sc')
@bot.command
def soundcloud_track(context):
query = context.args
tracks = client.get('/tracks', q=query)
if not tracks:
return 'Welp.'
track = tracks[0]
return "%s %s" % (track.obj['title'], track.obj['permalink_url'])
Binary file added gudguy/sc.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions gudguy/utils.py
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-

"""
Some utility functions
"""
import time
import datetime


def timestamp_utcnow():
now = datetime.datetime.now()
return time.mktime(now.timetuple())
Binary file added gudguy/utils.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions gudguy/youtube.py
@@ -0,0 +1,24 @@
import requests

from gudguy import bot

base_url = 'http://gdata.youtube.com/feeds/api/'
search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1'
video_url = "http://youtube.com/watch?v={0}"


@bot.command('yt')
@bot.command
def youtube_search(context):
res = requests.get(search_api_url, params={'q': context.args})
if not res.status_code == 200:
return 'Error making request'

data = res.json()
if 'error' in data:
return 'Error performing search.'
if data['data']['totalItems'] == 0:
return 'No results.'

video = data['data']['items'][0]
return "%s %s" % (video['title'], video_url.format(video['id']))
Binary file added gudguy/youtube.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions requirements.txt
@@ -0,0 +1,7 @@
IrcTK==0.2.7
fudge==1.0.3
pyflakes==0.6.1
requests==1.1.0
simplejson==3.1.0
soundcloud==0.3.6
wsgiref==0.1.2
5 changes: 5 additions & 0 deletions run.py
@@ -0,0 +1,5 @@
from gudguy import bot


if __name__ == '__main__':
bot.run()
Binary file added run.pyc
Binary file not shown.

0 comments on commit 249d7c5

Please sign in to comment.