Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
Add IpIfy (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saphyel authored Sep 19, 2017
1 parent 9523909 commit e1c327c
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Create a file in /etc/profile.d/ipteller.sh with the variables:
```bash
export GMAIL_USER='[email protected]'
export GMAIL_PASS='Password'
export IP_PROVIDER='jsonip'
```

and then in execute `crontab -e` and add this line at the end:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
PYTHONUSERBASE: /app/vendor
PYTHONPATH: '/app/vendor/lib/python2.7/site-packages'
PATH: '/app/vendor/bin:$PATH'
IP_PROVIDER: 'jsonip'
TWINE_USERNAME: 'User'
TWINE_PASSWORD: 'Password'
GMAIL_USER: '[email protected]'
Expand Down
6 changes: 3 additions & 3 deletions features/steps/step_mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
def step_impl(context, subject, body):
context.subject = subject
context.body = body
context.mailer = Mailer(subject, body)
context.message = Mailer.message({'user': 'test', 'pwd': 'pwd', 'subject': subject, 'body': body})


@then('I expect to get it with a proper format')
def step_impl(context):
assert context.subject in context.mailer.message()
assert context.body in context.mailer.message()
assert context.subject in context.message
assert context.body in context.message
13 changes: 13 additions & 0 deletions ipteller/ipify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python

from providerip import ProviderIp


class IpIfy(ProviderIp):
def __init__(self):
ProviderIp.__init__(self)
self.url = 'https://api.ipify.org?format=json'

@staticmethod
def get_ip(uri):
return uri["ip"]
21 changes: 19 additions & 2 deletions ipteller/ipteller.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
#!/usr/bin/env python
"""This script runs the whole app"""

from os import environ
from address import Address
from ipify import IpIfy
from jsonip import JsonIp
from mailer import Mailer

address = Address('address.txt')
provider = JsonIp()
try:
user = environ["GMAIL_USER"]
pwd = environ["GMAIL_PASS"]
except KeyError as err:
print('You need to add the environment variable: %s' % err)
quit(1)
try:
provider = environ["IP_PROVIDER"]
if provider == 'ipify':
provider = IpIfy()
else:
raise KeyError
except KeyError:
provider = JsonIp()

ip = provider.get_ip(provider.get_response())

if ip != address.load():
address.save(ip)
deliver = Mailer('IP Teller', 'Your IP changed to: ' + ip).send()
data = {'user': user, 'pwd': pwd, 'subject': 'IP Teller', 'body': 'Your IP changed to: %s' % ip}
deliver = Mailer().send(data)
15 changes: 5 additions & 10 deletions ipteller/jsonip.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#!/usr/bin/env python

import logging
import requests
from providerip import ProviderIp


class JsonIp(object):
@staticmethod
def get_response():
url = 'https://jsonip.com/'
try:
return requests.get(url).json()
except ValueError as err:
logging.exception(err)
class JsonIp(ProviderIp):
def __init__(self):
ProviderIp.__init__(self)
self.url = 'https://jsonip.com/'

@staticmethod
def get_ip(uri):
Expand Down
25 changes: 10 additions & 15 deletions ipteller/mailer.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#!/usr/bin/env python

import logging
import os
import smtplib


class Mailer(object):
def __init__(self, subject, body):
self.host = "smtp.gmail.com"
self.port = 465
self.user = os.environ["GMAIL_USER"]
self.pwd = os.environ["GMAIL_PASS"]
self.subject = subject
self.body = body

def send(self):
@staticmethod
def send(data):
host = "smtp.gmail.com"
port = 465
try:
server = smtplib.SMTP_SSL(self.host, self.port)
server.login(self.user, self.pwd)
server.sendmail(self.user, self.user, self.message())
server = smtplib.SMTP_SSL(host, port)
server.login(data['user'], data['pwd'])
server.sendmail(data['user'], data['user'], Mailer.message(data))
server.close()
logging.info('Notification of the new IP sent')
except smtplib.SMTPAuthenticationError as error:
Expand All @@ -28,11 +22,12 @@ def send(self):
logging.exception(error.message)
quit(0)

def message(self):
@staticmethod
def message(data):
return """\
From: %s
To: %s
Subject: %s
%s
""" % (self.user, self.user, self.subject, self.body)
""" % (data['user'], data['user'], data['subject'], data['body'])
15 changes: 15 additions & 0 deletions ipteller/providerip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python

import logging
import requests


class ProviderIp(object):
def __init__(self):
self.url = ''

def get_response(self):
try:
return requests.get(self.url).json()
except ValueError as err:
logging.exception(err)
11 changes: 7 additions & 4 deletions releaser.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env bash

GH=https://github.com/Saphyel/ipteller/compare/
LAST_TAG=`git describe --tags --abbrev=0 HEAD^`
LAST_TAG=`git describe --tags --abbrev=0 origin/master`
TMP_FILE=CHANGELOG.md

printf "**Changelog** (since "${GH}${LAST_TAG}"...NEW)\n" > CHANGELOG.md
git log ${LAST_TAG}..HEAD --pretty=format:"* %s" >> CHANGELOG.md
printf "\n" >> CHANGELOG.md
git fetch origin
printf "**Changelog** (since "${GH}${LAST_TAG}"...NEW)\n" > ${TMP_FILE}
git log ${LAST_TAG}..HEAD --pretty=format:"* %s" >> ${TMP_FILE}
printf "\n" >> ${TMP_FILE}
cat ${TMP_FILE} && rm ${TMP_FILE}

0 comments on commit e1c327c

Please sign in to comment.