Skip to content

Commit

Permalink
Support sending Twitter DMs to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestaylr committed Jan 12, 2017
1 parent b4b4e1d commit 1f121a6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
4 changes: 4 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
; Configuration
[daemon]
execution_offset=0
name=N1

[monitor]
delta_requests=3

[twitter]
should_tweet=true
tweet_duplicate_check=6
user_id=815812431318499329
consumer_key=COsko3fGS1et3oAYiRrET6KUt
consumer_secret=lQp2XcakoXYsVEXgimCN60qXxdvtZs9QMQgDve2rFoU81CGF9k
access_token=815812431318499329-1IOZcIT4X28yAHaDOlKGIStWs6iaGxu
Expand Down
15 changes: 10 additions & 5 deletions crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,21 @@ def is_duplicate(product, name):
media_id = None

try:
dat_filename = 'locks/{}.dat'.format(site['name'])
with open(dat_filename, 'a') as dat:
dat.write('{}\n'.format(product['loc']))

print 'Tweeting product', product['image:image']['image:title']
twitter.tweet('{} {}'.format(
product['image:image']['image:title'],
product['loc']
), media_id)

dat_filename = 'locks/{}.dat'.format(site['name'])
with open(dat_filename, 'a') as dat:
dat.write('{}\n'.format(product['loc']))

msg = '[{}] Posted tweet for {}'.format(
config.get('daemon', 'name'),
product['loc']
)
twitter.send_dm(msg)

except UnicodeEncodeError:
print 'Caught UnicodeEncodeError'

Expand Down
5 changes: 5 additions & 0 deletions daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
null = open(os.devnull, 'w')
offset = config.getint('daemon', 'execution_offset')

from lib import twitter
n = config.get('daemon', 'name')
twitter.send_dm('[{}] Daemon start!'.format(n))
twitter.send_dm('[{}] Running every minute at {} second offset'.format(n, offset))

while True:
# Conduct execution
subprocess.call(['venv/bin/python2', 'monitor.py'],
Expand Down
26 changes: 26 additions & 0 deletions lib/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ def upload_media(link):
except ValueError:
raise ValueError('No media ID returned')

def send_dm(text, user_handle=None):
# Build the OAuth client
consumer = oauth.Consumer(
key=config.get('twitter', 'consumer_key'),
secret=config.get('twitter', 'consumer_secret')
)
token = oauth.Token(
key=config.get('twitter', 'access_token'),
secret=config.get('twitter', 'access_secret')
)
client = oauth.Client(consumer, token)

# Send the request
resp, content = client.request(
'https://api.twitter.com/1.1/direct_messages/new.json',
method='POST',
body=urllib.urlencode({
'text': text,
'user_id': (config.get('twitter', 'user_id')
if user_handle is None
else user_handle
)
}),
headers=None
)

def has_been_tweeted(match):
# Build the OAuth client
consumer = oauth.Consumer(
Expand Down
8 changes: 7 additions & 1 deletion monitor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
import json, os, base64, sys
import requests
import oauth2 as oauth, urllib
from datetime import datetime
from dateutil import parser

Expand Down Expand Up @@ -95,6 +94,13 @@ def get_products(url, limit):
product['title'],
link
), media_id)

msg = '[{}] Posted tweet for {}'.format(
config.get('daemon', 'name'),
product['handle']
)
twitter.send_dm(msg)

except UnicodeEncodeError:
print 'Caught UnicodeEncodeError'

Expand Down

0 comments on commit 1f121a6

Please sign in to comment.