#!/usr/bin/env python

"""
CapitolWords.py:
Pull a phrase from the Capitol Words API and place the dates of US Congress in Session,
the total count that day of the word being said, and also the tf-idf count.
The td-idf count is erm frequency–inverse document frequency which shows the word usage
in relation to other words used that day.
"""

__author__ = "Marshall Humble"
__copyright__ = "MIT License"

import requests, json, csv

# Define the Phrase we want to look for
my_phrase = 'economy'
csv_file = my_phrase + '.csv'


def get_words(search_phrase):
    # Communitcate with the API via requests
    api_key = '<YOUR_API_KEY:https://sunlightfoundation.com/api/>'
    base_url = 'http://capitolwords.org/api/1/dates.json?'
    time_series__payload = {
        'phrase': search_phrase,
        'percentages': 'true',
        'granularity': 'day',
        'apikey': api_key
    }

    # take the response and turn it into workable JSON
    r = requests.get(base_url, params=time_series__payload)
    json_string = r.text
    json_data = json.loads(json_string)
    results = json_data['results']

    # write the data to a csv
    f = csv.writer(open(csv_file, "wt"))
    f.writerow(["date", "count", "tf-idf"])
    for x in results:
        f.writerow([x["day"],
                    x["total"],
                    x["percentage"]])


get_words(my_phrase)

@@ -424,16 +424,57 @@
 son, csv
+%0Afrom os import path%0Afrom sys import exit
 %0A%0A# Defi
@@ -668,44 +668,8 @@
 _KEY
-:https://sunlightfoundation.com/api/
 %3E'%0A 
@@ -945,64 +945,431 @@
     
-r = requests.get(base_url, params=time_series__payload)%0A
+if path.isfile(csv_file) == False:%0A        try:%0A            r = requests.get(base_url, params=time_series__payload)%0A%0A        except requests.exceptions.Timeout:%0A            print(%22Request timeout.%22)%0A        except requests.exceptions.TooManyRedirects:%0A            print(%22Incorrect URL, please check%22)%0A        except requests.exceptions.RequestException as e:%0A            print (e)%0A            exit(1)%0A        else:%0A        
     
@@ -1389,16 +1389,24 @@
  r.text%0A
+        
     json
@@ -1437,16 +1437,24 @@
 string)%0A
+        
     resu
@@ -1481,16 +1481,24 @@
 ults'%5D%0A%0A
+        
     # wr
@@ -1519,16 +1519,24 @@
 o a csv%0A
+        
     f = 
@@ -1568,16 +1568,24 @@
  %22wt%22))%0A
+        
     f.wr
@@ -1620,16 +1620,24 @@
 -idf%22%5D)%0A
+        
     for 
@@ -1646,24 +1646,32 @@
 in results:%0A
+        
         f.wr
@@ -1708,16 +1708,24 @@
         
+ 
+       
 x%5B%22total
@@ -1724,24 +1724,32 @@
 x%5B%22total%22%5D,%0A
+        
             
@@ -1774,16 +1774,166 @@
 age%22%5D%5D)%0A
+            print (csv_file, %22written, exiting%22)%0A%0A    elif path.isfile(csv_file)  == True:%0A        print (csv_file, %22exists, exiting%22)%0A        exit(0)
 %0A%0Aget_wo

Update CapitolWords.py