forked from thinkst/canarytokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_input_linkedin.py
71 lines (60 loc) · 3.1 KB
/
channel_input_linkedin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import simplejson
import twill
from twisted.application.internet import TimerService
from twisted.python import log
from canarydrop import Canarydrop
from channel import InputChannel
from queries import get_canarydrop, get_all_imgur_tokens, save_imgur_token,\
get_imgur_count, get_all_linkedin_accounts,\
save_linkedin_account, get_linkedin_viewer_count
from exception import LinkedInFailure
from constants import INPUT_CHANNEL_LINKEDIN
#create_linkedin_account(username='[email protected]', password='mooo')
#ht = get_canarydrop(canarytoken=get_linkedin_account(username='[email protected]')['canarytoken'])
#ht['alert_email_enabled'] = True
#ht['alert_email_recipient'] = '[email protected]'
#save_canarydrop(ht)
class ChannelLinkedIn(InputChannel):
"""Input channel that polls LinkedIn for changes to the profile view count, and
alerts when they climb."""
CHANNEL = INPUT_CHANNEL_LINKEDIN
def __init__(self, min_delay=3600*24, switchboard=None):
log.msg('Started channel {name}'.format(name=self.CHANNEL))
super(ChannelLinkedIn, self).__init__(switchboard=switchboard,
name=self.CHANNEL)
self.min_delay = min_delay
self.service = TimerService(self.min_delay, self.schedule_polling)
def schedule_polling(self,):
"""A dummy method. For now, all view count polls are run immediately.
In the future they'll be spread out over the interval."""
try:
for linkedin_account in get_all_linkedin_accounts():
self.poll(linkedin_account=linkedin_account)
except Exception as e:
log.err('LinkedIn error: {error}'.format(error=e))
def poll(self, linkedin_account=None):
try:
current_count = get_linkedin_viewer_count(
username=linkedin_account['username'],
password=linkedin_account['password'])
except LinkedInFailure as e:
log.err('Could not retrieve linkedin view count: {error}'\
.format(error=e))
return
if current_count > linkedin_account['count']:
canarydrop = Canarydrop(**get_canarydrop(
canarytoken=linkedin_account['canarytoken']))
self.dispatch(canarydrop=canarydrop, count=current_count,
linkedin_username=linkedin_account['username'])
linkedin_account['count'] = current_count
save_linkedin_account(linkedin_account=linkedin_account)
def format_additional_data(self, **kwargs):
log.msg('%r' % kwargs)
additional_report = ''
if kwargs.has_key('count') and kwargs['count']:
additional_report += 'View Count: {count}\r\n'.format(
count=kwargs['count'])
if kwargs.has_key('linkedin_username') and kwargs['linkedin_username']:
additional_report += 'LinkedIn User: {username}\r\n'.format(
username=kwargs['linkedin_username'])
return additional_report