<commit_before>package commands

import (
	"mime"
	"os"
	"path"
	"regexp"

	"github.com/codegangsta/cli"
	"github.com/mitsuse/bullet/pushbullet"
	"github.com/mitsuse/bullet/pushbullet/requests"
)

func NewSendCommand() cli.Command {
	command := cli.Command{
		Name:      "send",
		ShortName: "s",
		Usage:     "Send a message or a file",
		Action:    actionSend,

		Flags: []cli.Flag{
			configFlag(),

			cli.StringFlag{
				Name:  "title,t",
				Value: "",
				Usage: "The title of the message or file to be sent",
			},

			cli.StringFlag{
				Name:  "message,m",
				Value: "",
				Usage: "The message to be sent",
			},

			cli.StringFlag{
				Name:  "location,l",
				Value: "",
				Usage: "The path of file or link to be sent",
			},
		},
	}

	return command
}

func actionSend(ctx *cli.Context) {
	configPath := ctx.String("config")

	config, err := loadConfigPath(configPath)
	if err != nil {
		printError(err)
		return
	}

	pb := pushbullet.New(config.Token())

	title := ctx.String("title")
	message := ctx.String("message")
	location := ctx.String("location")

	if err := send(pb, title, message, location); err != nil {
		// TODO: Print an error message easy to understand.
		printError(err)
		return
	}
}

func send(pb *pushbullet.Pushbullet, title, message, location string) error {
	if len(location) == 0 {
		push := requests.NewNote()
		push.Title = title
		push.Body = message

		_, err := pb.PostPushesNote(push)
		return err
	}

	if isLink(location) {
		push := requests.NewLink()
		push.Title = title
		push.Body = message
		push.Url = location

		_, err := pb.PostPushesLink(push)
		return err
	}

	return upload(pb, title, message, location)
}

func upload(pb *pushbullet.Pushbullet, title, message, location string) error {
	fileName := path.Base(location)
	fileType := mime.TypeByExtension(path.Ext(fileName))

	res, err := pb.PostUploadRequest(fileName, fileType)
	if err != nil {
		return err
	}

	file, err := os.Open(location)
	if err != nil {
		return err
	}

	if err := pushbullet.Upload(res, file); err != nil {
		return err
	}

	push := requests.NewFile()
	push.Title = title
	push.Body = message
	push.FileName = res.FileName
	push.FileType = res.FileType
	push.FileUrl = res.FileUrl

	if _, err := pb.PostPushesFile(push); err != nil {
		return err
	}

	return nil
}

func isLink(location string) bool {
	return regexp.MustCompile(`^https?://`).MatchString(location)
}<commit_msg>Push to the device specified with its name.<commit_after>package commands

import (
	"errors"
	"mime"
	"os"
	"path"
	"regexp"

	"github.com/codegangsta/cli"
	"github.com/mitsuse/bullet/pushbullet"
	"github.com/mitsuse/bullet/pushbullet/requests"
)

func NewSendCommand() cli.Command {
	command := cli.Command{
		Name:      "send",
		ShortName: "s",
		Usage:     "Send a message or a file",
		Action:    actionSend,

		Flags: []cli.Flag{
			configFlag(),

			cli.StringFlag{
				Name:  "device,d",
				Value: "",
				Usage: "The name of target device",
			},

			cli.StringFlag{
				Name:  "title,t",
				Value: "",
				Usage: "The title of the message or file to be sent",
			},

			cli.StringFlag{
				Name:  "message,m",
				Value: "",
				Usage: "The message to be sent",
			},

			cli.StringFlag{
				Name:  "location,l",
				Value: "",
				Usage: "The path of file or link to be sent",
			},
		},
	}

	return command
}

func actionSend(ctx *cli.Context) {
	configPath := ctx.String("config")

	config, err := loadConfigPath(configPath)
	if err != nil {
		printError(err)
		return
	}

	pb := pushbullet.New(config.Token())

	title := ctx.String("title")
	message := ctx.String("message")
	location := ctx.String("location")
	device := ctx.String("device")

	deviceId, err := getDeviceId(pb, device)
	if err != nil {
		printError(err)
		return
	}

	if err := send(pb, deviceId, title, message, location); err != nil {
		// TODO: Print an error message easy to understand.
		printError(err)
		return
	}
}

func send(pb *pushbullet.Pushbullet, deviceId, title, message, location string) error {
	if len(location) == 0 {
		push := requests.NewNote()
		push.Title = title
		push.Body = message
		push.DeviceIden = deviceId

		_, err := pb.PostPushesNote(push)
		return err
	}

	if isLink(location) {
		push := requests.NewLink()
		push.Title = title
		push.Body = message
		push.Url = location
		push.DeviceIden = deviceId

		_, err := pb.PostPushesLink(push)
		return err
	}

	return upload(pb, deviceId, title, message, location)
}

func upload(pb *pushbullet.Pushbullet, deviceId, title, message, location string) error {
	fileName := path.Base(location)
	fileType := mime.TypeByExtension(path.Ext(fileName))

	res, err := pb.PostUploadRequest(fileName, fileType)
	if err != nil {
		return err
	}

	file, err := os.Open(location)
	if err != nil {
		return err
	}

	if err := pushbullet.Upload(res, file); err != nil {
		return err
	}

	push := requests.NewFile()
	push.Title = title
	push.Body = message
	push.FileName = res.FileName
	push.FileType = res.FileType
	push.FileUrl = res.FileUrl
	push.DeviceIden = deviceId

	if _, err := pb.PostPushesFile(push); err != nil {
		return err
	}

	return nil
}

func getDeviceId(pb *pushbullet.Pushbullet, name string) (string, error) {
	if len(name) > 0 {
		deviceRes, err := pb.GetDevices()
		if err != nil {
			return "", err
		}

		for _, device := range deviceRes.Devices {
			if device.Nickname == name {
				return device.Iden, nil
			}
		}
	} else {
		return "", nil
	}

	return "", errors.New("No such device.")
}

func isLink(location string) bool {
	return regexp.MustCompile(`^https?://`).MatchString(location)
}
{'fraction_non_alphanumeric': 0.1414756446991404, 'fraction_numerical': 0.0005372492836676218, 'mean_word_length': 3.3838304552590266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4909093', 'n_tokens_mistral': 2149, 'n_tokens_neox': 1936, 'n_words': 504}