Created
September 27, 2017 12:27
-
-
Save anonymous/51fefb6a70af050745440f496b84d1f9 to your computer and use it in GitHub Desktop.
Autobarcode: Another cruddy Reddit bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Another dirty, dirty Reddit bot: autobarcode | |
*/ | |
include_once 'reddit.php'; | |
class RedditUser { | |
const USERNAME = 'autobarcode'; | |
const PASSWORD = '***'; | |
const CLIENTID = '***'; | |
const SECRET = '***'; | |
} | |
class Autobarcode { | |
const DB_HOST = 'localhost'; | |
const DB_NAME = '***'; | |
const DB_USER = '***'; | |
const DB_PASS = '***'; | |
const FORWARD_USER = '***'; | |
private static $subs_to_check = array( | |
'Bitcoin', | |
); | |
private static $ignored_pm_authors = array( | |
); | |
private static $dbc; | |
public static $subreddit; | |
public static function parse_new_images() { | |
self::_ensure_connection(); | |
foreach (self::$subs_to_check as $sub) { | |
self::$subreddit = $sub; | |
$threads = Reddit::fetch_threadlist($sub); | |
if (isset( | |
$threads['body'], | |
$threads['body']['data'], | |
$threads['body']['data']['children'] | |
)) { | |
foreach ($threads['body']['data']['children'] as $thread) { | |
if (isset( | |
$thread['data'], | |
$thread['data']['preview'], | |
$thread['data']['preview']['images'], | |
$thread['data']['preview']['images'][0], | |
$thread['data']['preview']['images'][0]['source'], | |
$thread['data']['preview']['images'][0]['source']['url'] | |
)) { | |
$id = Reddit::_from_id($thread['data']['id']); | |
$url = $thread['data']['preview']['images'][0]['source']['url']; | |
if (!self::_is_thread_seen($id)) { | |
$barcodes = self::parse_image($id, $url); | |
self::_add_thread($id, $barcodes); | |
if (count($barcodes)) { | |
self::post($id, $barcodes); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
private static function parse_image($id, $url) { | |
$thread = '/r/'.self::$subreddit.'/comments/'.Reddit::_to_id($id); | |
$img_parts = parse_url($url); | |
$img_name = basename($img_parts['path']); | |
$img_file = '/tmp/autobarcode/'.$img_name; | |
file_put_contents($img_file, file_get_contents($url)); | |
$output = shell_exec("zbarimg {$img_file} 2>&1"); | |
$output = explode("\n", $output); | |
$barcodes = array(); | |
if (strpos($output[0], 'scanned 0 barcode symbols') === 0) { | |
// No barcodes in this image | |
self::_log("{$thread}: No barcodes"); | |
} else { | |
foreach ($output as $line) { | |
if (preg_match('#^scanned \d+ barcode symbols#', $line)) { | |
break; | |
} else { | |
$new_barcode = array( | |
'type' => substr($line, 0, strpos($line, ':')), | |
'value' => substr($line, strpos($line, ':') + 1) | |
); | |
if ($new_barcode['type'] !== 'ERROR') { | |
$barcodes[] = $new_barcode; | |
} | |
} | |
} | |
self::_log("{$thread}: ".json_encode($barcodes)); | |
} | |
unlink($img_file); | |
return $barcodes; | |
} | |
private static function post($id, $barcodes) { | |
$text[] = "Barcodes found in this image post:"; | |
$text[] = ''; | |
foreach ($barcodes as $barcode) { | |
$text[] = "- `{$barcode['value']}` ({$barcode['type']})"; | |
} | |
$text[] = ''; | |
$text[] = '----------------'; | |
$text[] = "[^\[Contact\]](https://reddit.com/message/compose?to=OrangeredStilton&subject=Hey,+your+barcode+bot+sucks)";// [^\[Source ^code\]](https://gist.github.com/Two9A/1d976f9b7441694162c8)"; | |
self::_log('Writing comment for thread '.Reddit::_to_id($id)); | |
Reddit::comment($id, join("\n", $text)); | |
} | |
public static function forward_inbox($to = null) { | |
if (!$to) { | |
$to = self::FORWARD_USER; | |
} | |
$inbox = Reddit::inbox(); | |
if (isset( | |
$inbox['body'], | |
$inbox['body']['data'], | |
$inbox['body']['data']['children'] | |
)) { | |
foreach ($inbox['body']['data']['children'] as $msg) { | |
$data = $msg['data']; | |
Reddit::mark_read($data['name']); | |
if (!in_array($data['author'], self::$ignored_pm_authors)) { | |
self::_log('Forwarding inbox message from '.$data['author']); | |
Reddit::send_message( | |
$to, | |
'Forwarded from '.__CLASS__, | |
join(" \n", array( | |
'From: '.$data['author'], | |
'Date: '.date('Y-m-d H:i:s', $data['created_utc']), | |
'Subject: '.$data['subject'], | |
'Context: '.$data['context'], | |
'', | |
$data['body'] | |
)) | |
); | |
} | |
} | |
} | |
} | |
private static function _is_thread_seen($thread_id) { | |
$st = self::$dbc->prepare('SELECT COUNT(*) AS cnt FROM threads WHERE thread_id=:thread AND subreddit_name=:subreddit'); | |
$st->bindValue(':thread', $thread_id); | |
$st->bindValue(':subreddit', self::$subreddit); | |
$st->execute(); | |
$row = $st->fetch(); | |
return ($row['cnt'] > 0); | |
} | |
private static function _add_thread($thread_id, $barcodes) { | |
$st = self::$dbc->prepare('INSERT INTO threads(thread_id, subreddit_name, barcode_data) VALUES(:thread, :subreddit, :barcodes)'); | |
$st->bindValue(':thread', $thread_id); | |
$st->bindValue(':subreddit', self::$subreddit); | |
$st->bindValue(':barcodes', json_encode($barcodes)); | |
$st->execute(); | |
} | |
private static function _ensure_connection() { | |
if (!self::$dbc) { | |
self::$dbc = new PDO( | |
sprintf('mysql:host=%s;dbname=%s', self::DB_HOST, self::DB_NAME), | |
self::DB_USER, | |
self::DB_PASS | |
); | |
self::$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
self::$dbc->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
} | |
} | |
private static function _log($str) { | |
$fp = fopen('/tmp/autobarcode.log', 'a'); | |
fprintf($fp, "[%s] [%s] %s\n", date('YmdHis'), self::$subreddit, $str); | |
fclose($fp); | |
} | |
} | |
Autobarcode::forward_inbox(); | |
Autobarcode::parse_new_images(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment