{"id":445,"date":"2014-09-16T19:29:04","date_gmt":"2014-09-17T02:29:04","guid":{"rendered":"http:\/\/danieltharp.com\/weblog\/?p=445"},"modified":"2014-09-16T19:29:53","modified_gmt":"2014-09-17T02:29:53","slug":"building-a-skybot-reddit-module","status":"publish","type":"post","link":"https:\/\/danieltharp.com\/weblog\/2014\/09\/building-a-skybot-reddit-module\/","title":{"rendered":"Building a Skybot reddit module."},"content":{"rendered":"<p>Perhaps I&#8217;ll update this with what&#8217;s been going in my life if there&#8217;s any interest. Anyway, I&#8217;ve been working with python and with a little help from \/r\/redditdev I designed a module to check regex (in this case, from IRC chatter) for a link to reddit and go get metadata about the link. This is for Skybot on Python 2.6 or 2.7.<\/p>\n<p><code>import json<br \/>\nimport urllib2<br \/>\nimport time<br \/>\nfrom util import hook<br \/>\nimport re<\/p>\n<p>reddit_re = (r'.*((www\\.)?reddit\\.com\/r[^ ]+)', re.I)<\/p>\n<p>@hook.regex(*reddit_re)<br \/>\ndef reddit_url(match):<\/p>\n<p>    jsonlink = match.group(0) + '.json' # retrieve JSON-ified version of link<br \/>\n    req = urllib2.Request(jsonlink)<br \/>\n    req.add_header('User-agent', 'Python\/Putyourbotnamehere-1.0') # Reddit wants us to use unique user-agents. OK.<br \/>\n    response = urllib2.urlopen(req)<\/p>\n<p>    data = json.load(response)<br \/>\n    submission = data[0]['data']['children'][0]['data'] # Dig down to the relevant bits.<br \/>\n    self = submission[\"is_self\"]<br \/>\n    if self == True:<br \/>\n        url = 'http:\/\/redd.it\/' + submission[\"id\"]<br \/>\n    else:<br \/>\n        url = submission[\"url\"]<br \/>\n    title = submission[\"title\"]<br \/>\n    score = submission[\"score\"]<br \/>\n    author = submission[\"author\"]<br \/>\n    timeago = time.strftime(\"%b %d %Y %H:%M:%S\", time.gmtime(submission[\"created_utc\"]))<br \/>\n    comments = submission[\"num_comments\"]<br \/>\n    return '%s - \\x02%s\\x02 - posted by \\x02%s\\x02 %s GMT - %s points and %s comments' % (<br \/>\n    url, title, author, timeago, score, comments)<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps I&#8217;ll update this with what&#8217;s been going in my life if there&#8217;s any interest. Anyway, I&#8217;ve been working with python and with a little help from \/r\/redditdev I designed a module to check regex (in this case, from IRC chatter) for a link to reddit and go get metadata about the link. This is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-nerd-stuff"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RwV4-7b","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":2,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts\/445\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}