Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Commit all the things

  • Loading branch information...
commit 249d7c583d04cf58e47bf020371adec99b478506 0 parents
@jjmalina authored
17 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 TODO.txt
@@ -0,0 +1,4 @@
+Stuff to implement
+------------------
+/r/gifs search
+gifbin search
11 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
+
BIN  gudguy/__init__.pyc
Binary file not shown
11 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'
BIN  gudguy/config.pyc
Binary file not shown
48 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']
BIN  gudguy/giphy.pyc
Binary file not shown
16 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'])
BIN  gudguy/sc.pyc
Binary file not shown
12 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())
BIN  gudguy/utils.pyc
Binary file not shown
24 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']))
BIN  gudguy/youtube.pyc
Binary file not shown
7 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 run.py
@@ -0,0 +1,5 @@
+from gudguy import bot
+
+
+if __name__ == '__main__':
+ bot.run()
BIN  run.pyc
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.