
import datetime
import json
import sys

import requests
import pytz
from twilio.rest import TwilioRestClient

import conf


def send(s):
    client.sms.messages.create(to=conf.TO, from_=conf.FROM, body=s)

# Use the first arg as the message to send, or use the default if not specified
default_message = "You haven't committed anything today!"
message = sys.argv[1] if len(sys.argv) > 1 else default_message

# Initialise twilio stuff
client = TwilioRestClient(conf.ACCOUNT_SID, conf.AUTH_TOKEN)

# Get Github contributions activity
url = 'https://github.com/users/%s/contributions_calendar_data' % conf.USERNAME
request = requests.get(url)
if request.ok:
    data = json.loads(request.text)

    # Set to epoch begin just in case this is a totally new account
    latest_contribution = datetime.datetime(1970, 1, 1, 0, 0)

    # Get data for the latest contribution
    for i in reversed(data):
        if i[1] > 0:
            latest_contribution = datetime.datetime.strptime(i[0], '%Y/%m/%d')
            break

    # Find out today's date in PST (since Github uses PST)
    today = datetime.datetime.now(pytz.timezone('US/Pacific'))

    # Haven't contributed anything today?
    if latest_contribution.date() < today.date():  
      send(message)
else:
    send('There was a problem accessing the Github API :(')


import json
import sys

import requests
from twilio.rest import TwilioRestClient

import conf


def send(s):
    client.sms.messages.create(to=conf.TO, from_=conf.FROM, body=s)

# Use the first arg as the message to send, or use the default if not specified
default_message = "You haven't committed anything today!"
message = sys.argv[1] if len(sys.argv) > 1 else default_message

# Initialise twilio stuff
client = TwilioRestClient(conf.ACCOUNT_SID, conf.AUTH_TOKEN)

# Get Github contributions activity
url = 'https://github.com/users/%s/contributions_calendar_data' % conf.USERNAME
request = requests.get(url)
if request.ok:
    try:
        data = json.loads(request.text)

        # Get the number of commits made today
        commits_today = data[-1][1]

        if not commits_today:
          send(message)
    except:
        send('There was an error getting the number of commits today')
else:
    send('There was a problem accessing the Github API :(')

Improve logic in determining latest contribution
Improve logic in determining latest contribution

Looks like the list will always contain data for the last 366 days, and
the last entry in the list will always contain data for the current day
(PST). Much simpler this way.

Added some generic error-handling just in case this isn't true.

  
- import datetime
  import json
  import sys
  
  import requests
- import pytz
  from twilio.rest import TwilioRestClient
  
  import conf
  
  
  def send(s):
      client.sms.messages.create(to=conf.TO, from_=conf.FROM, body=s)
  
  # Use the first arg as the message to send, or use the default if not specified
  default_message = "You haven't committed anything today!"
  message = sys.argv[1] if len(sys.argv) > 1 else default_message
  
  # Initialise twilio stuff
  client = TwilioRestClient(conf.ACCOUNT_SID, conf.AUTH_TOKEN)
  
  # Get Github contributions activity
  url = 'https://github.com/users/%s/contributions_calendar_data' % conf.USERNAME
  request = requests.get(url)
  if request.ok:
+     try:
-     data = json.loads(request.text)
+         data = json.loads(request.text)
  
-     # Set to epoch begin just in case this is a totally new account
-     latest_contribution = datetime.datetime(1970, 1, 1, 0, 0)
+         # Get the number of commits made today
+         commits_today = data[-1][1]
  
+         if not commits_today:
-     # Get data for the latest contribution
-     for i in reversed(data):
-         if i[1] > 0:
-             latest_contribution = datetime.datetime.strptime(i[0], '%Y/%m/%d')
-             break
- 
-     # Find out today's date in PST (since Github uses PST)
-     today = datetime.datetime.now(pytz.timezone('US/Pacific'))
- 
-     # Haven't contributed anything today?
-     if latest_contribution.date() < today.date():  
-       send(message)
+           send(message)
+     except:
+         send('There was an error getting the number of commits today')
  else:
      send('There was a problem accessing the Github API :(')
  
Improve logic in determining latest contribution
## Code Before:

import datetime
import json
import sys

import requests
import pytz
from twilio.rest import TwilioRestClient

import conf


def send(s):
    client.sms.messages.create(to=conf.TO, from_=conf.FROM, body=s)

# Use the first arg as the message to send, or use the default if not specified
default_message = "You haven't committed anything today!"
message = sys.argv[1] if len(sys.argv) > 1 else default_message

# Initialise twilio stuff
client = TwilioRestClient(conf.ACCOUNT_SID, conf.AUTH_TOKEN)

# Get Github contributions activity
url = 'https://github.com/users/%s/contributions_calendar_data' % conf.USERNAME
request = requests.get(url)
if request.ok:
    data = json.loads(request.text)

    # Set to epoch begin just in case this is a totally new account
    latest_contribution = datetime.datetime(1970, 1, 1, 0, 0)

    # Get data for the latest contribution
    for i in reversed(data):
        if i[1] > 0:
            latest_contribution = datetime.datetime.strptime(i[0], '%Y/%m/%d')
            break

    # Find out today's date in PST (since Github uses PST)
    today = datetime.datetime.now(pytz.timezone('US/Pacific'))

    # Haven't contributed anything today?
    if latest_contribution.date() < today.date():  
      send(message)
else:
    send('There was a problem accessing the Github API :(')

## Instruction:
Improve logic in determining latest contribution
## Code After:

import json
import sys

import requests
from twilio.rest import TwilioRestClient

import conf


def send(s):
    client.sms.messages.create(to=conf.TO, from_=conf.FROM, body=s)

# Use the first arg as the message to send, or use the default if not specified
default_message = "You haven't committed anything today!"
message = sys.argv[1] if len(sys.argv) > 1 else default_message

# Initialise twilio stuff
client = TwilioRestClient(conf.ACCOUNT_SID, conf.AUTH_TOKEN)

# Get Github contributions activity
url = 'https://github.com/users/%s/contributions_calendar_data' % conf.USERNAME
request = requests.get(url)
if request.ok:
    try:
        data = json.loads(request.text)

        # Get the number of commits made today
        commits_today = data[-1][1]

        if not commits_today:
          send(message)
    except:
        send('There was an error getting the number of commits today')
else:
    send('There was a problem accessing the Github API :(')
