forked from ipinto/checkIPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkIPy.py
executable file
·65 lines (49 loc) · 1.97 KB
/
checkIPy.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import sys
import urllib2
import dbManager
import settings
from messaging.messenger import Messenger
from messaging.notifiers.consoler import ConsoleNotifier
from messaging.notifiers.emailer import EmailNotifier
from messaging.notifiers.telegramer import TelegramNotifier
def get_arguments():
parser = argparse.ArgumentParser(description='Check your external IP.')
parser.add_argument('-d', '--diff', action='store_true',
help='check if the IP changed since last time. ' +
"If it didn't, it will do nothing.")
parser.add_argument('-c', '--console', action='store_true',
help='print your IP on console (default action)')
parser.add_argument('-e', '--email', action='store_true',
help='send an email with your IP')
parser.add_argument('-t', '--telegram', action='store_true',
help='send a Telegram message with your IP')
return parser.parse_args()
def get_device_name():
if settings.DEVICE_NAME:
return '[{}] '.format(settings.DEVICE_NAME)
return ''
def get_message(current_ip, change):
message = 'My IP is: {}'.format(current_ip)
if change:
last_ip = dbManager.get_last_ip()
if last_ip == current_ip:
sys.exit()
message = 'My IP has changed: {} -> {}'.format(last_ip, current_ip)
return message
def get_ip():
return urllib2.urlopen(settings.IP_SOURCE).read()
if __name__ == "__main__":
ip = get_ip()
messenger = Messenger()
args = get_arguments()
if args.email:
messenger.add_notifier(EmailNotifier(get_device_name()))
if args.telegram:
messenger.add_notifier(TelegramNotifier(get_device_name()))
if args.console or (not args.email and not args.telegram):
messenger.add_notifier(ConsoleNotifier())
messenger.notify(get_message(ip, args.diff))
dbManager.update_ip(ip)