Documentation
¶
Overview ¶
A web scraper for the Social Security Administration's life expectancy calculation.
Given a gender code ("m" or "f") and a date of birth in YYYY-MM-DD format, it will do a POST request to the Social Security Administrations website using the format defined by its [Life Expectancy Calculator], parse the response, and return the result.
Index ¶
- Constants
- func AlmostEqual(a, b float64) bool
- func FormatPostData(sex string, dob time.Time) string
- func GetDateFields(dob time.Time) (monthOfBirth, dayOfBirth, yearOfBirth string)
- func ParseCurrentAge(currentAgeString string) float64
- func ParseFloat(s string) float64
- func PostRequest(postData string) (string, error)
- type Response
Examples ¶
Constants ¶
const ( ISO_FORMAT = "2006-01-02" Float64EqualityThreshold = 1e-9 )
Variables ¶
This section is empty.
Functions ¶
func FormatPostData ¶
FormatPostData creates a dictionary of the data that will be used in the POST method
func GetDateFields ¶
GetDateFields extracts date fields and formats them in the way the cgi program expects them.
Output fields:
- monthofbirth is the month field minus 1 (0-11)
- dayofbirth is the day field with leading zero if less than 10
- yearofbirth is a 4-digit year field These fields are returned as tuple of 3 strings
func ParseCurrentAge ¶
ParseCurrentAge finds an age value in a string. This can be a simple integer: "68" or a combination of years and month(s): ("68 and 3 months")
func ParseFloat ¶
ParseFloat converts a string to a float, ignoring any errors
func PostRequest ¶
PostRequest posts the request and gets the response.
Parameters postdata: a string containing the parameters, e.g., for Keith Richards:
- "sex" : "m",
- "monthofbirth": "11", # Note: website expects actual month - 1
- "dayofbirth": "18",
- "yearofbirth": "1943",
Returns: a string containing the returned HTML
Separated from the mainline so that this function can be mocked and test data used instead of a live call to the URL.
Types ¶
type Response ¶
type Response struct {
CurrentAge float64
AdditionalYears float64
TotalYears float64
DeathDate time.Time
}
Response is a structure that contains the results of parsing a response.
func Get ¶
Get issues a POST request to the website, parses the response, and returns the results.
Parameters: - sex: either "m" or "f" - dob: date of birth
Returns: - The response
Example ¶
This example illustrates how to use the longevity package in a standalone mainline
const ISO_FORMAT = "2006-01-02"
sex := "m"
dob, _ := time.Parse(ISO_FORMAT, "1943-12-18")
// Invoke the requester
resp, err := Get(sex, dob)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
currentAge := resp.CurrentAge
additionalYears := resp.AdditionalYears
totalYears := resp.TotalYears
deathDate := resp.DeathDate
deathYear, deathMonth, deathDay := deathDate.Date()
fmt.Printf("current age = %.1f\n", currentAge)
fmt.Printf("additional years = %.1f\n", additionalYears)
fmt.Printf("total years = %.1f\n", totalYears)
fmt.Printf("expiration date = %4d-%02d-%02d\n", deathYear, deathMonth, deathDay)
func ParseResponse ¶
ParseResponse parses the response HTML to get the desired output. The section of interest is this:
Inside <table ... summary="life expectancy table" after the first <tr>...</tr> ...
<tr><td style="text-align:left">68 and 6 months<sup>a</sup></td> <td>16.5</td> <td>85.0</td></tr>