Skip to content

Commit

Permalink
Add Python 3 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Nov 23, 2015
1 parent 8260c7d commit 11a67df
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 0 deletions.
39 changes: 39 additions & 0 deletions python3/fucking_coffee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime
import telnetlib
import time

from hackerutils import sh

COFFEE_MACHINE_ADDR = '10.10.42.42'
COFFEE_MACHINE_PASS = '1234'
COFFEE_MACHINE_PROM = 'Password: '


def main():
# Skip on weekends.
if datetime.date.today().weekday() in (0, 6,):
return

# Exit early if no sessions with my_username are found.
if not any(s.startswith(b'my_username ') for s in sh('who').split(b'\n')):
return

time.sleep(17)

conn = telnetlib.Telnet(host=COFFEE_MACHINE_ADDR)
conn.open()
conn.expect([COFFEE_MACHINE_PROM])
conn.write(COFFEE_MACHINE_PASS)

conn.write('sys brew')
time.sleep(64)

conn.write('sys pour')
conn.close()


if __name__ == '__main__':
main()
23 changes: 23 additions & 0 deletions python3/hackerutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pathlib
import subprocess

from dotenv import Dotenv


def get_dotenv(filename='.env'):
return Dotenv(str(pathlib.Path(__file__).parent / filename))


def sh(*args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
return stdout


def get_log_path(name):
path = pathlib.Path(__file__).parent / 'logs' / name
path.parent.mkdir(parents=True, exist_ok=True)
return path
57 changes: 57 additions & 0 deletions python3/hangover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime
import random

from twilio import TwilioRestException
from twilio.rest import TwilioRestClient

from hackerutils import get_dotenv, get_log_path, sh

dotenv = get_dotenv()

TWILIO_ACCOUNT_SID = dotenv['TWILIO_ACCOUNT_SID']
TWILIO_AUTH_TOKEN = dotenv['TWILIO_AUTH_TOKEN']

LOG_FILE_PATH = get_log_path('hangover.txt')


def main():
# Skip on weekends.
if datetime.date.today().weekday() in (0, 6,):
return

# Exit early if any session with my_username is found.
if any(s.startswith(b'my_username ') for s in sh('who').split(b'\n')):
return

client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

# Phone numbers.
my_number = '+xxx'
number_of_boss = '+xxx'

excuses = [
'Locked out',
'Pipes broke',
'Food poisoning',
'Not feeling well',
]

try:
# Send a text message.
client.messages.create(
to=number_of_boss,
from_=my_number,
body='Gonna work from home. ' + random.choice(excuses),
)
except TwilioRestException as e:
# Log errors.
with LOG_FILE_PATH.open('a') as f:
f.write('Failed to send SMS: {}'.format(e))
raise


if __name__ == '__main__':
main()
44 changes: 44 additions & 0 deletions python3/kumar_asshole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

import gmail
import yagmail

from hackerutils import get_dotenv

dotenv = get_dotenv()

GMAIL_USERNAME = dotenv['GMAIL_USERNAME']
GMAIL_PASSWORD = dotenv['GMAIL_PASSWORD']

KUMAR_EMAIL = '[email protected]'
KEYWORDS_REGEX = re.compile(r'sorry|help|wrong', re.IGNORECASE)

REPLY_BODY = "No problem. I've fixed it. \n\n Please be careful next time."


yagmail.register(GMAIL_USERNAME, GMAIL_PASSWORD)


def send_reply(subject):
yag = yagmail.SMTP(GMAIL_USERNAME)
yag.send(
to=KUMAR_EMAIL,
subject='RE: {}'.format(subject),
contents=REPLY_BODY,
)


def main():
g = gmail.login(GMAIL_USERNAME, GMAIL_PASSWORD)
for mail in g.inbox().mail(unread=True, sender=KUMAR_EMAIL, prefetch=True):
if KEYWORDS_REGEX.search(mail.body):
# Restore DB and send a reply.
mail.add_label('Database fixes')
send_reply(mail.subject)


if __name__ == '__main__':
main()
4 changes: 4 additions & 0 deletions python3/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dotenv
twilio
yagmail
git+https://github.com/charlierguo/gmail
56 changes: 56 additions & 0 deletions python3/smack_my_bitch_up.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime
import random

from twilio import TwilioRestException
from twilio.rest import TwilioRestClient

from hackerutils import get_dotenv, get_log_path, sh

dotenv = get_dotenv()

TWILIO_ACCOUNT_SID = dotenv['TWILIO_ACCOUNT_SID']
TWILIO_AUTH_TOKEN = dotenv['TWILIO_AUTH_TOKEN']

LOG_FILE_PATH = get_log_path('smack_my_bitch_up.txt')


def main():
# Skip on weekends.
if datetime.date.today().weekday() in (0, 6,):
return

# Exit early if no sessions with my_username are found.
if not any(s.startswith(b'my_username ') for s in sh('who').split(b'\n')):
return

client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

# Phone numbers.
my_number = '+xxx'
her_number = '+xxx'

reasons = [
'Working hard',
'Gotta ship this feature',
'Someone fucked the system again',
]

try:
# Send a text message.
client.messages.create(
to=her_number,
from_=my_number,
body='Late at work. ' + random.choice(reasons),
)
except TwilioRestException as e:
# Log errors.
with LOG_FILE_PATH.open('a') as f:
f.write('Failed to send SMS: {}'.format(e))
raise


if __name__ == '__main__':
main()

0 comments on commit 11a67df

Please sign in to comment.