Ejemplo n.º 1
0
func downloadAndParse(j *job.Job) JSON {
	resp, err := http.Get(buildReadabilityUrl(j.Url.String()))
	if err != nil {
		logger.Panicf("Readability Error: %s", err.Error())
	}
	defer resp.Body.Close()
	return util.ParseJSON(resp.Body, func(err error) {
		logger.Panicf("JSON Parsing Error: %s", err.Error())
	})
}
Ejemplo n.º 2
0
func Mail(j *job.Job) {
	go safely.Do(logger, j, FriendlyMessage, func() {
		if stat, err := os.Stat(j.MobiFilePath()); err != nil {
			logger.Panicf("Something weird happen. Mobi is missing in postmark.go: %s", err.Error())
		} else {
			if stat.Size() > MaxAttachmentSize {
				blacklist.Blacklist(j.Url.String())
				failFriendly("Sorry, this article is too big to send!")
			}
		}

		payload := map[string]Any{
			"From":     from,
			"To":       j.Email,
			"Subject":  Subject,
			"TextBody": fmt.Sprintf("Straight to your Kindle! %s: %s", j.Title, j.Url),
			"Attachments": []Any{
				map[string]Any{
					"Name":        j.MobiFilename(),
					"ContentType": "application/octet-stream",
					"Content":     readFile(j.MobiFilePath()),
				},
			},
		}

		var buffer bytes.Buffer
		json.NewEncoder(&buffer).Encode(payload)

		req, err := http.NewRequest("POST", Endpoint, &buffer)
		if err != nil {
			logger.Panicf("Making HTTP Request failed: %s", err.Error())
		}

		setupHeaders(req)
		resp, err := client.Do(req)
		if err != nil {
			logger.Panicf("Postmark failed: %s", err.Error())
		}

		defer resp.Body.Close()
		answer := util.ParseJSON(resp.Body, func(err error) {
			logger.Panicf("Something bad happened with Postmark: %s", err.Error())
		})

		if answer["ErrorCode"] != nil {
			code := int(answer["ErrorCode"].(float64))
			switch code {
			case 0:
				// All is well
			case 300:
				blacklist.Blacklist(j.Email)
				failFriendly("Your email appears invalid. Please try carefully remaking the bookmarklet.")
			default:
				logger.Panicf("Unknown error code from Postmark: %d, %s", code, answer)
			}
		}

		j.Progress("All done! Grab your Kindle and hang tight!")
		cleanup.Clean(j)
	})
}