Last active
October 11, 2016 08:58
-
-
Save conoro/8437e2d5c50adee1832e to your computer and use it in GitHub Desktop.
RHMAP Studio email extractor for Mailshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
RHMAP Studio email extractor for Mailshots | |
Copyright Conor O'Neill 2016, [email protected] | |
MIT License https://opensource.org/licenses/MIT | |
You may need to create a User API key in your Profile in the Studio called X-FH-AUTH-USER | |
Then: | |
[1] Login to an RHMAP Studio e.g. https://prod-demos-emea.redhat.feedhenry.com/#admin/users or Chrome Inspect the "feedhenry" cookie value | |
[2] Or get the feedhenry cookie value by logging into a domain with fhc and seeing its value in fhcrc file in your user directory | |
[3] curl -H "Content-Type: application/json" --cookie "feedhenry=blahblah" https://prod-demos-emea.redhat.feedhenry.com/box/srv/1.1/admin/user/list | |
Also create conf.json in your working directory which looks like this and put the cookie in APIKey (I know I know!): | |
{ | |
"APIKey": "blahblah", | |
"UserURL": "https://prod-demos-emea.redhat.feedhenry.com/box/srv/1.1/admin/user/list" | |
} | |
Email addresses are saved to emails.csv | |
Also use this afterwards to get only Red Hatters: grep -i 'redhat' emails.csv > redhatters.csv | |
Note: Do not run any of this stuff in Windows Powershell. It outputs with UCS2 encoding which messes everything up. Use CMD instead. | |
*/ | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
"github.com/jmoiron/jsonq" | |
) | |
type configuration struct { | |
APIKey string | |
UserURL string | |
} | |
func main() { | |
configfile, _ := os.Open("conf.json") | |
decoder := json.NewDecoder(configfile) | |
configuration := configuration{} | |
err := decoder.Decode(&configuration) | |
if err != nil { | |
fmt.Println("config error:", err) | |
} | |
ofile, err := os.Create("./emails.csv") | |
if err != nil { | |
fmt.Printf("Output File error: %v\n", err) | |
os.Exit(1) | |
} | |
defer ofile.Close() | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", configuration.UserURL, nil) | |
rawCookies := "feedhenry=" + configuration.APIKey | |
req.Header.Set("Cookie", rawCookies) | |
//req.Header.Add("X-FH-AUTH-USER", configuration.APIKey) | |
response, err := client.Do(req) | |
if err != nil { | |
fmt.Printf("%s", err) | |
os.Exit(1) | |
} | |
defer response.Body.Close() | |
data := map[string]interface{}{} | |
dec := json.NewDecoder(response.Body) | |
dec.Decode(&data) | |
jq := jsonq.NewQuery(data) | |
emails := 0 | |
for { | |
email, err := jq.String("list", strconv.Itoa(emails), "fields", "email") | |
if err != nil { | |
if strings.Contains(err.Error(), "out of bounds") { | |
ofile.Sync() | |
fmt.Println("Done!") | |
os.Exit(1) | |
} else { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
n, err := ofile.WriteString(fmt.Sprintf("%s\n", email)) | |
if err != nil { | |
fmt.Println(n, err) | |
} | |
emails++ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed out-of-bounds error