#!/usr/bin/env python

# Script to tweet Raspberry Pi IP Address, date, time and IP-based geolocation
# Requires Twython, API credentials set as env vars

import os
import time
import subprocess
from twython import Twython
import twitter_api_creds

# Get the date & time
getdate = 'date'
time = os.popen(getdate).readline()

# Determine IP Address
arg = 'ip route list'
p = subprocess.Popen(arg, shell=True, stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src') + 1]

# Set Twitter Credentials from environment variables
CONSUMER_KEY = os.getenv("CONSUMER_KEY")
CONSUMER_SECRET = os.getenv("CONSUMER_SECRET")
ACCESS_KEY = os.getenv("ACCESS_KEY")
ACCESS_SECRET = os.getenv("ACCESS_SECRET")

api = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET)

# Get geolocation using IP address
getlat = 'curl -s https://whatismycountry.com/ | sed -n \'s/.*Coordinates \\(.*\\)<.*/\\1/p\' | cut -d \' \' -f1'
getlong = 'curl -s https://whatismycountry.com/ | sed -n \'s/.*Coordinates \\(.*\\)<.*/\\1/p\' | cut -d \' \' -f2'

lat = os.popen(getlat).readline()
long = os.popen(getlong).readline()

lat = lat.strip()
long = long.strip()

# Tweet
api.update_status(status='#RaspberryPi #IP ' + ipaddr + ' on ' + time + '', lat=(lat), long=(long))

#!/usr/bin/env python

# Script to tweet Raspberry Pi IP Address, date, time and IP-based geolocation
# Requires Twython, API credentials set as env vars

import os
import time
import subprocess
from twython import Twython
import twitter_api_creds

# Get the date & time
getdate = 'date'
time = os.popen(getdate).readline()

# Determine IP Address
arg = 'ip route list'
p = subprocess.Popen(arg, shell=True, stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src') + 1]

# Set Twitter Credentials from environment variables
CONSUMER_KEY = os.getenv("CONSUMER_KEY")
CONSUMER_SECRET = os.getenv("CONSUMER_SECRET")
ACCESS_KEY = os.getenv("ACCESS_KEY")
ACCESS_SECRET = os.getenv("ACCESS_SECRET")

api = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET)

# Get geolocation using IP address
getlat = 'curl -s https://whatismycountry.com/ | sed -n \'s/.*Coordinates \\(.*\\)<.*/\\1/p\' | cut -d \' \' -f1'
getlong = 'curl -s https://whatismycountry.com/ | sed -n \'s/.*Coordinates \\(.*\\)<.*/\\1/p\' | cut -d \' \' -f2'

lat = os.popen(getlat).readline()
long = os.popen(getlong).readline()

lat = lat.strip()
long = long.strip()

# Tweet
api.update_status(status='#RaspberryPi #IP ' + ipaddr + ' on ' + time + '', lat=(lat), long=(long))

#!/usr/bin/env python # Script to tweet Raspberry Pi IP Address, date, time and IP-based geolocation # Requires Twython, API credentials set as env vars # Get the date & time # Determine IP Address # Set Twitter Credentials from environment variables # Get geolocation using IP address # Tweet #IP ' + ipaddr + ' on ' + time + '', lat=(lat), long=(long))