-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_update.go
55 lines (41 loc) · 1.25 KB
/
status_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"github.com/PuerkitoBio/goquery"
)
const (
caseStatusURL = "https://egov.uscis.gov/casestatus/mycasestatus.do"
caseStatusContainerSelector = ".main-row"
caseStatusSelector = "h1"
caseStatusDescriptionSelector = ".rows p"
)
func statusUpdate(schedulerChan chan bool, caseNumber string, db database, notifier notifier) {
previousCaseStatus := db.loadCaseStatus(caseNumber)
for {
<-schedulerChan
status := getLatestStatus(caseNumber)
if previousCaseStatus.Status != status.Status {
notifier.Notify(fmt.Sprintf("Status was changed from %s to %s\n", previousCaseStatus.Status, status.Status))
}
db.saveCaseStatus(caseNumber, &status)
previousCaseStatus = status
}
}
func getLatestStatus(caseNumber string) caseStatus {
resp, err := http.PostForm(caseStatusURL, url.Values{"appReceiptNum": {caseNumber}})
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}
mainRow := doc.Find(caseStatusContainerSelector)
status := mainRow.Find(caseStatusSelector).First().Text()
description := mainRow.Find(caseStatusDescriptionSelector).First().Text()
return caseStatus{status, description}
}