Skip to content

Instantly share code, notes, and snippets.

@nilsding
Last active December 18, 2015 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilsding/5722778 to your computer and use it in GitHub Desktop.
Save nilsding/5722778 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# whatever.py
# Copyright © 2013 nilsding <nilsding@rrerr.net>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
# to run this you need:
# smstools
# pyinotify
# tweepy
# oh and run this as the user who has access to /var/spool/sms/incoming
import os
import tweepy
import pyinotify
import traceback
class config:
ck = "get"
cs = "your"
at = "666-own"
ats = "keys"
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE
auth = tweepy.OAuthHandler(config.ck, config.cs)
auth.set_access_token(config.at, config.ats)
api = tweepy.API(auth)
class PTmp(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
file_name = os.path.join(event.path, event.name)
try:
print "\033[33;1m>>>\033[0m " + file_name
with open(file_name) as f:
file_contents = [s.rstrip('\n') for s in f.readlines()]
try:
from_line = file_contents[0]
encoding = file_contents[9][10:]
print "Received message " + from_line + ", encoding " + encoding
except:
print "Received message"
j = 12
i = len(file_contents)
while j < i:
try:
if not ("follow " in file_contents[j][0:7] or "f " in file_contents[j][0:2] or "rt " in file_contents[j][0:3]):
if "UCS2" in encoding:
api.update_status(file_contents[j][0:140].decode("utf-16be"))
elif "ISO" in encoding:
api.update_status(file_contents[j][0:140].decode("latin-1"))
else:
print "\033[31;1mTried to follow someone, well fuck you too.\033[0m"
except:
print "except, sorry!"
traceback.print_exc()
if not ("follow " in file_contents[j][0:7] or "f " in file_contents[j][0:2] or "rt " in file_contents[j][0:3]):
api.update_status(file_contents[j][0:140].decode("latin-1"))
j = j + 1
except:
print "Errrerror"
traceback.print_exc()
notifier = pyinotify.Notifier(wm, PTmp())
wdd = wm.add_watch('/var/spool/sms/incoming', mask, rec = True)
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment