예제 #1
0
func init() {
	gob.Register(UserModel{})

	// Load config file
	conf, err := yaml.Open("config.yml")
	if err != nil {
		log.Fatal(err)
	}

	// Retrieve credentials from config file
	username := to.String(conf.Get("database", "username"))
	password := to.String(conf.Get("database", "password"))
	name := to.String(conf.Get("database", "name"))

	// Open mysql connection
	dsn := fmt.Sprintf("%s:%s@/%s?%s", username, password, name, "parseTime=true")
	db, err = sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}

	// Sets the maximum number of connections in the idle connection pool
	db.SetMaxIdleConns(100)

}
예제 #2
0
파일: database.go 프로젝트: tjbx/X10Bridge
func (db *database) SaveDNS(dns DNS) error {
	config := to.String(dns.Configuration())
	state := to.String(dns.State())
	err := db.storage.Set(configurationKeyForDNSName(dns.Name()), []byte(config))
	if err != nil {
		return err
	}

	return db.storage.Set(stateKeyForDNSName(dns.Name()), []byte(state))
}
예제 #3
0
파일: main.go 프로젝트: tantra35/notifier
func configureSenders() error {
	sendersList, ok := config.GetInterface("notifier", "senders").([]interface{})
	if ok == false {
		return fmt.Errorf("Failed parse senders")
	}
	for _, senderSettingsI := range sendersList {
		senderSettings := make(map[string]string)
		for k, v := range senderSettingsI.(map[interface{}]interface{}) {
			senderSettings[to.String(k)] = to.String(v)
		}
		senderSettings["front_uri"] = config.Get("front", "uri")
		switch senderSettings["type"] {
		case "pushover":
			if err := notifier.RegisterSender(senderSettings, &pushover.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		case "slack":
			if err := notifier.RegisterSender(senderSettings, &slack.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		case "mail":
			if err := notifier.RegisterSender(senderSettings, &mail.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		case "script":
			if err := notifier.RegisterSender(senderSettings, &script.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		case "telegram":
			if err := notifier.RegisterSender(senderSettings, &telegram.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		case "twilio sms":
			if err := notifier.RegisterSender(senderSettings, &twilio.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		case "twilio voice":
			if err := notifier.RegisterSender(senderSettings, &twilio.Sender{}); err != nil {
				log.Fatalf("Can not register sender %s: %s", senderSettings["type"], err)
			}
		// case "email":
		// 	if err := notifier.RegisterSender(senderSettings, &kontur.MailSender{}); err != nil {
		// 	}
		// case "phone":
		// 	if err := notifier.RegisterSender(senderSettings, &kontur.SmsSender{}); err != nil {
		// 	}
		default:
			return fmt.Errorf("Unknown sender type [%s]", senderSettings["type"])
		}
	}
	return nil
}
예제 #4
0
파일: morph.go 프로젝트: kdar/morphgen
// options:
//  url - url to generate codes from
//  notmog - turn off grabbing transmogged items from armory
func (g *Generator) Generate(options map[string]interface{}, w io.Writer) error {
	url := to.String(options["url"])
	var tmorphItems TMorphItems
	var err error
	switch {
	case strings.Contains(url, "wowhead.com"):
		tmorphItems, err = wowhead(options)
	case strings.Contains(url, "battle.net/wow"):
		tmorphItems, err = wowarmory(options)
	case strings.Contains(url, "http"):
		tmorphItems, err = generic(options)
	default:
		return errors.New("Do not recognize the URL.")
	}

	if err != nil {
		return err
	}

	g.lastTmorphItems = tmorphItems
	bonus := int(to.Int64(options["bonus"]))
	g.Bonus(bonus)
	g.Output(w)

	return nil
}
예제 #5
0
파일: generic.go 프로젝트: kdar/morphgen
// Attempt to find any links in the page that we can
// parse and generate codes for.
func generic(options map[string]interface{}) (TMorphItems, error) {
	resp, err := http.Get(to.String(options["url"]))
	if err != nil {
		return nil, err
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	resp.Body.Close()

	matches := wowheadUrlRe.FindAllStringSubmatch(string(data), -1)
	var items []string
	for _, match := range matches {
		items = append(items, match[1])
	}

	matches = wowdbUrlRe.FindAllStringSubmatch(string(data), -1)
	for _, match := range matches {
		items = append(items, match[1])
	}

	//options["url"] = wowheadUrl + strings.Join(items, ";")
	//return wowhead(options)
	return wowapi(items)
}
예제 #6
0
func main() {
	// intialize routes muxer
	r := mux.NewRouter()

	// article routes
	r.HandleFunc("/articles", ArticleController{}.Index()).Methods("GET")
	r.HandleFunc("/articles", ArticleController{}.Create()).Methods("POST")
	r.HandleFunc("/articles/new", ArticleController{}.Form()).Methods("GET")
	r.HandleFunc("/articles/{id:[0-9]+}", ArticleController{}.Retrieve()).Methods("GET")
	r.HandleFunc("/articles/{id:[0-9]+}/publish", ArticleController{}.Publish()).Methods("POST")
	r.HandleFunc("/articles/{id:[0-9]+}/edit", ArticleController{}.Edit()).Methods("GET")
	r.HandleFunc("/articles/{id:[0-9]+}/delete", ArticleController{}.Delete()).Methods("POST")

	// interview routes
	r.HandleFunc("/interviews", InterviewController{}.Index()).Methods("GET")
	r.HandleFunc("/interviews/{id:[0-9]+}", InterviewController{}.Retrieve()).Methods("GET")
	r.HandleFunc("/interviews/new", InterviewController{}.Form()).Methods("GET")
	r.HandleFunc("/interviews/new", InterviewController{}.Create()).Methods("POST")

	// account routes
	r.HandleFunc("/accounts", UserController{}.Index())
	r.HandleFunc("/accounts/{id:[0-9]+}", UserController{}.Retrieve())

	// static page routes
	r.HandleFunc("/about", MainController{}.About())
	r.HandleFunc("/terms", MainController{}.Terms())
	r.HandleFunc("/privacy", MainController{}.Privacy())
	r.HandleFunc("/", MainController{}.Landing())

	// user routes
	r.HandleFunc("/signin", UserController{}.SignInForm()).Methods("GET")
	r.HandleFunc("/signin", UserController{}.SignInApi()).Methods("POST")
	r.HandleFunc("/signout", UserController{}.SignOut()).Methods("GET")
	r.HandleFunc("/signup", UserController{}.SignUpForm()).Methods("GET")
	r.HandleFunc("/signup", UserController{}.SignUpApi()).Methods("POST")

	// api routes
	r.HandleFunc("/api/charts/groupwork", Chart{}.GroupWork())
	r.HandleFunc("/api/charts/fulfillment", Chart{}.Fulfillment())
	r.HandleFunc("/api/charts/breakdown", Chart{}.Breakdown())

	// static resource files
	http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
	http.Handle("/vendor/", http.StripPrefix("/vendor/", http.FileServer(http.Dir("vendor"))))

	// register gorrilla router as root
	http.Handle("/", r)

	// Load config file
	conf, err := yaml.Open("config.yml")
	if err != nil {
		log.Fatal(err)
	}

	port := to.String(conf.Get("server", "port"))
	if err = http.ListenAndServe(":"+port, nil); err != nil {
		log.Fatal(err)
	}
}
예제 #7
0
파일: image.go 프로젝트: astrata/tango-doc
func (self *Image) Resize(size string) {
	var err error

	url := to.String(self.Params.Get("url"))

	relPath := ImageRoot + tango.PS + size + tango.PS + checksum.String(fmt.Sprintf("%s/%s", size, url), crypto.SHA1) + ".png"
	fullPath := Root + tango.PS + relPath

	_, err = os.Stat(fullPath)

	if err == nil {

		app.Server.Context.Redirect("/" + relPath)

		app.Server.Context.HttpError(200)

		return

	} else {

		filePath, err := resource.Download(url)

		if err == nil {

			thumb := canvas.New()

			opened := thumb.Open(filePath)

			if opened == true {

				resize := strings.Split(size, "x")

				width, _ := strconv.Atoi(resize[0])
				height, _ := strconv.Atoi(resize[1])

				thumb.AutoOrientate()
				thumb.Thumbnail(uint(width), uint(height))

				os.MkdirAll(path.Dir(fullPath), os.ModeDir|0755)

				written := thumb.Write(fullPath)

				if written {
					app.Server.Context.Redirect("/" + relPath)
				} else {
					app.Server.Context.HttpError(500)
				}

				return
			}

		}

	}

	app.Server.Context.HttpError(404)
}
예제 #8
0
func TestToken(t *testing.T) {
	yf, err := yaml.Open("settings.yaml")

	client = New()

	if err == nil {
		client.SetAccessToken(to.String(yf.Get("providers", "instagram", "access_token")))
	}
}
예제 #9
0
파일: server.go 프로젝트: astrata/tango
// Starts a fastcgi/http server.
func (server *Server) Run() error {

	var err error

	server.serveMux.Handle("/", server)

	domain := "unix"
	addr := to.String(config.Get("server/socket"))

	if addr == "" {
		domain = "tcp"
		addr = fmt.Sprintf("%s:%d", to.String(config.Get("server/bind")), to.Int(config.Get("server/port")))
	}

	server.listener, err = net.Listen(domain, addr)

	if err != nil {
		log.Fatalf("Failed to bind on %s: %s", addr, err.Error())
	}

	defer server.listener.Close()

	log.Printf("%s is ready to dance.\n", server.listener.Addr())
	log.Printf("Stop server with ^C.\n")

	fmt.Fprintf(os.Stderr, "\n")

	switch to.String(config.Get("server/type")) {
	case "fastcgi":
		if err == nil {
			fcgi.Serve(server.listener, server.serveMux)
		} else {
			log.Fatalf("Failed to start FastCGI server.")
		}
	default:
		if err == nil {
			http.Serve(server.listener, server.serveMux)
		} else {
			log.Fatalf("Failed to start HTTP server.")
		}
	}

	return nil
}
예제 #10
0
파일: weibo.go 프로젝트: hiproz/weigo
func encodeParams(params map[string]interface{}) (string, error) {
	if len(params) > 0 {
		values := url.Values{}
		for key, value := range params {
			values.Add(key, to.String(value))
		}
		return values.Encode(), nil
	}
	return "", errors.New("Params Is Empty!")
}
예제 #11
0
func init() {
	// Load config file
	conf, err := yaml.Open("config.yml")
	if err != nil {
		log.Fatal(err)
	}

	// Retrieve credentials from config file
	username := to.String(conf.Get("database", "username"))
	password := to.String(conf.Get("database", "password"))
	name := to.String(conf.Get("database", "name"))

	// Open mysql connection
	db, err = sql.Open("mysql", username+":"+password+"@/"+name)
	if err != nil {
		log.Fatal(err)
	}

	// Sets the maximum number of connections in the idle connection pool
	db.SetMaxIdleConns(100)
}
예제 #12
0
파일: collection.go 프로젝트: kdar/db
// Returns a mgo.Query based on the given terms.
func (self *SourceCollection) BuildQuery(terms ...interface{}) *mgo.Query {

	var sort interface{}

	limit := -1
	offset := -1
	sort = nil

	// Conditions
	query := self.compileQuery(terms)

	itop := len(terms)
	for i := 0; i < itop; i++ {
		term := terms[i]

		switch term.(type) {
		case db.Limit:
			limit = int(term.(db.Limit))
		case db.Offset:
			offset = int(term.(db.Offset))
		case db.Sort:
			sort = term.(db.Sort)
		}
	}

	// Actually executing query, returning a pointer.
	q := self.collection.Find(query)

	// Applying limits and offsets.
	if offset > -1 {
		q = q.Skip(offset)
	}

	if limit > -1 {
		q = q.Limit(limit)
	}

	// Sorting result
	if sort != nil {
		for key, val := range sort.(db.Sort) {
			sval := to.String(val)
			if sval == "-1" || sval == "DESC" {
				q = q.Sort("-" + key)
			} else if sval == "1" || sval == "ASC" {
				q = q.Sort(key)
			} else {
				panic(fmt.Sprintf(`Unknown sort value "%s".`, sval))
			}
		}
	}

	return q
}
예제 #13
0
파일: wowhead.go 프로젝트: kdar/morphgen
func wowhead(options map[string]interface{}) (TMorphItems, error) {
	url := to.String(options["url"])

	// if they just put a wowhead item url in, just output that item
	if matches := wowheadUrlRe.FindStringSubmatch(url); len(matches) > 0 {
		items, err := wowapi([]string{matches[1]})
		if err != nil {
			return nil, errors.New(merry.Details(err))
		}

		if len(items) > 0 {
			return items, nil
		}
	}

	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	resp.Body.Close()

	if matches := itemIdRe.FindStringSubmatch(string(data)); len(matches) == 3 {
		//return nil, errors.New(fmt.Sprintf("%#+v", matches))
		count, _ := strconv.Atoi(matches[2])
		if count == 1 {
			items, err := wowapi([]string{matches[1]})
			if err != nil {
				return nil, errors.New(merry.Details(err))
			}

			if len(items) > 0 {
				return items, nil
			}
		} else if count > 1 {
			itemids := strings.Split(matches[1][1:len(matches[1])-1], ":")
			items, err := wowapi(itemids)
			if err != nil {
				return nil, errors.New(merry.Details(err))
			}

			if len(items) > 0 {
				return items, nil
			}
		}
	}

	return nil, errors.New(`Could not find anything to morph on that wowhead page.`)
}
예제 #14
0
파일: main.go 프로젝트: warmfusion/cache
func readConfig(configFileName *string) error {
	file, err := yaml.Open(*configFileName)
	if err != nil {
		return fmt.Errorf("Can't read config file %s: %s", *configFileName, err.Error())
	}
	pidFileName = to.String(file.Get("cache", "pid"))
	logFileName = to.String(file.Get("cache", "log_file"))
	listen = to.String(file.Get("cache", "listen"))
	retentionConfigFileName = to.String(file.Get("cache", "retention-config"))
	redisURI = fmt.Sprintf("%s:%s", to.String(file.Get("redis", "host")), to.String(file.Get("redis", "port")))
	graphiteURI = to.String(file.Get("graphite", "uri"))
	graphitePrefix = to.String(file.Get("graphite", "prefix"))
	graphiteInterval = to.Int64(file.Get("graphite", "interval"))
	return nil
}
예제 #15
0
func (s Dostow) FilterGetAll(filter map[string]interface{}, count int, skip int, store string, opts gostore.ObjectStoreOptions) (rrows *DostowRows, err error) {
	params := map[string]string{}
	for k, v := range filter {
		params[k] = to.String(v)
	}
	if count > -1 {
		params["size"] = strconv.Itoa(count)
	}
	// resp, bodyBytes, errs := goreq.New().Get(s.url+"/store/"+store).
	// 	SetHeader("X-DOSTOW-GROUP-ACCESS-KEY", s.key).
	// 	Query(filter).
	// 	SetClient(s.client).
	// 	EndBytes()

	resp, err := s.get(s.url+"/store/"+store, params)
	// if resp != nil && resp.Body != nil {
	// 	defer resp.Body.Close()
	// }
	if err != nil {
		logger.Error("Filter GetAll Error:", "err", err)
		return nil, handleError(err)
	}
	defer resp.Body.Close()
	var bodyBytes []byte
	var dst map[string]interface{}
	switch resp.StatusCode {
	case 200:
		bodyBytes, err = getBytes(resp)
		if err != nil {
			return
		}
		if err := json.Unmarshal(bodyBytes, &dst); err != nil {
			return nil, err
		}
		return &DostowRows{dst, bodyBytes}, err
	case 500, 400, 401:
		bodyBytes, err = getBytes(resp)
		if err != nil {
			return
		}
		if err := json.Unmarshal(bodyBytes, &dst); err != nil {
			return nil, newServerError(resp.StatusCode, "Unable to perform action due to server error")
		}
		return nil, newServerError(resp.StatusCode, dst["msg"].(string))
	default:
		return nil, errors.New("Cannot perfrom action")
	}
	return nil, errors.New("Not found")
}
예제 #16
0
func init() {
	// Load config file
	conf, err := yaml.Open("config.yml")
	if err != nil {
		log.Fatal(err)
	}

	// compile templates
	t = template.Must(t.ParseGlob("views/_templates/*.html"))
	t = template.Must(t.ParseGlob("views/users/*.html"))
	t = template.Must(t.ParseGlob("views/songs/*.html"))
	t = template.Must(t.ParseGlob("views/auth/*.html"))
	t = template.Must(t.ParseGlob("views/*.html"))

	// initialize session storage
	store = sessions.NewCookieStore([]byte(to.String(conf.Get("server", "secret"))))

	gob.Register(&models.UserModel{})

	// load variables from config
	access := to.String(conf.Get("amazon", "s3", "access"))
	secret := to.String(conf.Get("amazon", "s3", "secret"))
	name := to.String(conf.Get("amazon", "s3", "name"))

	// configure aws authentication
	auth, err := aws.GetAuth(access, secret)
	if err != nil {
		log.Fatal(err)
	}

	// create s3 client
	client := s3.New(auth, aws.USWest2)

	// retrieve bucket from name
	bucket = client.Bucket(name)
}
예제 #17
0
파일: util.go 프로젝트: kdar/morphgen
func Map(val interface{}) map[string]interface{} {
	list := map[string]interface{}{}
	if val == nil {
		return list
	}
	switch reflect.TypeOf(val).Kind() {
	case reflect.Map:
		vval := reflect.ValueOf(val)
		vlist := reflect.ValueOf(list)
		for _, vkey := range vval.MapKeys() {
			key := to.String(vkey.Interface())
			vlist.SetMapIndex(reflect.ValueOf(key), vval.MapIndex(vkey))
		}
		return list
	}
	return list
}
예제 #18
0
파일: morph.go 프로젝트: kdar/morphgen
func (t TMorphItem) String() string {
	var args []string
	for _, i := range t.Args {
		args = append(args, to.String(i))
	}

	if t.Type == "item" {
		switch t.Bonus {
		case raidNormal:
			args = append(args, "0")
		case raidHeroic:
			args = append(args, "1")
		case raidMythic:
			args = append(args, "3")
		}
	}

	return fmt.Sprintf(".%s %s", t.Type, strings.Join(args, " "))
}
예제 #19
0
func init() {
	// parse templates
	t = template.Must(t.ParseGlob("views/_templates/*.html"))
	t = template.Must(t.ParseGlob("views/articles/*.html"))
	t = template.Must(t.ParseGlob("views/interviews/*.html"))
	t = template.Must(t.ParseGlob("views/interviews/components/*.html"))
	t = template.Must(t.ParseGlob("views/accounts/*.html"))
	t = template.Must(t.ParseGlob("views/users/*.html"))
	t = template.Must(t.ParseGlob("views/*.html"))

	// Load config file
	conf, err := yaml.Open("config.yml")
	if err != nil {
		log.Fatal(err)
	}

	// grab server secret from config file
	secret := to.String(conf.Get("server", "secret"))

	// initialize session storage
	store = sessions.NewCookieStore([]byte(secret))
}
예제 #20
0
파일: weibo.go 프로젝트: hiproz/weigo
func encodeMultipart(params map[string]interface{}) (multipartContentType string, multipartData *bytes.Buffer, err error) {
	if len(params) > 0 {
		multipartData := new(bytes.Buffer)
		bufferWriter := multipart.NewWriter(multipartData) // type *bytes.Buffer
		defer bufferWriter.Close()
		var multipartContentType string
		for key, value := range params {
			switch value.(type) {
			case *os.File:
				picdata, err := bufferWriter.CreateFormFile(key, value.(*os.File).Name())
				if err != nil {
					return "", nil, err
				}
				multipartContentType = bufferWriter.FormDataContentType()
				io.Copy(picdata, value.(*os.File))
			default:
				bufferWriter.WriteField(key, to.String(value))
			}
		}
		return multipartContentType, multipartData, nil
	}
	return "", nil, errors.New("Params Is Empty!")
}
예제 #21
0
func main() {

	// load settings from config file
	conf, err := yaml.Open("config.yml")
	if err != nil {
		log.Fatal(err)
	}

	// initialize router
	router := InitializeRouter()

	// register router
	http.Handle("/", router)

	// get port number from config file
	port := to.String(conf.Get("server", "port"))

	fmt.Printf("Serving application from port %s\n", port)

	// serve application
	if err = http.ListenAndServe(":"+port, nil); err != nil {
		log.Fatal(err)
	}
}
예제 #22
0
파일: sweet.go 프로젝트: mishudark/sweet
//json decoder helper
func Json_decode(bytes []byte) (ListTuple, error) {
	var result []sugar.Tuple

	err := json.Unmarshal(bytes, &result)
	s := to.String(err)

	//FIXME: there is another way to make this?
	if strings.Contains(s, "json: cannot unmarshal object into Go") {
		var result_simple sugar.Tuple
		err = json.Unmarshal(bytes, &result_simple)

		if err == nil {
			//convert siple to array
			array := []sugar.Tuple{result_simple}
			return array, err
		}
	}

	if err != nil {
		fmt.Println("error:", err)
		return result, err
	}
	return result, err
}
예제 #23
0
파일: update.go 프로젝트: kdar/morphgen
func CheckUpdate() (string, error) {
	resp, err := http.Get("https://api.github.com/repos/kdar/morphgen/tags")
	if err != nil {
		return "", err
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	resp.Body.Close()

	var datam []map[string]interface{}
	err = json.Unmarshal(data, &datam)
	if err != nil {
		return "", err
	}

	name := to.String(datam[0]["name"])
	if len(name) > 0 && name[0] == 'v' {
		name = name[1:]
	}

	if len(datam) > 0 {
		v, err := version.Parse(name)
		if err != nil {
			return "", err
		}

		if VERSION.Less(v) {
			return "Update available: " + v.String(), nil
		}
	}

	return "", nil
}
예제 #24
0
func TestApi(t *testing.T) {

	var err error

	client := New(&oauth.Credentials{
		to.String(conf.Get("twitter/app/key")),
		to.String(conf.Get("twitter/app/secret")),
	})

	client.SetAuth(&oauth.Credentials{
		to.String(conf.Get("twitter/user/token")),
		to.String(conf.Get("twitter/user/secret")),
	})

	_, err = client.VerifyCredentials(nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	_, err = client.HomeTimeline(nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	_, err = client.MentionsTimeline(nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	_, err = client.UserTimeline(nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	_, err = client.RetweetsOfMe(nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	_, err = client.Retweets(int64(21947795900469248), nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	var status *sugar.Map
	status, err = client.Update(fmt.Sprintf("Test message @ %s", time.Now()), nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	tweetId := to.Int64(status.Get("id_str"))

	_, err = client.Destroy(tweetId, nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	tweetId = to.Int64(status.Get("id_str"))

	files := []string{
		"_resources/test.jpg",
	}

	_, err = client.UpdateWithMedia("Hello", nil, files)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

	_, err = client.Retweet(int64(21947795900469248), nil)

	if err != nil {
		t.Errorf("Test failed: %s\n", err.Error())
	}

}
예제 #25
0
파일: battlenet.go 프로젝트: kdar/morphgen
// Get detailed codes from a character's armory page.
func wowarmory(options map[string]interface{}) (TMorphItems, error) {
	u, err := neturl.Parse(to.String(options["url"]))
	if err != nil {
		return nil, merry.Wrap(err)
	}

	parts := strings.Split(u.Path, "/")
	loc := -1
	for x := 0; x < len(parts); x++ {
		if parts[x] == "character" {
			loc = x + 1
			break
		}
	}

	if loc == -1 {
		return nil, errors.New("Could not parse battle.net URL")
	}

	// FIXME: this isn't exactly correct, because you can be in the US and want
	// to get the tmorph codes of a person in EU/China. So we need to probably
	// have settings to where the user of TMorphGen is.
	hostParts := strings.Split(u.Host, ".")
	if hostParts[0] == "cn" {
	} else if len(hostParts) == 2 {
		u.Host = "us.api." + strings.Join(hostParts, ".")
	} else {
		u.Host = hostParts[0] + ".api." + strings.Join(hostParts[1:], ".")
	}

	u.Scheme = "https"
	u.Path = fmt.Sprintf("/wow/character/%s/%s", parts[loc], parts[loc+1])
	u.RawQuery = "fields=items,appearance&locale=en_US"

	resp, err := apicall(u)
	if err != nil {
		return nil, merry.Wrap(err)
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, merry.Wrap(err)
	}
	resp.Body.Close()

	var tmorphItems TMorphItems
	var datam map[string]interface{}

	err = json.Unmarshal(data, &datam)
	if err != nil {
		return nil, merry.Wrap(err)
	}

	// get all armor, weapons, and enchants
	items := Map(datam["items"])
	for k, v := range items {
		if v, ok := v.(map[string]interface{}); ok {
			if canDisplayName(k) {
				id := to.Int64(v["id"])
				tooltipParams := Map(v["tooltipParams"])
				if !to.Bool(options["notmog"]) {
					transmogItem := tooltipParams["transmogItem"]
					if transmogItem != nil {
						id = to.Int64(transmogItem)
					}
				}

				tmorphItems = append(tmorphItems, &TMorphItem{
					Type: "item",
					Args: []int{nameSlotMap[k], int(id)},
				})

				// get enchants off the weapons
				if k == "mainHand" || k == "offHand" {
					if tooltipParams["enchant"] != nil {
						which := 1
						if k == "offHand" {
							which = 2
						}

						tmorphItems = append(tmorphItems, &TMorphItem{
							Type: "enchant",
							Args: []int{which, int(to.Int64(tooltipParams["enchant"]))},
						})
					}
				}
			}
		}
	}

	// set offhand to 0 if there is none.
	// TODO: maybe there should be defaults?
	if items["offHand"] == nil {
		tmorphItems = append(tmorphItems, &TMorphItem{
			Type: "item",
			Args: []int{nameSlotMap["offHand"], 0},
		})
	}

	// appearance stuff
	appearance := Map(datam["appearance"])
	for k, v := range appearance {
		if typ, ok := appearanceMap[k]; ok {
			tmorphItems = append(tmorphItems, &TMorphItem{
				Type: typ,
				Args: []int{int(to.Int64(v))},
			})
		}
	}
	tmorphItems = append(tmorphItems, &TMorphItem{
		Type: "race",
		Args: []int{int(to.Int64(datam["race"]))},
	}, &TMorphItem{
		Type: "gender",
		Args: []int{int(to.Int64(datam["gender"]))},
	})

	return tmorphItems, nil
}
예제 #26
0
파일: main.go 프로젝트: Olbitla/photopy
func startImport(name string, dest string) {

	defer func() {
		ok <- 1
	}()

	tags, err := getExifData(name)

	if err == nil {

		hash := checksum.File(name, crypto.SHA1)
		rename := ""

		switch tags["File Type"] {

		case "MP3":

			rename = strings.Join(
				[]string{
					dest,
					normalize(pick(tags["Artist"], "Unknown Artist")),
					normalize(pick(tags["Album"], "Unknown Album")),
					fmt.Sprintf("%s%s", normalize(tags["Track"], fmt.Sprintf("%s-%s", pick(tags["Title"], "Unknown Title"), hash[0:4])), pick(strings.ToLower(path.Ext(name)), ".mp3")),
				},
				Ps,
			)

		default:
			var taken string

			dateTimeFields := []string{
				"Date and Time (Original)",
				"Date/Time Original",
				"Media Create Date",
				"Track Create Date",
				"Create Date",
			}

			for _, field := range dateTimeFields {
				if tags[field] != "" {
					taken = tags[field]
					break
				}
			}

			if taken == "" {
				stats.unknown++
				return
			}

			all := reDateTime.FindAllStringSubmatch(taken, -1)

			timeTaken := time.Date(
				int(to.Int64(all[0][1])),
				time.Month(int(to.Int64(all[0][2]))),
				int(to.Int64(all[0][3])),
				int(to.Int64(all[0][4])),
				int(to.Int64(all[0][5])),
				int(to.Int64(all[0][6])),
				0,
				time.UTC,
			)

			rename = strings.Join(
				[]string{
					dest,
					to.String(timeTaken.Year()),
					fmt.Sprintf("%02d-%s", timeTaken.Month(), timeTaken.Month()),
					fmt.Sprintf("%02d-%s", timeTaken.Day(), timeTaken.Weekday()),
					fmt.Sprintf("%02d%02d%02d-%s%s", timeTaken.Hour(), timeTaken.Minute(), timeTaken.Second(), strings.ToUpper(hash[0:4]), strings.ToLower(path.Ext(name))),
				},
				Ps,
			)
		}

		if rename != "" {

			_, err := os.Stat(rename)

			if err != nil {

				if *flagDryRun == false {
					err = os.MkdirAll(path.Dir(rename), os.ModeDir|0750)
					if err != nil {
						panic(err)
					}
				}
				err = nil
				if *flagMove == true {
					log.Printf("Moving file: %s -> %s\n", name, rename)
					if *flagDryRun == false {
						err = fileMove(name, rename)
						stats.moved++
					}
				} else {
					log.Printf("Copying file: %s -> %s\n", name, rename)
					if *flagDryRun == false {
						err = copyFile(name, rename)
						stats.copied++
					}
				}
				if err != nil {
					panic(err)
				}

			} else {
				rehash := checksum.File(rename, crypto.SHA1)

				if hash == rehash {
					log.Printf("Destination already exists: %s, removing original: %s (same file).\n", rename, name)
					os.Remove(name)
					stats.deleted++
				} else {
					log.Printf("Destination already exists: %s, skipping original: %s (files differ).\n", rename, name)
					stats.skipped++
				}
			}

		} else {
			stats.unknown++
		}

	} else {
		stats.unknown++
	}

}
예제 #27
0
파일: bot.go 프로젝트: harperreed/horace
func main() {

	// Parse flags from command line
	flag.Parse()
	log := logging.InitFromFlags()

	// setup logging
	log.SetLogLevel(2)

	if _, err := os.Stat(*config_file); err != nil {

		generate_config_file(*config_file)
		log.Error("You must edit the " + *config_file + " file before continuing")
		os.Exit(0)
	}

	//generate a config file if it isn't found
	if *generate_config {
		generate_config_file(*config_file)
		log.Error("You must edit the " + *config_file + " file before continuing")
		os.Exit(0)
	}

	// handle configuration

	log.Info("Read configuration file: " + *config_file)
	settings := yaml.New()
	settings.Read(*config_file)

	if *channel == "" {
		*channel = settings.Get("connection/channel").(string)
		log.Debug("Read channel from config file: " + *channel)
	} else {
		log.Debug("Read channel from flag: " + *channel)
	}

	if *nick == "" {
		*nick = settings.Get("connection/nick").(string)
		log.Debug("Read nick from config file: " + *nick)
	} else {
		log.Debug("Read nick from flag: " + *nick)
	}

	if *realname == "" {
		*realname = settings.Get("connection/realname").(string)
		log.Debug("Read realname from config file: " + *realname)
	} else {
		log.Debug("Read realname from flag: " + *realname)
	}

	if *irc_server == "" {
		*irc_server = settings.Get("connection/irc_server").(string)
		log.Debug("Read irc_server from config file: " + *irc_server)
	} else {
		log.Debug("Read irc_server from flag: " + *irc_server)
	}

	if *rejoin_on_kick == true {
		*rejoin_on_kick = settings.Get("bot_config/rejoin_on_kick").(bool)
		log.Debug("Read rejoin_on_kick from config file: %t ", *rejoin_on_kick)
	} else {
		log.Debug("Read rejoin_on_kick from flag: %t ", *rejoin_on_kick)
	}

	// bitly

	shorturl_enabled := settings.Get("bitly/shorturls_enabled").(bool)
	bitly_username := settings.Get("bitly/username").(string)
	bitly_api_key := settings.Get("bitly/api_key").(string)
	if shorturl_enabled {
		bitly.SetUser(bitly_username)
		bitly.SetKey(bitly_api_key)
	}

	owner_nick := to.String(settings.Get("bot_config/owner"))
	friends := to.List(settings.Get("bot_config/friends"))
	trusted_identities := make([]string, 0)
	trusted_identities = append(trusted_identities, owner_nick)
	for _, value := range friends {
		trusted_identities = append(trusted_identities, value.(string))
	}

	// set up bot command event registry
	bot_command_registry := event.NewRegistry()
	reallyquit := false

	// Bot command handlers
	// addfriend
	addfriend_state := make(chan string)
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if line.Src == owner_nick {
			channel := line.Args[0]
			if len(commands) > 1 {
				target := commands[1]
				log.Debug("adding friend: %q", target)
				conn.Whois(target)
				log.Debug("triggering channel: %q", target)
				addfriend_state <- target

			} else {
				conn.Privmsg(channel, line.Nick+": use !addfriend <friend nick>")
			}
		}
	}), "addfriend")

	//save
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if line.Src == owner_nick {
			channel := line.Args[0]
			conn.Privmsg(channel, line.Nick+": saving settings")
			settings.Set("bot_config/friends", friends)
			settings.Write(*config_file)
			log.Info("%q", to.List(settings.Get("bot_config/friends")))
		}
	}), "save")

	//reload
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if line.Src == owner_nick {
			channel := line.Args[0]
			conn.Privmsg(channel, line.Nick+": reloading settings")
			friends := to.List(settings.Get("bot_config/friends"))
			trusted_identities = make([]string, 0)
			trusted_identities = append(trusted_identities, owner_nick)
			for _, value := range friends {
				trusted_identities = append(trusted_identities, value.(string))
			}
			log.Info("%q", to.List(settings.Get("bot_config/friends")))
		}
	}), "reload")

	// op
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if Trust(line.Src, trusted_identities) {
			channel := line.Args[0]
			if len(commands) > 1 {
				target := commands[1]
				log.Info("Oping user: "******"+o "+target)
			} else {
				log.Info("Oping user: "******"+o "+line.Nick)
			}
		}
	}), "op")

	//deop
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if Trust(line.Src, trusted_identities) {
			channel := line.Args[0]
			if len(commands) > 1 {
				target := commands[1]
				conn.Mode(channel, "-o "+target)
			} else {
				conn.Mode(channel, "-o "+line.Nick)
			}
		}
	}), "deop")

	// kick
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if Trust(line.Src, trusted_identities) {
			channel := line.Args[0]
			if len(commands) > 1 {
				target := commands[1]
				kick_message := "get out"
				if len(commands) > 2 {
					//this doesn't work. Need to fix.
					kick_message = commands[2]
				}
				//do'nt kick if owner
				if target != strings.Split(owner_nick, "!")[0] {
					//don't kick if self
					if *nick != target {
						conn.Kick(channel, target, kick_message)
					} else {
						conn.Privmsg(channel, line.Nick+": why would i kick myself?")
					}
				} else {
					conn.Privmsg(channel, line.Nick+": why would i kick my lovely friend "+target+"?")

				}
			} else {
				conn.Privmsg(channel, line.Nick+": invalid command")
			}
		}
	}), "kick")

	//quit
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		if line.Src == owner_nick {
			quit_message := "i died"
			reallyquit = true
			if len(commands) > 1 {
				quit_message = commands[1]
			}
			conn.Quit(quit_message)
		}
	}), "quit")

	//urlshortener
	bot_command_registry.AddHandler(NewHandler(func(conn *irc.Conn, line *irc.Line, commands []string) {
		log.Info("URLS event")
		channel := line.Args[0]
		for _, long_url := range commands {
			work_url := long_url
			work_urlr := regexp.MustCompile(`^[\w-]+://([^/?]+)(/(?:[^/?]+/)*)?([^./?][^/?]+?)?(\.[^.?]*)?(\?.*)?$`)
			url_parts := work_urlr.FindAllStringSubmatch(work_url, -1)
			domain := url_parts[0][1]
			ext := url_parts[0][4]

			forbidden_extensions := "png|gif|jpg|mp3|avi|md|zip"
			extension_regex := regexp.MustCompile(`(` + forbidden_extensions + `)`)
			extension_test := extension_regex.FindAllStringSubmatch(ext, -1)

			title := ""
			url_util_channel := make(chan string)
			if extension_test == nil {
				go grab_title(work_url, url_util_channel)
				title = <-url_util_channel
			}

			go shorten_url(work_url, url_util_channel, bitly_username, bitly_api_key)
			short_url := <-url_util_channel
			output := ""
			if short_url != long_url {

				output = output + "<" + short_url + "> (at " + domain + ") "
			}
			if title != "" {
				output = output + " " + title
			}
			conn.Privmsg(channel, output)
		}
	}), "urlshortener")

	// create new IRC connection
	log.Info("create new IRC connection")
	irc_client := irc.SimpleClient(*nick, *realname)

	// IRC HANDLERS!

	irc_client.EnableStateTracking()
	irc_client.AddHandler("connected",
		func(conn *irc.Conn, line *irc.Line) {
			log.Info("connected as " + *nick)
			conn.Join(*channel)
		})

	// Set up a handler to notify of disconnect events.
	quit := make(chan bool)
	irc_client.AddHandler("disconnected",
		func(conn *irc.Conn, line *irc.Line) {
			log.Info("disconnected")
			quit <- true
		})

	//Handle Private messages
	irc_client.AddHandler("PRIVMSG",
		func(conn *irc.Conn, line *irc.Line) {
			log.Info("privmsg")
			irc_input := strings.ToLower(line.Args[1])
			if strings.HasPrefix(irc_input, *command_char) {
				irc_command := strings.Split(irc_input[1:], " ")
				bot_command_registry.Dispatch(irc_command[0], conn, line, irc_command)
			}
			url_regex := regexp.MustCompile(`\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))`)
			urls := url_regex.FindAllString(irc_input, -1)
			if len(urls) > 0 {
				bot_command_registry.Dispatch("urlshortener", conn, line, urls)
			}

		})

	//handle kick by rejoining kicked channel
	irc_client.AddHandler("KICK",
		func(conn *irc.Conn, line *irc.Line) {
			log.Info("Kicked from " + line.Args[0])
			if *rejoin_on_kick {
				log.Info("rejoining " + line.Args[0])
				conn.Join(line.Args[0])
			}
		})

	//notify on 332 - topic reply on join to channel
	irc_client.AddHandler("332",
		func(conn *irc.Conn, line *irc.Line) {
			log.Debug("Topic is %q, on %q ", line.Args[2], line.Args[1])
		})

	//notify on MODE
	irc_client.AddHandler("MODE",
		func(conn *irc.Conn, line *irc.Line) {
			for _, v := range line.Args {
				log.Info("mode: %q ", v)
			}
		})

	//notify on WHOIS
	irc_client.AddHandler("311",
		func(conn *irc.Conn, line *irc.Line) {
			addedfriend := <-addfriend_state
			log.Info("addfriend channel: %q", addedfriend)
			if addedfriend == line.Args[1] {
				friend := line.Args[1] + "!" + line.Args[2] + "@" + line.Args[3]
				log.Debug("added friend " + friend)
				trusted_identities = append(trusted_identities, friend)
				friends = append(friends, friend)
				log.Debug("friends: %q", friends)
				conn.Privmsg(strings.Split(owner_nick, "!")[0], line.Nick+": added "+line.Args[1]+" as friend")
				//addfriend_state <- ""
			} else {
				log.Info("addfriend channel is empty: %q", addedfriend)

			}
		})

	//notify on join
	irc_client.AddHandler("JOIN",
		func(conn *irc.Conn, line *irc.Line) {
			log.Info("Joined " + line.Args[0])
		})

	//handle topic changes
	irc_client.AddHandler("TOPIC",
		func(conn *irc.Conn, line *irc.Line) {
			log.Info("Topic on " + line.Args[0] + " changed to: " + line.Args[1])
		})

	// set up a goroutine to read commands from stdin

	if *generate_config == false {

		for !reallyquit {
			// connect to server
			if err := irc_client.Connect(*irc_server); err != nil {
				fmt.Printf("Connection error: %s\n", err)
				return
			}

			// wait on quit channel
			<-quit
		}
	}
}
예제 #28
0
파일: sqlite.go 프로젝트: seacoastboy/db
// Returns all the rows in the table that match certain conditions.
func (t *SqliteTable) FindAll(terms ...interface{}) []db.Item {
	var itop int

	var relate interface{}
	var relateAll interface{}

	fields := "*"
	conditions := ""
	limit := ""
	offset := ""
	sort := ""

	// Analyzing
	itop = len(terms)

	for i := 0; i < itop; i++ {
		term := terms[i]

		switch term.(type) {
		case db.Limit:
			limit = fmt.Sprintf("LIMIT %v", term.(db.Limit))
		case db.Sort:
			sortBy := []string{}
			for k, v := range term.(db.Sort) {
				v = strings.ToUpper(to.String(v))
				if v == "-1" {
					v = "DESC"
				}
				if v == "1" {
					v = "ASC"
				}
				sortBy = append(sortBy, fmt.Sprintf("%s %s", k, v))
			}
			sort = fmt.Sprintf("ORDER BY %s", strings.Join(sortBy, ", "))
		case db.Offset:
			offset = fmt.Sprintf("OFFSET %v", term.(db.Offset))
		case db.Fields:
			fields = strings.Join(term.(db.Fields), ", ")
		case db.Relate:
			relate = term.(db.Relate)
		case db.RelateAll:
			relateAll = term.(db.RelateAll)
		}
	}

	conditions, args := t.compileConditions(terms)

	if conditions == "" {
		conditions = "1 = 1"
	}

	rows, _ := t.parent.slExec(
		"Query",
		fmt.Sprintf("SELECT %s FROM %s", fields, slTable(t.name)),
		fmt.Sprintf("WHERE %s", conditions), args,
		sort, limit, offset,
	)

	result := t.slFetchAll(rows)

	var relations []sugar.Tuple
	var rcollection db.Collection

	// This query is related to other collections.
	if relate != nil {
		for rname, rterms := range relate.(db.Relate) {

			rcollection = nil

			ttop := len(rterms)
			for t := ttop - 1; t >= 0; t-- {
				rterm := rterms[t]
				switch rterm.(type) {
				case db.Collection:
					rcollection = rterm.(db.Collection)
				}
			}

			if rcollection == nil {
				rcollection = t.parent.Collection(rname)
			}

			relations = append(relations, sugar.Tuple{"all": false, "name": rname, "collection": rcollection, "terms": rterms})
		}
	}

	if relateAll != nil {
		for rname, rterms := range relateAll.(db.RelateAll) {
			rcollection = nil

			ttop := len(rterms)
			for t := ttop - 1; t >= 0; t-- {
				rterm := rterms[t]
				switch rterm.(type) {
				case db.Collection:
					rcollection = rterm.(db.Collection)
				}
			}

			if rcollection == nil {
				rcollection = t.parent.Collection(rname)
			}

			relations = append(relations, sugar.Tuple{"all": true, "name": rname, "collection": rcollection, "terms": rterms})
		}
	}

	var term interface{}

	jtop := len(relations)

	itop = len(result)
	items := make([]db.Item, itop)

	for i := 0; i < itop; i++ {

		item := db.Item{}

		// Default values.
		for key, val := range result[i] {
			item[key] = val
		}

		// Querying relations
		for j := 0; j < jtop; j++ {

			relation := relations[j]

			terms := []interface{}{}

			ktop := len(relation["terms"].(db.On))

			for k := 0; k < ktop; k++ {

				//term = tcopy[k]
				term = relation["terms"].(db.On)[k]

				switch term.(type) {
				// Just waiting for db.Cond statements.
				case db.Cond:
					for wkey, wval := range term.(db.Cond) {
						//if reflect.TypeOf(wval).Kind() == reflect.String { // does not always work.
						if reflect.TypeOf(wval).Name() == "string" {
							// Matching dynamic values.
							matched, _ := regexp.MatchString("\\{.+\\}", wval.(string))
							if matched {
								// Replacing dynamic values.
								kname := strings.Trim(wval.(string), "{}")
								term = db.Cond{wkey: item[kname]}
							}
						}
					}
				}
				terms = append(terms, term)
			}

			// Executing external query.
			if relation["all"] == true {
				value := relation["collection"].(*SqliteTable).invoke("FindAll", terms)
				item[relation["name"].(string)] = value[0].Interface().([]db.Item)
			} else {
				value := relation["collection"].(*SqliteTable).invoke("Find", terms)
				item[relation["name"].(string)] = value[0].Interface().(db.Item)
			}

		}

		// Appending to results.
		items[i] = item
	}

	return items
}
예제 #29
0
파일: main.go 프로젝트: tantra35/notifier
func (s *yamlSettings) Get(section, key string) string {
	return to.String(s.file.Get(section, key))
}
예제 #30
0
파일: weibo.go 프로젝트: hiproz/weigo
func call(client *http.Client, the_url string, method int, authorization string, params map[string]interface{}) ([]byte, error) {
	var url_params string
	var multipart_data *bytes.Buffer //For Upload Image
	var http_url string
	var http_body io.Reader
	var content_type string
	var request *http.Request
	var HTTP_METHOD string
	var err error
	switch method {
	case HTTP_GET:
		HTTP_METHOD = "GET"
		url_params, err = encodeParams(params)
		http_url = fmt.Sprintf("%v?%v", the_url, url_params)
		http_body = nil
	case HTTP_POST:
		HTTP_METHOD = "POST"
		url_params, err = encodeParams(params)
		content_type = "application/x-www-form-urlencoded"
		http_url = the_url
		http_body = strings.NewReader(url_params)
	case HTTP_UPLOAD:
		HTTP_METHOD = "POST"
		the_url = strings.Replace(the_url, "https://api.", "https://upload.api.", 1)
		content_type, multipart_data, err = encodeMultipart(params)
		http_url = the_url
		http_body = multipart_data
	}
	if err != nil {
		return nil, err
	}
	request, err = http.NewRequest(HTTP_METHOD, http_url, http_body)
	if err != nil {
		return nil, err
	}

	request.Header.Add("Accept-Encoding", "gzip")

	switch method {
	case HTTP_POST:
		request.Header.Add("Content-Type", content_type)
	case HTTP_UPLOAD:
		request.Header.Add("Content-Type", content_type)
		request.Header.Add("Content-Length", to.String(multipart_data.Len()))
	}
	if authorization != "" {
		request.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", authorization))
	}

	response, err := client.Do(request) // Do Request
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()

	body, err := read_body(response)
	if err != nil {
		return nil, err
	}

	return body, nil
}