[PYTHON] Get a global IP and export it to Google Spreadsheets

Overview

I made a python script that periodically writes the global IP for ssh connection to Raspberry pi to Google Spreadsheet.

Advance preparation

Creating config.ini

The following information is summarized in config.ini.

--Google account --Google app password (app-specific) --Spreadsheet ID of Google Sheets prepared for IP export

config.ini


[google]
username = ****
password = ****
spreadsheet_key = *****(Spreadsheet ID)

The spreadsheet ID corresponds to the ***** part.

https://docs.google.com/spreadsheets/d/*******/edit#gid=0

Installation of required libraries

Use gdata.

sudo apt-get install python-gdata

Create your own class to handle Google Spreadsheets

I made a class that can be updated by giving IP and position (row, column). --Export IP --Line --Column

By default, the first sheet (od6) is selected as the sheet.

Reference: http://qiita.com/debiru/items/27a48dfb3b1c010cd478

mySpreadsheet.py


import gdata.spreadsheets.client
import gdata.spreadsheet.service
import ConfigParser

class mySpreadsheet:

  user = ''
  passwd = ''
  key = ''
  sheet_id= ''

  def __init__(self,config):
    inifile = ConfigParser.SafeConfigParser()
    inifile.read(config)
    self.user = unicode(inifile.get("google","username"))
    self.passwd = unicode(inifile.get("google","password"))
    self.key = unicode(inifile.get("google","spreadsheet_key"))
    self.sheet_id = 'od6'

  def updateCell(self,value,row,col):
    client = gdata.spreadsheet.service.SpreadsheetsService(self.user,self.passwd)
    client.ProgrammaticLogin()
    cell = client.UpdateCell(inputValue = value,
                             row = row, col = col,
                             key = self.key,
                             wksht_id = self.sheet_id)

if __name__ == "__main__":
  m = mySpreadsheet('config.ini')
  m.updateCell('hoge',1,1)

From getting an IP to updating to google spreadsheet

If you read the above class and pass the IP, it's OK

updateip.py


import json
from urllib2 import urlopen
from mySpreadsheet import *

m = mySpreadsheet('config.ini')
ip = json.load(urlopen('http://httpbin.org/ip'))['origin']
m.updateCell(ip,1,2)
print ip

Recommended Posts

Get a global IP and export it to Google Spreadsheets
To get a local IP address programmatically
Get an image from a web page and resize it
[Google Colab] How to interrupt learning and then resume it
Get a Python web page, character encode it, and display it
Recursively get the Excel list in a specific folder with python and write it to Excel.
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
A memorandum when I tried to get it automatically with selenium
Get conversions and revenue with Google Analytics API and report to Slack
I want to create a pipfile and reflect it in docker
Export Google Analytics Standard to BigQuery
Get and visualize google search trends
Selenium and python to open google
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
Deploy a Python app on Google App Engine and integrate it with GitHub
How to get a specific column name and index name in pandas DataFrame
Until you get a snapshot of Amazon Elasticsearch service and restore it
Get tweets with Google Cloud Function and automatically save images to Google Photos
[Personal memo] Get data on the Web and make it a DataFrame
How to make a container name a subdomain and make it accessible in Docker
Start the webcam to take a still image and save it locally
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
How to split and save a DataFrame
How to get a stacktrace in python
Read Python csv and export to txt
Reinforcement learning 35 python Local development, paste a link to myModule and import it.
[Introduction to Pandas] Read a csv file without a column name and give it a column name
Try to get a web page and JSON file using Python's Requests library
I made a tool to notify Slack of Connpass events and made it Terraform
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to write an element to a file with numpy and check it.
[Python] Concatenate a List containing numbers and write it to an output file.
How to read a serial number file in a loop, process it, and graph it
Load a photo and make a handwritten sketch. With zoom function. Tried to make it.
A tool to insert the country name and country code in the IP address part
I set up TensowFlow and was addicted to it, so make a note