<commit_before>package mongo

import (
	"encoding/json"
	"time"

	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"gopkg.in/mgo.v2/txn"
	"gopkg.in/oauth2.v3"
	"gopkg.in/oauth2.v3/models"
)

// TokenConfig Token Config
type TokenConfig struct {
	TxnCName     string // Store txn collection name(The default is oauth2)
	BasicCName   string // Store token based data collection name(The default is oauth2_basic)
	AccessCName  string // Store access token data collection name(The default is oauth2_access)
	RefreshCName string // Store refresh token data collection name(The default is oauth2_refresh)
}

// NewDefaultTokenConfig Create default token config
func NewDefaultTokenConfig() *TokenConfig {
	return &TokenConfig{
		TxnCName:     "oauth2_txn",
		BasicCName:   "oauth2_basic",
		AccessCName:  "oauth2_access",
		RefreshCName: "oauth2_refresh",
	}
}

// NewTokenStore Create a token store instance based on mongodb
func NewTokenStore(cfg *Config, tcfgs ...*TokenConfig) (store oauth2.TokenStore, err error) {
	ts := &TokenStore{
		mcfg: cfg,
		tcfg: NewDefaultTokenConfig(),
	}
	if len(tcfgs) > 0 {
		ts.tcfg = tcfgs[0]
	}
	session, err := mgo.Dial(ts.mcfg.URL)
	if err != nil {
		return
	}
	ts.session = session
	err = ts.c(ts.tcfg.BasicCName).EnsureIndex(mgo.Index{
		Key:         []string{"ExpiredAt"},
		ExpireAfter: time.Second * 1,
	})
	if err != nil {
		return
	}
	err = ts.c(ts.tcfg.AccessCName).EnsureIndex(mgo.Index{
		Key:         []string{"ExpiredAt"},
		ExpireAfter: time.Second * 1,
	})
	if err != nil {
		return
	}
	err = ts.c(ts.tcfg.RefreshCName).EnsureIndex(mgo.Index{
		Key:         []string{"ExpiredAt"},
		ExpireAfter: time.Second * 1,
	})
	if err != nil {
		return
	}
	store = ts
	return
}

// TokenStore MongoDB token store
type TokenStore struct {
	tcfg    *TokenConfig
	mcfg    *Config
	session *mgo.Session
}

func (ts *TokenStore) c(name string) *mgo.Collection {
	return ts.session.DB(ts.mcfg.DB).C(name)
}

// Create Create and store the new token information
func (ts *TokenStore) Create(info oauth2.TokenInfo) (err error) {
	var expiredAt time.Time
	if refresh := info.GetRefresh(); refresh != "" {
		expiredAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
		rinfo, rerr := ts.GetByRefresh(refresh)
		if rerr != nil {
			err = rerr
			return
		}
		if rinfo != nil {
			expiredAt = rinfo.GetRefreshCreateAt().Add(rinfo.GetRefreshExpiresIn())
		}
	}
	if expiredAt.IsZero() {
		expiredAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
	}
	id := bson.NewObjectId().Hex()
	jv, err := json.Marshal(info)
	if err != nil {
		return
	}
	runner := txn.NewRunner(ts.c(ts.tcfg.TxnCName))
	ops := []txn.Op{{
		C:      ts.tcfg.BasicCName,
		Id:     id,
		Assert: txn.DocMissing,
		Insert: basicData{
			Data:      jv,
			ExpiredAt: expiredAt,
		},
	}, {
		C:      ts.tcfg.AccessCName,
		Id:     info.GetAccess(),
		Assert: txn.DocMissing,
		Insert: tokenData{
			BasicID:   id,
			ExpiredAt: info.GetAccessCreateAt().Add(info.GetAccessExpiresIn()),
		},
	}}
	if refresh := info.GetRefresh(); refresh != "" {
		ops = append(ops, txn.Op{
			C:      ts.tcfg.RefreshCName,
			Id:     refresh,
			Assert: txn.DocMissing,
			Insert: tokenData{
				BasicID:   id,
				ExpiredAt: expiredAt,
			},
		})
	}
	err = runner.Run(ops, "", nil)
	return
}

// RemoveByAccess Use the access token to delete the token information
func (ts *TokenStore) RemoveByAccess(access string) (err error) {
	verr := ts.c(ts.tcfg.AccessCName).RemoveId(access)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
	}
	return
}

// RemoveByRefresh Use the refresh token to delete the token information
func (ts *TokenStore) RemoveByRefresh(refresh string) (err error) {
	verr := ts.c(ts.tcfg.RefreshCName).RemoveId(refresh)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
	}
	return
}

// get
func (ts *TokenStore) get(cname, token string) (ti oauth2.TokenInfo, err error) {
	var td tokenData
	verr := ts.c(cname).FindId(token).One(&td)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
		return
	}
	var bd basicData
	verr = ts.c(ts.tcfg.BasicCName).FindId(td.BasicID).One(&bd)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
		return
	}
	var tm models.Token
	err = json.Unmarshal(bd.Data, &tm)
	if err != nil {
		return
	}
	ti = &tm
	return
}

// GetByAccess Use the access token for token information data
func (ts *TokenStore) GetByAccess(access string) (ti oauth2.TokenInfo, err error) {
	ti, err = ts.get(ts.tcfg.AccessCName, access)
	return
}

// GetByRefresh Use the refresh token for token information data
func (ts *TokenStore) GetByRefresh(refresh string) (ti oauth2.TokenInfo, err error) {
	ti, err = ts.get(ts.tcfg.RefreshCName, refresh)
	return
}

type basicData struct {
	ID        string    `bson:"_id"`
	Data      []byte    `bson:"Data"`
	ExpiredAt time.Time `bson:"ExpiredAt"`
}

type tokenData struct {
	ID        string    `bson:"_id"`
	BasicID   string    `bson:"BasicID"`
	ExpiredAt time.Time `bson:"ExpiredAt"`
}
<commit_msg>Token storage optimization<commit_after>package mongo

import (
	"encoding/json"
	"time"

	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"gopkg.in/mgo.v2/txn"
	"gopkg.in/oauth2.v3"
	"gopkg.in/oauth2.v3/models"
)

// TokenConfig Token Config
type TokenConfig struct {
	TxnCName     string // Store txn collection name(The default is oauth2)
	BasicCName   string // Store token based data collection name(The default is oauth2_basic)
	AccessCName  string // Store access token data collection name(The default is oauth2_access)
	RefreshCName string // Store refresh token data collection name(The default is oauth2_refresh)
}

// NewDefaultTokenConfig Create default token config
func NewDefaultTokenConfig() *TokenConfig {
	return &TokenConfig{
		TxnCName:     "oauth2_txn",
		BasicCName:   "oauth2_basic",
		AccessCName:  "oauth2_access",
		RefreshCName: "oauth2_refresh",
	}
}

// NewTokenStore Create a token store instance based on mongodb
func NewTokenStore(cfg *Config, tcfgs ...*TokenConfig) (store oauth2.TokenStore, err error) {
	ts := &TokenStore{
		mcfg: cfg,
		tcfg: NewDefaultTokenConfig(),
	}
	if len(tcfgs) > 0 {
		ts.tcfg = tcfgs[0]
	}
	session, err := mgo.Dial(ts.mcfg.URL)
	if err != nil {
		return
	}
	ts.session = session
	err = ts.c(ts.tcfg.BasicCName).EnsureIndex(mgo.Index{
		Key:         []string{"ExpiredAt"},
		ExpireAfter: time.Second * 1,
	})
	if err != nil {
		return
	}
	err = ts.c(ts.tcfg.AccessCName).EnsureIndex(mgo.Index{
		Key:         []string{"ExpiredAt"},
		ExpireAfter: time.Second * 1,
	})
	if err != nil {
		return
	}
	err = ts.c(ts.tcfg.RefreshCName).EnsureIndex(mgo.Index{
		Key:         []string{"ExpiredAt"},
		ExpireAfter: time.Second * 1,
	})
	if err != nil {
		return
	}
	store = ts
	return
}

// TokenStore MongoDB token store
type TokenStore struct {
	tcfg    *TokenConfig
	mcfg    *Config
	session *mgo.Session
}

func (ts *TokenStore) c(name string) *mgo.Collection {
	return ts.session.DB(ts.mcfg.DB).C(name)
}

// Create Create and store the new token information
func (ts *TokenStore) Create(info oauth2.TokenInfo) (err error) {
	aexp := info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
	rexp := aexp
	if refresh := info.GetRefresh(); refresh != "" {
		rexp = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
		if aexp.Second() > rexp.Second() {
			aexp = rexp
		}
	}
	id := bson.NewObjectId().Hex()
	jv, err := json.Marshal(info)
	if err != nil {
		return
	}
	runner := txn.NewRunner(ts.c(ts.tcfg.TxnCName))
	ops := []txn.Op{{
		C:      ts.tcfg.BasicCName,
		Id:     id,
		Assert: txn.DocMissing,
		Insert: basicData{
			Data:      jv,
			ExpiredAt: rexp,
		},
	}, {
		C:      ts.tcfg.AccessCName,
		Id:     info.GetAccess(),
		Assert: txn.DocMissing,
		Insert: tokenData{
			BasicID:   id,
			ExpiredAt: aexp,
		},
	}}
	if refresh := info.GetRefresh(); refresh != "" {
		ops = append(ops, txn.Op{
			C:      ts.tcfg.RefreshCName,
			Id:     refresh,
			Assert: txn.DocMissing,
			Insert: tokenData{
				BasicID:   id,
				ExpiredAt: rexp,
			},
		})
	}
	err = runner.Run(ops, "", nil)
	return
}

// RemoveByAccess Use the access token to delete the token information
func (ts *TokenStore) RemoveByAccess(access string) (err error) {
	verr := ts.c(ts.tcfg.AccessCName).RemoveId(access)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
	}
	return
}

// RemoveByRefresh Use the refresh token to delete the token information
func (ts *TokenStore) RemoveByRefresh(refresh string) (err error) {
	verr := ts.c(ts.tcfg.RefreshCName).RemoveId(refresh)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
	}
	return
}

// get
func (ts *TokenStore) get(cname, token string) (ti oauth2.TokenInfo, err error) {
	var td tokenData
	verr := ts.c(cname).FindId(token).One(&td)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
		return
	}
	var bd basicData
	verr = ts.c(ts.tcfg.BasicCName).FindId(td.BasicID).One(&bd)
	if verr != nil {
		if verr == mgo.ErrNotFound {
			return
		}
		err = verr
		return
	}
	var tm models.Token
	err = json.Unmarshal(bd.Data, &tm)
	if err != nil {
		return
	}
	ti = &tm
	return
}

// GetByAccess Use the access token for token information data
func (ts *TokenStore) GetByAccess(access string) (ti oauth2.TokenInfo, err error) {
	ti, err = ts.get(ts.tcfg.AccessCName, access)
	return
}

// GetByRefresh Use the refresh token for token information data
func (ts *TokenStore) GetByRefresh(refresh string) (ti oauth2.TokenInfo, err error) {
	ti, err = ts.get(ts.tcfg.RefreshCName, refresh)
	return
}

type basicData struct {
	ID        string    `bson:"_id"`
	Data      []byte    `bson:"Data"`
	ExpiredAt time.Time `bson:"ExpiredAt"`
}

type tokenData struct {
	ID        string    `bson:"_id"`
	BasicID   string    `bson:"BasicID"`
	ExpiredAt time.Time `bson:"ExpiredAt"`
}
<|endoftext|>


<commit_before>package ncp

import (
	"regexp"
	"strconv"
	"strings"
)

func (t *Topic) getRating() float64 {
	var (
		reRating = regexp.MustCompile(`>(\d,\d|\d)<\/span>.+?\(Голосов:`)
		rating   float64
	)
	if reRating.Match(t.Body) == true {
		str := string(reRating.FindSubmatch(t.Body)[1])
		str = strings.Replace(str, ",", ".", -1)
		rating, _ = strconv.ParseFloat(str, 64)
	}
	return rating
}

func (t *Topic) getSize() int64 {
	var (
		reSize = regexp.MustCompile(`Размер блока: \d.+?B"> (\d{1,2},\d{1,2}|\d{3,4}|\d{1,2})\s`)
		size   int64
	)
	if reSize.Match(t.Body) == true {
		str := string(reSize.FindSubmatch(t.Body)[1])
		str = strings.Replace(str, ",", ".", -1)
		if s64, err := strconv.ParseFloat(str, 64); err == nil {
			if s64 < 100 {
				s64 = s64 * 1000
			}
			size = int64(s64)
		}
	}
	return size
}

func (t *Topic) getTorrent() string {
	var (
		reTor   = regexp.MustCompile(`<a href="download\.php\?id=(\d{5,7})" rel="nofollow">Скачать<`)
		torrent string
	)
	if reTor.Match(t.Body) == true {
		findTor := reTor.FindSubmatch(t.Body)
		torrent = "http://nnm-club.me/forum/download.php?id=" + string(findTor[1])
	}
	return torrent
}

func (t *Topic) getPoster() string {
	var (
		rePos = regexp.MustCompile(`"postImg postImgAligned img-right" title="http:\/\/assets\.nnm-club\.ws\/forum\/image\.php\?link=(.+?jpe{0,1}g)`)
		image string
	)
	if rePos.Match(t.Body) == true {
		image = string(rePos.FindSubmatch(t.Body)[1])
	}
	return image
}

func (t *Topic) getDate() string {
	var (
		reDate = regexp.MustCompile(`> (\d{1,2} .{3} \d{4}).{9}<`)
		date   string
	)
	if reDate.Match(t.Body) == true {
		date = replaceDate(string(reDate.FindSubmatch(t.Body)[1]))
	}
	return date
}

func (t *Topic) getSeeds() int64 {
	var (
		reSs  = regexp.MustCompile(`<span class="seed">\[ <b>(\d{1,5})\s`)
		seeds int64
	)
	if reSs.Match(t.Body) == true {
		ss := reSs.FindSubmatch(t.Body)
		seeds, _ = strconv.ParseInt(string(ss[1]), 10, 64)
	}
	return seeds
}

func (t *Topic) getLeechs() int64 {
	var (
		reLs   = regexp.MustCompile(`<span class="leech">\[ <b>(\d{1,5})\s`)
		leechs int64
	)
	if reLs.Match(t.Body) == true {
		ls := reLs.FindSubmatch(t.Body)
		leechs, _ = strconv.ParseInt(string(ls[1]), 10, 64)
	}
	return leechs
}

func getResolution(str string) string {
	var (
		reRes      = regexp.MustCompile(`(\d{3,4}x\d{3,4}|\d{3,4}X\d{3,4}|\d{3,4}х\d{3,4}|\d{3,4}Х\d{3,4})`)
		resolution string
	)
	if reRes.MatchString(str) == true {
		resolution = reRes.FindString(str)
	}
	return resolution
}

func (t *Topic) getCountry() string {
	var (
		reCountry = regexp.MustCompile(`<span style="font-weight: bold">Производство:\s*<\/span>(.+?)<`)
		country   string
	)
	if reCountry.Match(t.Body) == true {
		country = string(reCountry.FindSubmatch(t.Body)[1])
		country = cleanStr(country)
	}
	return country
}

func (t *Topic) getGenre() string {
	var (
		reGenre = regexp.MustCompile(`<span style="font-weight: bold">Жанр:\s*<\/span>(.+?)<`)
		genre   string
	)
	if reGenre.Match(t.Body) == true {
		genre = string(reGenre.FindSubmatch(t.Body)[1])
		genre = strings.ToLower(cleanStr(genre))
	}
	return genre
}

func (t *Topic) getDirector() string {
	var (
		reDirector = regexp.MustCompile(`<span style="font-weight: bold">Режиссер:\s*<\/span>(.+?)<`)
		director   string
	)
	if reDirector.Match(t.Body) == true {
		director = string(reDirector.FindSubmatch(t.Body)[1])
		director = cleanStr(director)
	}
	return director
}

func (t *Topic) getProducer() string {
	var (
		reProducer = regexp.MustCompile(`<span style="font-weight: bold">Продюсер:\s*<\/span>(.+?)<`)
		producer   string
	)
	if reProducer.Match(t.Body) == true {
		producer = string(reProducer.FindSubmatch(t.Body)[1])
		producer = cleanStr(producer)
	}
	return producer
}

func (t *Topic) getActors() string {
	var (
		reActors = regexp.MustCompile(`<span style="font-weight: bold">Актеры:\s*<\/span>(.+?)<`)
		actors   string
	)
	if reActors.Match(t.Body) == true {
		actors = string(reActors.FindSubmatch(t.Body)[1])
		actors = cleanStr(actors)
	}
	return actors
}

func (t *Topic) getDescription() string {
	var (
		reDescription = regexp.MustCompile(`<span style="font-weight: bold">(?:Описание фильма|Описание):\s*<\/span>(.+?)<`)
		description   string
	)
	if reDescription.Match(t.Body) == true {
		description = string(reDescription.FindSubmatch(t.Body)[1])
		description = cleanStr(description)
	}
	return description
}

func (t *Topic) getAge() string {
	var (
		reAge = regexp.MustCompile(`<span style="font-weight: bold">Возраст:\s*<\/span>(.+?)<`)
		age   string
	)
	if reAge.Match(t.Body) == true {
		age = string(reAge.FindSubmatch(t.Body)[1])
		age = cleanStr(age)
	}
	return age
}

func (t *Topic) getReleaseDate() string {
	var (
		reReleaseDate = regexp.MustCompile(`<span style="font-weight: bold">Дата мировой премьеры:\s*<\/span>(.+?)<`)
		releaseDate   string
	)
	if reReleaseDate.Match(t.Body) == true {
		releaseDate = string(reReleaseDate.FindSubmatch(t.Body)[1])
		releaseDate = cleanStr(releaseDate)
		releaseDate = replaceDate(releaseDate)
	}
	return releaseDate
}

func (t *Topic) getRussianDate() string {
	var (
		reRussianDate = regexp.MustCompile(`<span style="font-weight: bold">(?:Дата премьеры в России|Дата Российской премьеры|Дата российской премьеры):\s*<\/span>(.+?)<`)
		russianDate   string
	)
	if reRussianDate.Match(t.Body) == true {
		russianDate = string(reRussianDate.FindSubmatch(t.Body)[1])
		russianDate = cleanStr(russianDate)
		russianDate = replaceDate(russianDate)
	}
	return russianDate
}

func (t *Topic) getDuration() string {
	var (
		reDuration = regexp.MustCompile(`<span style="font-weight: bold">Продолжительность:\s*<\/span>(.+?)<`)
		duration   string
	)
	if reDuration.Match(t.Body) == true {
		duration = string(reDuration.FindSubmatch(t.Body)[1])
		duration = cleanStr(duration)
	} else {
		reDuration = regexp.MustCompile(`<br \/>Продолжительность\s+?: (\d{1,2}) ч\. (\d{1,2}) м\.`)
		if reDuration.Match(t.Body) == true {
			submatch := reDuration.FindSubmatch(t.Body)
			hour := string(submatch[1])
			minute := string(submatch[2])
			if len(hour) == 1 {
				hour = "0" + hour
			}
			if len(minute) == 1 {
				minute = "0" + minute
			}
			duration = hour + ":" + minute + ":00"
		}
	}
	return duration
}

func (t *Topic) getQuality() string {
	var (
		reQuality = regexp.MustCompile(`<span style="font-weight: bold">(?:Качество видео|Качество):\s*<\/span>(.+?)<`)
		quality   string
	)
	if reQuality.Match(t.Body) == true {
		quality = string(reQuality.FindSubmatch(t.Body)[1])
		quality = cleanStr(quality)
	}
	return quality
}

func (t *Topic) getTranslation() string {
	var (
		reTranslation = regexp.MustCompile(`<span style="font-weight: bold">Перевод:\s*<\/span>(.+?)<`)
		translation   string
	)
	if reTranslation.Match(t.Body) == true {
		translation = string(reTranslation.FindSubmatch(t.Body)[1])
		translation = cleanStr(translation)
		// if caseInsensitiveContains(translation, "не требуется") == true {
		// 	translation = "Не требуется"
		// }
	}
	return translation
}

func (t *Topic) getSubtitlesType() string {
	var (
		reSubtitlesType = regexp.MustCompile(`<span style="font-weight: bold">Вид субтитров:\s*<\/span>(.+?)<`)
		subtitlesType   string
	)
	if reSubtitlesType.Match(t.Body) == true {
		subtitlesType = string(reSubtitlesType.FindSubmatch(t.Body)[1])
		subtitlesType = cleanStr(subtitlesType)
	}
	return subtitlesType
}

func (t *Topic) getSubtitles() string {
	var (
		reSubtitles = regexp.MustCompile(`<span style="font-weight: bold">Субтитры:\s*<\/span>(.+?)<`)
		subtitles   string
	)
	if reSubtitles.Match(t.Body) == true {
		subtitles = string(reSubtitles.FindSubmatch(t.Body)[1])
		subtitles = cleanStr(subtitles)
	}
	return subtitles
}

func (t *Topic) getVideo() string {
	var (
		reVideo = regexp.MustCompile(`<span style="font-weight: bold">Видео:\s*<\/span>(.+?)<`)
		video   string
	)
	if reVideo.Match(t.Body) == true {
		video = string(reVideo.FindSubmatch(t.Body)[1])
		video = cleanStr(video)
	}
	return video
}

func (t *Topic) getAudio1() string {
	var (
		reAudio = regexp.MustCompile(`<span style="font-weight: bold">(?:Аудио\s?:\s*|Аудио\s?.?1.?:\s*)<\/span>(.+?)<`)
		audio   string
	)
	if reAudio.Match(t.Body) == true {
		audio = string(reAudio.FindSubmatch(t.Body)[1])
		audio = cleanStr(audio)
	}
	return audio
}

func (t *Topic) getAudio2() string {
	var (
		reAudio = regexp.MustCompile(`<span style="font-weight: bold">Аудио\s?.?2.?:\s*<\/span>(.+?)<`)
		audio   string
	)
	if reAudio.Match(t.Body) == true {
		audio = string(reAudio.FindSubmatch(t.Body)[1])
		audio = cleanStr(audio)
	}
	return audio
}

func (t *Topic) getAudio3() string {
	var (
		reAudio = regexp.MustCompile(`<span style="font-weight: bold">Аудио\s?.?3.?:\s*<\/span>(.+?)<`)
		audio   string
	)
	if reAudio.Match(t.Body) == true {
		audio = string(reAudio.FindSubmatch(t.Body)[1])
		audio = cleanStr(audio)
	}
	return audio
}
<commit_msg>fix regexp duration<commit_after>package ncp

import (
	"regexp"
	"strconv"
	"strings"
)

func (t *Topic) getRating() float64 {
	var (
		reRating = regexp.MustCompile(`>(\d,\d|\d)<\/span>.+?\(Голосов:`)
		rating   float64
	)
	if reRating.Match(t.Body) == true {
		str := string(reRating.FindSubmatch(t.Body)[1])
		str = strings.Replace(str, ",", ".", -1)
		rating, _ = strconv.ParseFloat(str, 64)
	}
	return rating
}

func (t *Topic) getSize() int64 {
	var (
		reSize = regexp.MustCompile(`Размер блока: \d.+?B"> (\d{1,2},\d{1,2}|\d{3,4}|\d{1,2})\s`)
		size   int64
	)
	if reSize.Match(t.Body) == true {
		str := string(reSize.FindSubmatch(t.Body)[1])
		str = strings.Replace(str, ",", ".", -1)
		if s64, err := strconv.ParseFloat(str, 64); err == nil {
			if s64 < 100 {
				s64 = s64 * 1000
			}
			size = int64(s64)
		}
	}
	return size
}

func (t *Topic) getTorrent() string {
	var (
		reTor   = regexp.MustCompile(`<a href="download\.php\?id=(\d{5,7})" rel="nofollow">Скачать<`)
		torrent string
	)
	if reTor.Match(t.Body) == true {
		findTor := reTor.FindSubmatch(t.Body)
		torrent = "http://nnm-club.me/forum/download.php?id=" + string(findTor[1])
	}
	return torrent
}

func (t *Topic) getPoster() string {
	var (
		rePos = regexp.MustCompile(`"postImg postImgAligned img-right" title="http:\/\/assets\.nnm-club\.ws\/forum\/image\.php\?link=(.+?jpe{0,1}g)`)
		image string
	)
	if rePos.Match(t.Body) == true {
		image = string(rePos.FindSubmatch(t.Body)[1])
	}
	return image
}

func (t *Topic) getDate() string {
	var (
		reDate = regexp.MustCompile(`> (\d{1,2} .{3} \d{4}).{9}<`)
		date   string
	)
	if reDate.Match(t.Body) == true {
		date = replaceDate(string(reDate.FindSubmatch(t.Body)[1]))
	}
	return date
}

func (t *Topic) getSeeds() int64 {
	var (
		reSs  = regexp.MustCompile(`<span class="seed">\[ <b>(\d{1,5})\s`)
		seeds int64
	)
	if reSs.Match(t.Body) == true {
		ss := reSs.FindSubmatch(t.Body)
		seeds, _ = strconv.ParseInt(string(ss[1]), 10, 64)
	}
	return seeds
}

func (t *Topic) getLeechs() int64 {
	var (
		reLs   = regexp.MustCompile(`<span class="leech">\[ <b>(\d{1,5})\s`)
		leechs int64
	)
	if reLs.Match(t.Body) == true {
		ls := reLs.FindSubmatch(t.Body)
		leechs, _ = strconv.ParseInt(string(ls[1]), 10, 64)
	}
	return leechs
}

func getResolution(str string) string {
	var (
		reRes      = regexp.MustCompile(`(\d{3,4}x\d{3,4}|\d{3,4}X\d{3,4}|\d{3,4}х\d{3,4}|\d{3,4}Х\d{3,4})`)
		resolution string
	)
	if reRes.MatchString(str) == true {
		resolution = reRes.FindString(str)
	}
	return resolution
}

func (t *Topic) getCountry() string {
	var (
		reCountry = regexp.MustCompile(`<span style="font-weight: bold">Производство:\s*<\/span>(.+?)<`)
		country   string
	)
	if reCountry.Match(t.Body) == true {
		country = string(reCountry.FindSubmatch(t.Body)[1])
		country = cleanStr(country)
	}
	return country
}

func (t *Topic) getGenre() string {
	var (
		reGenre = regexp.MustCompile(`<span style="font-weight: bold">Жанр:\s*<\/span>(.+?)<`)
		genre   string
	)
	if reGenre.Match(t.Body) == true {
		genre = string(reGenre.FindSubmatch(t.Body)[1])
		genre = strings.ToLower(cleanStr(genre))
	}
	return genre
}

func (t *Topic) getDirector() string {
	var (
		reDirector = regexp.MustCompile(`<span style="font-weight: bold">Режиссер:\s*<\/span>(.+?)<`)
		director   string
	)
	if reDirector.Match(t.Body) == true {
		director = string(reDirector.FindSubmatch(t.Body)[1])
		director = cleanStr(director)
	}
	return director
}

func (t *Topic) getProducer() string {
	var (
		reProducer = regexp.MustCompile(`<span style="font-weight: bold">Продюсер:\s*<\/span>(.+?)<`)
		producer   string
	)
	if reProducer.Match(t.Body) == true {
		producer = string(reProducer.FindSubmatch(t.Body)[1])
		producer = cleanStr(producer)
	}
	return producer
}

func (t *Topic) getActors() string {
	var (
		reActors = regexp.MustCompile(`<span style="font-weight: bold">Актеры:\s*<\/span>(.+?)<`)
		actors   string
	)
	if reActors.Match(t.Body) == true {
		actors = string(reActors.FindSubmatch(t.Body)[1])
		actors = cleanStr(actors)
	}
	return actors
}

func (t *Topic) getDescription() string {
	var (
		reDescription = regexp.MustCompile(`<span style="font-weight: bold">(?:Описание фильма|Описание):\s*<\/span>(.+?)<`)
		description   string
	)
	if reDescription.Match(t.Body) == true {
		description = string(reDescription.FindSubmatch(t.Body)[1])
		description = cleanStr(description)
	}
	return description
}

func (t *Topic) getAge() string {
	var (
		reAge = regexp.MustCompile(`<span style="font-weight: bold">Возраст:\s*<\/span>(.+?)<`)
		age   string
	)
	if reAge.Match(t.Body) == true {
		age = string(reAge.FindSubmatch(t.Body)[1])
		age = cleanStr(age)
	}
	return age
}

func (t *Topic) getReleaseDate() string {
	var (
		reReleaseDate = regexp.MustCompile(`<span style="font-weight: bold">Дата мировой премьеры:\s*<\/span>(.+?)<`)
		releaseDate   string
	)
	if reReleaseDate.Match(t.Body) == true {
		releaseDate = string(reReleaseDate.FindSubmatch(t.Body)[1])
		releaseDate = cleanStr(releaseDate)
		releaseDate = replaceDate(releaseDate)
	}
	return releaseDate
}

func (t *Topic) getRussianDate() string {
	var (
		reRussianDate = regexp.MustCompile(`<span style="font-weight: bold">(?:Дата премьеры в России|Дата Российской премьеры|Дата российской премьеры):\s*<\/span>(.+?)<`)
		russianDate   string
	)
	if reRussianDate.Match(t.Body) == true {
		russianDate = string(reRussianDate.FindSubmatch(t.Body)[1])
		russianDate = cleanStr(russianDate)
		russianDate = replaceDate(russianDate)
	}
	return russianDate
}

func (t *Topic) getDuration() string {
	var (
		reDuration = regexp.MustCompile(`<span style="font-weight: bold">Продолжительность:\s*<\/span>(.+?)<`)
		duration   string
	)
	if reDuration.Match(t.Body) == true {
		duration = string(reDuration.FindSubmatch(t.Body)[1])
		duration = cleanStr(duration)
	} else {
		reDuration = regexp.MustCompile(`\sПродолжительность\s+?: (\d{1,2}) ч\. (\d{1,2}) м\.`)
		if reDuration.Match(t.Body) == true {
			submatch := reDuration.FindSubmatch(t.Body)
			hour := string(submatch[1])
			minute := string(submatch[2])
			if len(hour) == 1 {
				hour = "0" + hour
			}
			if len(minute) == 1 {
				minute = "0" + minute
			}
			duration = hour + ":" + minute + ":00"
		}
	}
	return duration
}

func (t *Topic) getQuality() string {
	var (
		reQuality = regexp.MustCompile(`<span style="font-weight: bold">(?:Качество видео|Качество):\s*<\/span>(.+?)<`)
		quality   string
	)
	if reQuality.Match(t.Body) == true {
		quality = string(reQuality.FindSubmatch(t.Body)[1])
		quality = cleanStr(quality)
	}
	return quality
}

func (t *Topic) getTranslation() string {
	var (
		reTranslation = regexp.MustCompile(`<span style="font-weight: bold">Перевод:\s*<\/span>(.+?)<`)
		translation   string
	)
	if reTranslation.Match(t.Body) == true {
		translation = string(reTranslation.FindSubmatch(t.Body)[1])
		translation = cleanStr(translation)
		// if caseInsensitiveContains(translation, "не требуется") == true {
		// 	translation = "Не требуется"
		// }
	}
	return translation
}

func (t *Topic) getSubtitlesType() string {
	var (
		reSubtitlesType = regexp.MustCompile(`<span style="font-weight: bold">Вид субтитров:\s*<\/span>(.+?)<`)
		subtitlesType   string
	)
	if reSubtitlesType.Match(t.Body) == true {
		subtitlesType = string(reSubtitlesType.FindSubmatch(t.Body)[1])
		subtitlesType = cleanStr(subtitlesType)
	}
	return subtitlesType
}

func (t *Topic) getSubtitles() string {
	var (
		reSubtitles = regexp.MustCompile(`<span style="font-weight: bold">Субтитры:\s*<\/span>(.+?)<`)
		subtitles   string
	)
	if reSubtitles.Match(t.Body) == true {
		subtitles = string(reSubtitles.FindSubmatch(t.Body)[1])
		subtitles = cleanStr(subtitles)
	}
	return subtitles
}

func (t *Topic) getVideo() string {
	var (
		reVideo = regexp.MustCompile(`<span style="font-weight: bold">Видео:\s*<\/span>(.+?)<`)
		video   string
	)
	if reVideo.Match(t.Body) == true {
		video = string(reVideo.FindSubmatch(t.Body)[1])
		video = cleanStr(video)
	}
	return video
}

func (t *Topic) getAudio1() string {
	var (
		reAudio = regexp.MustCompile(`<span style="font-weight: bold">(?:Аудио\s?:\s*|Аудио\s?.?1.?:\s*)<\/span>(.+?)<`)
		audio   string
	)
	if reAudio.Match(t.Body) == true {
		audio = string(reAudio.FindSubmatch(t.Body)[1])
		audio = cleanStr(audio)
	}
	return audio
}

func (t *Topic) getAudio2() string {
	var (
		reAudio = regexp.MustCompile(`<span style="font-weight: bold">Аудио\s?.?2.?:\s*<\/span>(.+?)<`)
		audio   string
	)
	if reAudio.Match(t.Body) == true {
		audio = string(reAudio.FindSubmatch(t.Body)[1])
		audio = cleanStr(audio)
	}
	return audio
}

func (t *Topic) getAudio3() string {
	var (
		reAudio = regexp.MustCompile(`<span style="font-weight: bold">Аудио\s?.?3.?:\s*<\/span>(.+?)<`)
		audio   string
	)
	if reAudio.Match(t.Body) == true {
		audio = string(reAudio.FindSubmatch(t.Body)[1])
		audio = cleanStr(audio)
	}
	return audio
}
<|endoftext|>


<commit_before>package gps

import (
	"fmt"
	"strconv"
	"strings"
)

const (
	successChar   = "✓"
	successCharSp = successChar + " "
	failChar      = "✗"
	failCharSp    = failChar + " "
	backChar      = "←"
	innerIndent   = "  "
)

func (s *solver) traceCheckPkgs(bmi bimodalIdentifier) {
	if s.tl == nil {
		return
	}

	prefix := getprei(len(s.vqs) + 1)
	s.tl.Printf("%s\n", tracePrefix(fmt.Sprintf("? revisit %s to add %v pkgs", bmi.id.errString(), len(bmi.pl)), prefix, prefix))
}

func (s *solver) traceCheckQueue(q *versionQueue, bmi bimodalIdentifier, cont bool, offset int) {
	if s.tl == nil {
		return
	}

	prefix := getprei(len(s.vqs) + offset)
	vlen := strconv.Itoa(len(q.pi))
	if !q.allLoaded {
		vlen = "at least " + vlen
	}

	// TODO(sdboyer) how...to list the packages in the limited space we have?
	var verb string
	indent := ""
	if cont {
		// Continue is an "inner" message.. indenting
		verb = "continue"
		vlen = vlen + " more"
		indent = innerIndent
	} else {
		verb = "attempt"
	}

	s.tl.Printf("%s\n", tracePrefix(fmt.Sprintf("%s? %s %s with %v pkgs; %s versions to try",indent, verb, bmi.id.errString(), len(bmi.pl), vlen), prefix, prefix))
}

// traceStartBacktrack is called with the bmi that first failed, thus initiating
// backtracking
func (s *solver) traceStartBacktrack(bmi bimodalIdentifier, err error, pkgonly bool) {
	if s.tl == nil {
		return
	}

	var msg string
	if pkgonly {
		msg = fmt.Sprintf("%s%s could not add %v pkgs to %s; begin backtrack",innerIndent, backChar, len(bmi.pl), bmi.id.errString())
	} else {
		msg = fmt.Sprintf("%s%s no more versions of %s to try; begin backtrack",innerIndent, backChar, bmi.id.errString())
	}

	prefix := getprei(len(s.sel.projects))
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

// traceBacktrack is called when a package or project is poppped off during
// backtracking
func (s *solver) traceBacktrack(bmi bimodalIdentifier, pkgonly bool) {
	if s.tl == nil {
		return
	}

	var msg string
	if pkgonly {
		msg = fmt.Sprintf("%s backtrack: popped %v pkgs from %s", backChar, len(bmi.pl), bmi.id.errString())
	} else {
		msg = fmt.Sprintf("%s backtrack: no more versions of %s to try", backChar, bmi.id.errString())
	}

	prefix := getprei(len(s.sel.projects))
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

// Called just once after solving has finished, whether success or not
func (s *solver) traceFinish(sol solution, err error) {
	if s.tl == nil {
		return
	}

	if err == nil {
		var pkgcount int
		for _, lp := range sol.Projects() {
			pkgcount += len(lp.pkgs)
		}
		s.tl.Printf("%s%s found solution with %v packages from %v projects",innerIndent, successChar, pkgcount, len(sol.Projects()))
	} else {
		s.tl.Printf("%s%s solving failed",innerIndent, failChar)
	}
}

// traceSelectRoot is called just once, when the root project is selected
func (s *solver) traceSelectRoot(ptree PackageTree, cdeps []completeDep) {
	if s.tl == nil {
		return
	}

	// This duplicates work a bit, but we're in trace mode and it's only once,
	// so who cares
	rm := ptree.ExternalReach(true, true, s.rd.ig)

	s.tl.Printf("Root project is %q", s.rd.rpt.ImportRoot)

	var expkgs int
	for _, cdep := range cdeps {
		expkgs += len(cdep.pl)
	}

	// TODO(sdboyer) include info on ignored pkgs/imports, etc.
	s.tl.Printf(" %v transitively valid internal packages", len(rm))
	s.tl.Printf(" %v external packages imported from %v projects", expkgs, len(cdeps))
	s.tl.Printf("(0)   " + successCharSp + "select (root)")
}

// traceSelect is called when an atom is successfully selected
func (s *solver) traceSelect(awp atomWithPackages, pkgonly bool) {
	if s.tl == nil {
		return
	}

	var msg string
	if pkgonly {
		msg = fmt.Sprintf("%s%s include %v more pkgs from %s",innerIndent, successChar, len(awp.pl), a2vs(awp.a))
	} else {
		msg = fmt.Sprintf("%s select %s w/%v pkgs", successChar, a2vs(awp.a), len(awp.pl))
	}

	prefix := getprei(len(s.sel.projects) - 1)
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

func (s *solver) traceInfo(args ...interface{}) {
	if s.tl == nil {
		return
	}

	if len(args) == 0 {
		panic("must pass at least one param to traceInfo")
	}

	preflen := len(s.sel.projects)
	var msg string
	switch data := args[0].(type) {
	case string:
		msg = tracePrefix(innerIndent + fmt.Sprintf(data, args[1:]...), "  ", "  ")
	case traceError:
		preflen++
		// We got a special traceError, use its custom method
		msg = tracePrefix(innerIndent + data.traceString(), "  ", failCharSp)
	case error:
		// Regular error; still use the x leader but default Error() string
		msg = tracePrefix(innerIndent + data.Error(), "  ", failCharSp)
	default:
		// panic here because this can *only* mean a stupid internal bug
		panic(fmt.Sprintf("%canary - unknown type passed as first param to traceInfo %T", data))
	}

	prefix := getprei(preflen)
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

func getprei(i int) string {
	var s string
	if i < 10 {
		s = fmt.Sprintf("(%d)	", i)
	} else if i < 100 {
		s = fmt.Sprintf("(%d)  ", i)
	} else {
		s = fmt.Sprintf("(%d) ", i)
	}
	return s
}

func tracePrefix(msg, sep, fsep string) string {
	parts := strings.Split(strings.TrimSuffix(msg, "\n"), "\n")
	for k, str := range parts {
		if k == 0 {
			parts[k] = fsep + str
		} else {
			parts[k] = sep + str
		}
	}

	return strings.Join(parts, "\n")
}
<commit_msg>Removing extraneous %<commit_after>package gps

import (
	"fmt"
	"strconv"
	"strings"
)

const (
	successChar   = "✓"
	successCharSp = successChar + " "
	failChar      = "✗"
	failCharSp    = failChar + " "
	backChar      = "←"
	innerIndent   = "  "
)

func (s *solver) traceCheckPkgs(bmi bimodalIdentifier) {
	if s.tl == nil {
		return
	}

	prefix := getprei(len(s.vqs) + 1)
	s.tl.Printf("%s\n", tracePrefix(fmt.Sprintf("? revisit %s to add %v pkgs", bmi.id.errString(), len(bmi.pl)), prefix, prefix))
}

func (s *solver) traceCheckQueue(q *versionQueue, bmi bimodalIdentifier, cont bool, offset int) {
	if s.tl == nil {
		return
	}

	prefix := getprei(len(s.vqs) + offset)
	vlen := strconv.Itoa(len(q.pi))
	if !q.allLoaded {
		vlen = "at least " + vlen
	}

	// TODO(sdboyer) how...to list the packages in the limited space we have?
	var verb string
	indent := ""
	if cont {
		// Continue is an "inner" message.. indenting
		verb = "continue"
		vlen = vlen + " more"
		indent = innerIndent
	} else {
		verb = "attempt"
	}

	s.tl.Printf("%s\n", tracePrefix(fmt.Sprintf("%s? %s %s with %v pkgs; %s versions to try",indent, verb, bmi.id.errString(), len(bmi.pl), vlen), prefix, prefix))
}

// traceStartBacktrack is called with the bmi that first failed, thus initiating
// backtracking
func (s *solver) traceStartBacktrack(bmi bimodalIdentifier, err error, pkgonly bool) {
	if s.tl == nil {
		return
	}

	var msg string
	if pkgonly {
		msg = fmt.Sprintf("%s%s could not add %v pkgs to %s; begin backtrack",innerIndent, backChar, len(bmi.pl), bmi.id.errString())
	} else {
		msg = fmt.Sprintf("%s%s no more versions of %s to try; begin backtrack",innerIndent, backChar, bmi.id.errString())
	}

	prefix := getprei(len(s.sel.projects))
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

// traceBacktrack is called when a package or project is poppped off during
// backtracking
func (s *solver) traceBacktrack(bmi bimodalIdentifier, pkgonly bool) {
	if s.tl == nil {
		return
	}

	var msg string
	if pkgonly {
		msg = fmt.Sprintf("%s backtrack: popped %v pkgs from %s", backChar, len(bmi.pl), bmi.id.errString())
	} else {
		msg = fmt.Sprintf("%s backtrack: no more versions of %s to try", backChar, bmi.id.errString())
	}

	prefix := getprei(len(s.sel.projects))
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

// Called just once after solving has finished, whether success or not
func (s *solver) traceFinish(sol solution, err error) {
	if s.tl == nil {
		return
	}

	if err == nil {
		var pkgcount int
		for _, lp := range sol.Projects() {
			pkgcount += len(lp.pkgs)
		}
		s.tl.Printf("%s%s found solution with %v packages from %v projects",innerIndent, successChar, pkgcount, len(sol.Projects()))
	} else {
		s.tl.Printf("%s%s solving failed",innerIndent, failChar)
	}
}

// traceSelectRoot is called just once, when the root project is selected
func (s *solver) traceSelectRoot(ptree PackageTree, cdeps []completeDep) {
	if s.tl == nil {
		return
	}

	// This duplicates work a bit, but we're in trace mode and it's only once,
	// so who cares
	rm := ptree.ExternalReach(true, true, s.rd.ig)

	s.tl.Printf("Root project is %q", s.rd.rpt.ImportRoot)

	var expkgs int
	for _, cdep := range cdeps {
		expkgs += len(cdep.pl)
	}

	// TODO(sdboyer) include info on ignored pkgs/imports, etc.
	s.tl.Printf(" %v transitively valid internal packages", len(rm))
	s.tl.Printf(" %v external packages imported from %v projects", expkgs, len(cdeps))
	s.tl.Printf("(0)   " + successCharSp + "select (root)")
}

// traceSelect is called when an atom is successfully selected
func (s *solver) traceSelect(awp atomWithPackages, pkgonly bool) {
	if s.tl == nil {
		return
	}

	var msg string
	if pkgonly {
		msg = fmt.Sprintf("%s%s include %v more pkgs from %s",innerIndent, successChar, len(awp.pl), a2vs(awp.a))
	} else {
		msg = fmt.Sprintf("%s select %s w/%v pkgs", successChar, a2vs(awp.a), len(awp.pl))
	}

	prefix := getprei(len(s.sel.projects) - 1)
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

func (s *solver) traceInfo(args ...interface{}) {
	if s.tl == nil {
		return
	}

	if len(args) == 0 {
		panic("must pass at least one param to traceInfo")
	}

	preflen := len(s.sel.projects)
	var msg string
	switch data := args[0].(type) {
	case string:
		msg = tracePrefix(innerIndent + fmt.Sprintf(data, args[1:]...), "  ", "  ")
	case traceError:
		preflen++
		// We got a special traceError, use its custom method
		msg = tracePrefix(innerIndent + data.traceString(), "  ", failCharSp)
	case error:
		// Regular error; still use the x leader but default Error() string
		msg = tracePrefix(innerIndent + data.Error(), "  ", failCharSp)
	default:
		// panic here because this can *only* mean a stupid internal bug
		panic(fmt.Sprintf("canary - unknown type passed as first param to traceInfo %T", data))
	}

	prefix := getprei(preflen)
	s.tl.Printf("%s\n", tracePrefix(msg, prefix, prefix))
}

func getprei(i int) string {
	var s string
	if i < 10 {
		s = fmt.Sprintf("(%d)	", i)
	} else if i < 100 {
		s = fmt.Sprintf("(%d)  ", i)
	} else {
		s = fmt.Sprintf("(%d) ", i)
	}
	return s
}

func tracePrefix(msg, sep, fsep string) string {
	parts := strings.Split(strings.TrimSuffix(msg, "\n"), "\n")
	for k, str := range parts {
		if k == 0 {
			parts[k] = fsep + str
		} else {
			parts[k] = sep + str
		}
	}

	return strings.Join(parts, "\n")
}
<|endoftext|>


<commit_before>package main

import (
	"net/http"
	"strings"
	"io"
	"io/ioutil"
	"encoding/json"
	"github.com/palmergs/tokensearch"
)

func TokensHandler(w http.ResponseWriter, r *http.Request) {

	r.ParseForm()
	switch strings.ToUpper(r.Method) {
	case "POST", "PUT":
		insertTokenHandler(w, r)
	case "DELETE":
		deleteTokenHandler(w, r)
	case "GET", "":
		getTokensHandler(w, r)
	}
}

func deleteTokenHandler(w http.ResponseWriter, r *http.Request) {
	token, err := unmarshalToken(r)
	if err != nil {
		writeError(w, err)
	} else {
		token.InitKey();
		root.Remove(&token)
		writeToken(w, token)
	}
}

func insertTokenHandler(w http.ResponseWriter, r *http.Request) {
	token, err := unmarshalToken(r)
	if err != nil {
		writeError(w, err)
	} else {
		token.InitKey();
		root.Insert(&token)
		writeToken(w, token)
	}
}

func getTokensHandler(w http.ResponseWriter, r *http.Request) {
	matches := root.AllValues(9999)
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(matches); err != nil {
		panic(err)
	}
}

func unmarshalToken(r *http.Request) (tokensearch.Token, error) {
	var token tokensearch.Token
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 4097))
	if err != nil {
		return token, err
	}
	if err := r.Body.Close(); err != nil {
		return token, err
	}
	if err := json.Unmarshal(body, &token); err != nil {
		return token, err
	}
	return token, nil
}

func writeError(w http.ResponseWriter, err error) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(422)
	if err := json.NewEncoder(w).Encode(err); err != nil {
		panic(err)
	}
}

func writeToken(w http.ResponseWriter, token tokensearch.Token) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(token); err != nil {
		panic(err)
	}
}<commit_msg>change id for token to int64<commit_after>package main

import (
	"net/http"
	"strings"
	"io"
	"io/ioutil"
	"encoding/json"
	"log"
	"github.com/palmergs/tokensearch"
)

func TokensHandler(w http.ResponseWriter, r *http.Request) {

	r.ParseForm()
	switch strings.ToUpper(r.Method) {
	case "POST", "PUT":
		insertTokenHandler(w, r)
	case "DELETE":
		deleteTokenHandler(w, r)
	case "GET", "":
		getTokensHandler(w, r)
	}
}

func deleteTokenHandler(w http.ResponseWriter, r *http.Request) {
	token, err := unmarshalToken(r)
	if err != nil {
		writeError(w, err)
	} else {
		token.InitKey()
		root.Remove(&token)
		writeToken(w, token)
	}
}

func insertTokenHandler(w http.ResponseWriter, r *http.Request) {
	token, err := unmarshalToken(r)
	if err != nil {
		writeError(w, err)
	} else {
		token.InitKey()
		root.Insert(&token)
		writeToken(w, token)
	}
}

func getTokensHandler(w http.ResponseWriter, r *http.Request) {
	matches := root.AllValues(9999)
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(matches); err != nil {
		panic(err)
	}
}

func unmarshalToken(r *http.Request) (tokensearch.Token, error) {
	var token tokensearch.Token
	defer r.Body.Close()
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 4097))
	if err != nil {
		log.Printf("errored: %v\n", err)
		return token, err
	}
	if err := json.Unmarshal(body, &token); err != nil {
		log.Printf("errored: %v\n", err)
		return token, err
	}
	log.Printf("token: %v\n", token)
	return token, nil
}

func writeError(w http.ResponseWriter, err error) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(422)
	if err := json.NewEncoder(w).Encode(err); err != nil {
		panic(err)
	}
}

func writeToken(w http.ResponseWriter, token tokensearch.Token) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(token); err != nil {
		panic(err)
	}
}<|endoftext|>


<commit_before>package python

import (
	"bytes"
	"encoding/json"
	"fmt"
	"text/template"

	"sourcegraph.com/sourcegraph/srcgraph/config"
	"sourcegraph.com/sourcegraph/srcgraph/container"
	"sourcegraph.com/sourcegraph/srcgraph/dep2"
	"sourcegraph.com/sourcegraph/srcgraph/task2"
	"sourcegraph.com/sourcegraph/srcgraph/unit"
)

func init() {
	dep2.RegisterLister(&FauxPackage{}, dep2.DockerLister{defaultPythonEnv})
	dep2.RegisterResolver(pythonRequirementTargetType, defaultPythonEnv)
}

func (p *pythonEnv) BuildLister(dir string, unit unit.SourceUnit, c *config.Repository, x *task2.Context) (*container.Command, error) {
	dockerfile, err := p.depDockerfile()
	if err != nil {
		return nil, err
	}

	return &container.Command{
		Container: container.Container{
			Dockerfile: dockerfile,
			RunOptions: []string{"-v", dir + ":" + srcRoot},
			Cmd:        []string{"pydep-run.py", srcRoot},
			Stderr:     x.Stderr,
			Stdout:     x.Stdout,
		},
		Transform: func(orig []byte) ([]byte, error) {
			var reqs []requirement
			err := json.NewDecoder(bytes.NewReader(orig)).Decode(&reqs)
			if err != nil {
				return nil, err
			}
			deps := make([]*dep2.RawDependency, len(reqs))
			for i, req := range reqs {
				deps[i] = &dep2.RawDependency{
					TargetType: pythonRequirementTargetType,
					Target:     req,
				}
			}
			return json.Marshal(deps)
		},
	}, nil
}

type requirement struct {
	ProjectName string      `json:"project_name"`
	UnsafeName  string      `json:"unsafe_name"`
	Key         string      `json:"key"`
	Specs       [][2]string `json:"specs"`
	Extras      []string    `json:"extras"`
	RepoURL     string      `json:"repo_url"`
	Packages    []string    `json:"packages"`
	Modules     []string    `json:"modules"`
}

func (p *pythonEnv) Resolve(dep *dep2.RawDependency, c *config.Repository, x *task2.Context) (*dep2.ResolvedTarget, error) {
	switch dep.TargetType {
	case pythonRequirementTargetType:
		var req requirement
		reqJson, _ := json.Marshal(dep.Target)
		json.Unmarshal(reqJson, &req)

		toUnit := &FauxPackage{}
		return &dep2.ResolvedTarget{
			ToRepoCloneURL: req.RepoURL,
			ToUnit:         toUnit.Name(),
			ToUnitType:     unit.Type(toUnit),
		}, nil
	default:
		return nil, fmt.Errorf("Unexpected target type for Python: %+v", dep.TargetType)
	}
}

func (l *pythonEnv) depDockerfile() ([]byte, error) {
	var buf bytes.Buffer
	template.Must(template.New("").Parse(depDockerfile)).Execute(&buf, l)
	return buf.Bytes(), nil
}

const pythonRequirementTargetType = "python-requirement"
const depDockerfile = `FROM ubuntu:13.10
RUN apt-get update
RUN apt-get install -qy curl
RUN apt-get install -qy git
RUN apt-get install -qy {{.PythonVersion}}
RUN ln -s $(which {{.PythonVersion}}) /usr/bin/python
RUN apt-get install -qy python-pip

RUN pip install git+git://github.com/sourcegraph/pydep@{{.PydepVersion}}
`
<commit_msg>use ubuntu 14.04 to speed up docker (fewer distinct base images)<commit_after>package python

import (
	"bytes"
	"encoding/json"
	"fmt"
	"text/template"

	"sourcegraph.com/sourcegraph/srcgraph/config"
	"sourcegraph.com/sourcegraph/srcgraph/container"
	"sourcegraph.com/sourcegraph/srcgraph/dep2"
	"sourcegraph.com/sourcegraph/srcgraph/task2"
	"sourcegraph.com/sourcegraph/srcgraph/unit"
)

func init() {
	dep2.RegisterLister(&FauxPackage{}, dep2.DockerLister{defaultPythonEnv})
	dep2.RegisterResolver(pythonRequirementTargetType, defaultPythonEnv)
}

func (p *pythonEnv) BuildLister(dir string, unit unit.SourceUnit, c *config.Repository, x *task2.Context) (*container.Command, error) {
	dockerfile, err := p.depDockerfile()
	if err != nil {
		return nil, err
	}

	return &container.Command{
		Container: container.Container{
			Dockerfile: dockerfile,
			RunOptions: []string{"-v", dir + ":" + srcRoot},
			Cmd:        []string{"pydep-run.py", srcRoot},
			Stderr:     x.Stderr,
			Stdout:     x.Stdout,
		},
		Transform: func(orig []byte) ([]byte, error) {
			var reqs []requirement
			err := json.NewDecoder(bytes.NewReader(orig)).Decode(&reqs)
			if err != nil {
				return nil, err
			}
			deps := make([]*dep2.RawDependency, len(reqs))
			for i, req := range reqs {
				deps[i] = &dep2.RawDependency{
					TargetType: pythonRequirementTargetType,
					Target:     req,
				}
			}
			return json.Marshal(deps)
		},
	}, nil
}

type requirement struct {
	ProjectName string      `json:"project_name"`
	UnsafeName  string      `json:"unsafe_name"`
	Key         string      `json:"key"`
	Specs       [][2]string `json:"specs"`
	Extras      []string    `json:"extras"`
	RepoURL     string      `json:"repo_url"`
	Packages    []string    `json:"packages"`
	Modules     []string    `json:"modules"`
}

func (p *pythonEnv) Resolve(dep *dep2.RawDependency, c *config.Repository, x *task2.Context) (*dep2.ResolvedTarget, error) {
	switch dep.TargetType {
	case pythonRequirementTargetType:
		var req requirement
		reqJson, _ := json.Marshal(dep.Target)
		json.Unmarshal(reqJson, &req)

		toUnit := &FauxPackage{}
		return &dep2.ResolvedTarget{
			ToRepoCloneURL: req.RepoURL,
			ToUnit:         toUnit.Name(),
			ToUnitType:     unit.Type(toUnit),
		}, nil
	default:
		return nil, fmt.Errorf("Unexpected target type for Python: %+v", dep.TargetType)
	}
}

func (l *pythonEnv) depDockerfile() ([]byte, error) {
	var buf bytes.Buffer
	template.Must(template.New("").Parse(depDockerfile)).Execute(&buf, l)
	return buf.Bytes(), nil
}

const pythonRequirementTargetType = "python-requirement"
const depDockerfile = `FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -qy curl
RUN apt-get install -qy git
RUN apt-get install -qy {{.PythonVersion}}
RUN ln -s $(which {{.PythonVersion}}) /usr/bin/python
RUN apt-get install -qy python-pip

RUN pip install git+git://github.com/sourcegraph/pydep@{{.PydepVersion}}
`
<|endoftext|>


<commit_before>package db

import (
	"database/sql"
	"fmt"
	"log"

	// Not referenced but needed for database/sql
	_ "github.com/lib/pq"
)

// DB configured or nil if not
var DB *sql.DB

// InitDB start DB connection
func InitDB(dbHost string, dbPort int, dbUser string, dbPass string, dbName string) {
	database, err := sql.Open(`postgres`, fmt.Sprintf(`host=%s port=%d user=%s password=%s dbname=%s sslmode=disable`, dbHost, dbPort, dbUser, dbPass, dbName))
	if err != nil {
		log.Fatal(err)
	}

	err = database.Ping()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf(`Connected to %s database`, dbName)
	DB = database
}

// GetTx return given transaction if not nil or create a new one
func GetTx(tx *sql.Tx) (*sql.Tx, error) {
	if tx == nil {
		return DB.Begin()
	}

	return tx, nil
}

// EndTx end transaction properly according to error
func EndTx(tx *sql.Tx, err error) {
	if err != nil {
		usedTx.Rollback()
	} else {
		usedTx.Commit()
	}
}
<commit_msg>Update db.go<commit_after>package db

import (
	"database/sql"
	"fmt"
	"log"

	// Not referenced but needed for database/sql
	_ "github.com/lib/pq"
)

// DB configured or nil if not
var DB *sql.DB

// InitDB start DB connection
func InitDB(dbHost string, dbPort int, dbUser string, dbPass string, dbName string) {
	database, err := sql.Open(`postgres`, fmt.Sprintf(`host=%s port=%d user=%s password=%s dbname=%s sslmode=disable`, dbHost, dbPort, dbUser, dbPass, dbName))
	if err != nil {
		log.Fatal(err)
	}

	err = database.Ping()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf(`Connected to %s database`, dbName)
	DB = database
}

// GetTx return given transaction if not nil or create a new one
func GetTx(tx *sql.Tx) (*sql.Tx, error) {
	if tx == nil {
		return DB.Begin()
	}

	return tx, nil
}

// EndTx end transaction properly according to error
func EndTx(tx *sql.Tx, err error) {
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}
}
<|endoftext|>


<commit_before>package couchbase

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type ViewDefinition struct {
	Map    string `json:"map"`
	Reduce string `json:"reduce"`
}

type DDocJSON struct {
	Language string                    `json:"language"`
	Views    map[string]ViewDefinition `json:"views"`
}

type DDoc struct {
	Meta map[string]interface{} `json:"meta"`
	Json DDocJSON               `json:"json"`
}

type DDocRow struct {
	DDoc DDoc `json:"doc"`
}

type DDocsResult struct {
	Rows []DDocRow `json:"rows"`
}

// Get the design documents
func (b *Bucket) GetDDocs() (DDocsResult, error) {
	var ddocsResult DDocsResult
	err := b.pool.client.parseURLResponse(b.DDocs.URI, &ddocsResult)
	if err != nil {
		return DDocsResult{}, err
	}
	return ddocsResult, nil
}

func (b *Bucket) ddocURL(docname string) (string, error) {
	u, err := b.randomBaseURL()
	if err != nil {
		return "", err
	}
	u.Path = fmt.Sprintf("/%s/_design/%s", b.Name, docname)
	return u.String(), nil
}

// Install a design document.
func (b *Bucket) PutDDoc(docname string, value interface{}) error {
	ddocU, err := b.ddocURL(docname)
	if err != nil {
		return err
	}

	j, err := json.Marshal(value)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("PUT", ddocU, bytes.NewReader(j))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")
	maybeAddAuth(req, b.authHandler())

	res, err := HttpClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	if res.StatusCode != 201 {
		body, _ := ioutil.ReadAll(res.Body)
		return fmt.Errorf("Error installing view: %v / %s",
			res.Status, body)
	}

	return nil
}

// Get a design doc.
func (b *Bucket) GetDDoc(docname string, into interface{}) error {
	ddocU, err := b.ddocURL(docname)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("GET", ddocU, nil)
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")
	maybeAddAuth(req, b.authHandler())

	res, err := HttpClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		body, _ := ioutil.ReadAll(res.Body)
		return fmt.Errorf("Error installing view: %v / %s",
			res.Status, body)
	}

	d := json.NewDecoder(res.Body)
	return d.Decode(into)
}
<commit_msg>Fix error message<commit_after>package couchbase

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type ViewDefinition struct {
	Map    string `json:"map"`
	Reduce string `json:"reduce"`
}

type DDocJSON struct {
	Language string                    `json:"language"`
	Views    map[string]ViewDefinition `json:"views"`
}

type DDoc struct {
	Meta map[string]interface{} `json:"meta"`
	Json DDocJSON               `json:"json"`
}

type DDocRow struct {
	DDoc DDoc `json:"doc"`
}

type DDocsResult struct {
	Rows []DDocRow `json:"rows"`
}

// Get the design documents
func (b *Bucket) GetDDocs() (DDocsResult, error) {
	var ddocsResult DDocsResult
	err := b.pool.client.parseURLResponse(b.DDocs.URI, &ddocsResult)
	if err != nil {
		return DDocsResult{}, err
	}
	return ddocsResult, nil
}

func (b *Bucket) ddocURL(docname string) (string, error) {
	u, err := b.randomBaseURL()
	if err != nil {
		return "", err
	}
	u.Path = fmt.Sprintf("/%s/_design/%s", b.Name, docname)
	return u.String(), nil
}

// Install a design document.
func (b *Bucket) PutDDoc(docname string, value interface{}) error {
	ddocU, err := b.ddocURL(docname)
	if err != nil {
		return err
	}

	j, err := json.Marshal(value)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("PUT", ddocU, bytes.NewReader(j))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")
	maybeAddAuth(req, b.authHandler())

	res, err := HttpClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	if res.StatusCode != 201 {
		body, _ := ioutil.ReadAll(res.Body)
		return fmt.Errorf("Error installing view: %v / %s",
			res.Status, body)
	}

	return nil
}

// Get a design doc.
func (b *Bucket) GetDDoc(docname string, into interface{}) error {
	ddocU, err := b.ddocURL(docname)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("GET", ddocU, nil)
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")
	maybeAddAuth(req, b.authHandler())

	res, err := HttpClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		body, _ := ioutil.ReadAll(res.Body)
		return fmt.Errorf("Error reading view: %v / %s",
			res.Status, body)
	}

	d := json.NewDecoder(res.Body)
	return d.Decode(into)
}
<|endoftext|>


<commit_before>package pi

import (
	"fmt"
	"io"
	"os"
)

var (
	debugMode   = false
	debugOutput = io.Writer(os.Stdout)
)

// SetDebug sets the debugMode, logging every requests and pretty prints JSON and XML.
func SetDebug(debug bool) {
	debugMode = debug
}

// SetDebugOutput sets where we need to write the output of the debug.
// By default, it is the standard output.
func SetDebugOutput(writer io.Writer) {
	debugOutput = writer
}

// writeDebug writes debug string formatted as: [GET] to [http://localhost] debug_message.
func writeDebug(method, remoteAddr, output string) {
	fmt.Fprintf(debugOutput, "[%s] to [%s] %s", method, remoteAddr, output)
}
<commit_msg>Changed write debug<commit_after>package pi

import (
	"fmt"
	"io"
	"os"
	"net"
)

var (
	debugMode   = false
	debugOutput = io.Writer(os.Stdout)
)

// SetDebug sets the debugMode, logging every requests and pretty prints JSON and XML.
func SetDebug(debug bool) {
	debugMode = debug
}

// SetDebugOutput sets where we need to write the output of the debug.
// By default, it is the standard output.
func SetDebugOutput(writer io.Writer) {
	debugOutput = writer
}

// writeDebug writes debug string formatted as: [GET] to/from [IP address] debug_message.
// If you are working on localhost and your machine is using IPV6 addresses, you'll get ::1.
func writeDebug(method, remoteAddr, output string) {
	ip, _, _ := net.SplitHostPort(remoteAddr)
	fmt.Fprintf(debugOutput, "[%s] to/from [%s] %s", method, ip, output)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"reflect"
	"runtime"

	"github.com/bcicen/ctop/container"
	ui "github.com/gizak/termui"
)

var mstats = &runtime.MemStats{}

func logEvent(e ui.Event) {
	var s string
	s += fmt.Sprintf("Type=%s", quote(e.Type))
	s += fmt.Sprintf(" Path=%s", quote(e.Path))
	s += fmt.Sprintf(" From=%s", quote(e.From))
	if e.To != "" {
		s += fmt.Sprintf(" To=%s", quote(e.To))
	}
	log.Debugf("new event: %s", s)
}

func runtimeStats() {
	var msg string
	msg += fmt.Sprintf("cgo calls=%v", runtime.NumCgoCall())
	msg += fmt.Sprintf(" routines=%v", runtime.NumGoroutine())
	runtime.ReadMemStats(mstats)
	msg += fmt.Sprintf(" numgc=%v", mstats.NumGC)
	msg += fmt.Sprintf(" alloc=%v", mstats.Alloc)
	log.Debugf("runtime: %v", msg)
}

func runtimeStack() {
	buf := make([]byte, 32768)
	buf = buf[:runtime.Stack(buf, true)]
	log.Infof(fmt.Sprintf("stack:\n%v", string(buf)))
}

// log container, metrics, and widget state
func dumpContainer(c *container.Container) {
	msg := fmt.Sprintf("logging state for container: %s\n", c.Id)
	for k, v := range c.Meta {
		msg += fmt.Sprintf("Meta.%s = %s\n", k, v)
	}
	msg += inspect(&c.Metrics)
	log.Infof(msg)
}

func inspect(i interface{}) (s string) {
	val := reflect.ValueOf(i)
	elem := val.Type().Elem()

	eName := elem.String()
	for i := 0; i < elem.NumField(); i++ {
		field := elem.Field(i)
		fieldVal := reflect.Indirect(val).FieldByName(field.Name)
		s += fmt.Sprintf("%s.%s = ", eName, field.Name)
		s += fmt.Sprintf("%v (%s)\n", fieldVal, field.Type)
	}
	return s
}

func quote(s string) string {
	return fmt.Sprintf("\"%s\"", s)
}
<commit_msg>logging: skip timer events e.g. /timer/1s<commit_after>package main

import (
	"fmt"
	"reflect"
	"runtime"

	"github.com/bcicen/ctop/container"
	ui "github.com/gizak/termui"
)

var mstats = &runtime.MemStats{}

func logEvent(e ui.Event) {
	// skip timer events e.g. /timer/1s
	if e.From == "timer" {
		return
	}
	var s string
	s += fmt.Sprintf("Type=%s", quote(e.Type))
	s += fmt.Sprintf(" Path=%s", quote(e.Path))
	s += fmt.Sprintf(" From=%s", quote(e.From))
	if e.To != "" {
		s += fmt.Sprintf(" To=%s", quote(e.To))
	}
	log.Debugf("new event: %s", s)
}

func runtimeStats() {
	var msg string
	msg += fmt.Sprintf("cgo calls=%v", runtime.NumCgoCall())
	msg += fmt.Sprintf(" routines=%v", runtime.NumGoroutine())
	runtime.ReadMemStats(mstats)
	msg += fmt.Sprintf(" numgc=%v", mstats.NumGC)
	msg += fmt.Sprintf(" alloc=%v", mstats.Alloc)
	log.Debugf("runtime: %v", msg)
}

func runtimeStack() {
	buf := make([]byte, 32768)
	buf = buf[:runtime.Stack(buf, true)]
	log.Infof(fmt.Sprintf("stack:\n%v", string(buf)))
}

// log container, metrics, and widget state
func dumpContainer(c *container.Container) {
	msg := fmt.Sprintf("logging state for container: %s\n", c.Id)
	for k, v := range c.Meta {
		msg += fmt.Sprintf("Meta.%s = %s\n", k, v)
	}
	msg += inspect(&c.Metrics)
	log.Infof(msg)
}

func inspect(i interface{}) (s string) {
	val := reflect.ValueOf(i)
	elem := val.Type().Elem()

	eName := elem.String()
	for i := 0; i < elem.NumField(); i++ {
		field := elem.Field(i)
		fieldVal := reflect.Indirect(val).FieldByName(field.Name)
		s += fmt.Sprintf("%s.%s = ", eName, field.Name)
		s += fmt.Sprintf("%v (%s)\n", fieldVal, field.Type)
	}
	return s
}

func quote(s string) string {
	return fmt.Sprintf("\"%s\"", s)
}
<|endoftext|>


<commit_before>package main

import ()

type dnsRec struct {
	TTL int
	value string
	String() string
}

type aRec struct {
	dnsRec
}

type cnameRec struct {
	dnsRec
}

type mxRec struct {
	dnsRec
	priority int
}

type txtRec struct {
	dnsRec
}
<commit_msg>making sure everything for dns zone types got in there<commit_after>package main

import ()

type dnsRec struct {
	TTL int
	value string
	String() string
}

type aRec struct {
	dnsRec
}

type cnameRec struct {
	dnsRec
}

type mxRec struct {
	dnsRec
	priority int
}

type txtRec struct {
	dnsRec
}

type nsRec struct {
	cnameRec
}

type ptrRec struct {
	aRec
}

type soaRec struct {

}

type fqdn struct {
	parentPart string
	localPart string
	records []dnsRec
	subdomains []fqdn
}

type zone struct {
	soa soaRec
	defaultTTL int
	tld fqdn
}<|endoftext|>


<commit_before>package guber

// Common
//==============================================================================
type ResourceDefinition struct {
	Kind       string `json:"kind"`
	ApiVersion string `json:"apiVersion"`
}

type Metadata struct {
	Name              string            `json:"name"`
	Namespace         string            `json:"namespace"`
	Labels            map[string]string `json:"labels,omitempty"`
	CreationTimestamp string            `json:"creationTimestamp,omitempty"`
}

// Namespace
//==============================================================================
type Namespace struct {
	collection *Namespaces
	*ResourceDefinition

	Metadata *Metadata `json:"metadata"`
}

type NamespaceList struct {
	Items []*Namespace `json:"items"`
}

// Node
//==============================================================================
type NodeSpec struct {
	ExternalID string `json:"externalID"`
}

type NodeStatusCapacity struct {
	CPU    string `json:"cpu"`
	Memory string `json:"memory"`
}

type NodeStatusCondition struct {
	Type   string `json:"type"`
	Status string `json:"status"`
}

type NodeAddress struct {
	Type    string `json:"type"`
	Address string `json:"address"`
}

type NodeStatus struct {
	Capacity   *NodeStatusCapacity    `json:"capacity"`
	Conditions []*NodeStatusCondition `json:"conditions"`
	Addresses  []*NodeAddress         `json:"addresses"`
}

type Node struct {
	collection *Nodes
	*ResourceDefinition

	Metadata *Metadata   `json:"metadata"`
	Spec     *NodeSpec   `json:"spec"`
	Status   *NodeStatus `json:"status"`
}

type NodeList struct {
	Items []*Node `json:"items"`
}

// ReplicationController
//==============================================================================
type PodTemplate struct {
	Metadata *Metadata `json:"metadata"`
	Spec     *PodSpec  `json:"spec"`
}

type ReplicationControllerSpec struct {
	Selector map[string]string `json:"selector"`
	Replicas int               `json:"replicas"`
	Template *PodTemplate      `json:"template"`
}

type ReplicationControllerStatus struct {
	Replicas int `json:"replicas"`
}

type ReplicationController struct {
	collection *ReplicationControllers
	*ResourceDefinition

	Metadata *Metadata                    `json:"metadata"`
	Spec     *ReplicationControllerSpec   `json:"spec"`
	Status   *ReplicationControllerStatus `json:"status,omitempty"`
}

type ReplicationControllerList struct {
	Items []*ReplicationController `json:"items"`
}

// Pod
//==============================================================================
type AwsElasticBlockStore struct {
	VolumeID string `json:"volumeID"`
	FSType   string `json:"fsType"`
}

type Volume struct {
	Name                 string                `json:"name"`
	AwsElasticBlockStore *AwsElasticBlockStore `json:"awsElasticBlockStore"`
}

type VolumeMount struct {
	Name      string `json:"name"`
	MountPath string `json:"mountPath"`
}

type ResourceValues struct {
	Memory string `json:"memory,omitempty"`
	CPU    string `json:"cpu,omitempty"`
}

type Resources struct {
	Limits   *ResourceValues `json:"limits"`
	Requests *ResourceValues `json:"requests"`
}

type ContainerPort struct {
	Name          string `json:"name,omitempty"`
	ContainerPort int    `json:"containerPort"`
	Protocol      string `json:"protocol,omitempty"`
}

type EnvVar struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type SecurityContext struct {
	Privileged bool `json:"privileged"`
}

type Container struct {
	Name            string           `json:"name"`
	Image           string           `json:"image"`
	Command         []string         `json:"command"`
	Resources       *Resources       `json:"resources"`
	Ports           []*ContainerPort `json:"ports"`
	VolumeMounts    []*VolumeMount   `json:"volumeMounts"`
	Env             []*EnvVar        `json:"env"`
	SecurityContext *SecurityContext `json:"securityContext"`
	ImagePullPolicy string           `json:"imagePullPolicy"`
}

type ImagePullSecret struct {
	Name string `json:"name"`
}

type PodSpec struct {
	Volumes                       []*Volume          `json:"volumes"`
	Containers                    []*Container       `json:"containers"`
	ImagePullSecrets              []*ImagePullSecret `json:"imagePullSecrets"`
	TerminationGracePeriodSeconds int                `json:"terminationGracePeriodSeconds"`
	RestartPolicy                 string             `json:"restartPolicy"`
}

type ContainerStateRunning struct {
	StartedAt string `json:"startedAt"` // TODO should be time type
}

type ContainerStateTerminated struct {
	ExitCode   int    `json:"exitcode"`
	StartedAt  string `json:"startedAt"`  // TODO should be time type
	FinishedAt string `json:"finishedAt"` // TODO should be time type
	Reason     string `json:"reason"`
}

type ContainerState struct {
	Running    *ContainerStateRunning    `json:"running"`
	Terminated *ContainerStateTerminated `json:"terminated"`
}

type ContainerStatus struct {
	ContainerID  string          `json:"containerID"`
	Image        string          `json:"image"`
	ImageID      string          `json:"imageID"`
	Name         string          `json:"name"`
	Ready        bool            `json:"ready"`
	RestartCount int             `json:"restartCount"`
	State        *ContainerState `json:"state"`
	LastState    *ContainerState `json:"state"`
}

type PodStatusCondition struct {
	Type   string `json:"type"`
	Status string `json:"status"`
}

type PodStatus struct {
	Phase             string                `json:"phase"`
	Conditions        []*PodStatusCondition `json:"conditions"`
	ContainerStatuses []*ContainerStatus    `json:"containerStatuses"`
}

type Pod struct {
	collection *Pods
	*ResourceDefinition

	Metadata *Metadata  `json:"metadata"`
	Spec     *PodSpec   `json:"spec"`
	Status   *PodStatus `json:"status"`
}

type PodList struct {
	Items []*Pod `json:"items"`
}

// Service
//==============================================================================
type ServicePort struct {
	Name       string `json:"name"`
	Port       int    `json:"port"`
	Protocol   string `json:"protocol,omitempty"`
	NodePort   int    `json:"nodePort,omitempty"`
	TargetPort int    `json:"targetPort,omitempty"`
}

type ServiceSpec struct {
	Type     string            `json:"type,omitempty"`
	Selector map[string]string `json:"selector"`
	Ports    []*ServicePort    `json:"ports"`

	ClusterIP string `json:"clusterIP,omitempty"`
}

type Service struct {
	collection *Services
	*ResourceDefinition

	Metadata *Metadata    `json:"metadata"`
	Spec     *ServiceSpec `json:"spec"`
	// Status   *ServiceStatus `json:"status"`
}

type ServiceList struct {
	Items []*Service `json:"items"`
}

// Secret
//==============================================================================
type Secret struct {
	collection *Secrets
	*ResourceDefinition

	Metadata *Metadata         `json:"metadata"`
	Type     string            `json:"type"`
	Data     map[string]string `json:"data"`
}

type SecretList struct {
	Items []*Secret `json:"items"`
}

// Event
//==============================================================================
type Source struct {
	Host string `json:"host"`
}

type Event struct {
	collection *Events
	*ResourceDefinition

	Metadata *Metadata `json:"metadata"`
	Message  string    `json:"message"`
	Count    int       `json:"count"`
	Source   *Source   `json:"source"`
}

type EventList struct {
	Items []*Event `json:"items"`
}

// TODO not sure if this should be in the types file.. related to queries, but is a Kube-specific thing
type QueryParams struct {
	LabelSelector string
	FieldSelector string
}

type HeapsterStatMetric struct {
	Average    int `json:"average"`
	Percentile int `json:"percentile"`
	Max        int `json:"max"`
}

type HeapsterStatPeriods struct {
	Minute *HeapsterStatMetric `json:"minute"`
	Hour   *HeapsterStatMetric `json:"hour"`
	Day    *HeapsterStatMetric `json:"day"`
}

type HeapsterStats struct {
	Uptime int `json:"uptime"`
	Stats  map[string]*HeapsterStatPeriods
}
<commit_msg>Changing HeapsterStatMetric values from int to int64<commit_after>package guber

// Common
//==============================================================================
type ResourceDefinition struct {
	Kind       string `json:"kind"`
	ApiVersion string `json:"apiVersion"`
}

type Metadata struct {
	Name              string            `json:"name"`
	Namespace         string            `json:"namespace"`
	Labels            map[string]string `json:"labels,omitempty"`
	CreationTimestamp string            `json:"creationTimestamp,omitempty"`
}

// Namespace
//==============================================================================
type Namespace struct {
	collection *Namespaces
	*ResourceDefinition

	Metadata *Metadata `json:"metadata"`
}

type NamespaceList struct {
	Items []*Namespace `json:"items"`
}

// Node
//==============================================================================
type NodeSpec struct {
	ExternalID string `json:"externalID"`
}

type NodeStatusCapacity struct {
	CPU    string `json:"cpu"`
	Memory string `json:"memory"`
}

type NodeStatusCondition struct {
	Type   string `json:"type"`
	Status string `json:"status"`
}

type NodeAddress struct {
	Type    string `json:"type"`
	Address string `json:"address"`
}

type NodeStatus struct {
	Capacity   *NodeStatusCapacity    `json:"capacity"`
	Conditions []*NodeStatusCondition `json:"conditions"`
	Addresses  []*NodeAddress         `json:"addresses"`
}

type Node struct {
	collection *Nodes
	*ResourceDefinition

	Metadata *Metadata   `json:"metadata"`
	Spec     *NodeSpec   `json:"spec"`
	Status   *NodeStatus `json:"status"`
}

type NodeList struct {
	Items []*Node `json:"items"`
}

// ReplicationController
//==============================================================================
type PodTemplate struct {
	Metadata *Metadata `json:"metadata"`
	Spec     *PodSpec  `json:"spec"`
}

type ReplicationControllerSpec struct {
	Selector map[string]string `json:"selector"`
	Replicas int               `json:"replicas"`
	Template *PodTemplate      `json:"template"`
}

type ReplicationControllerStatus struct {
	Replicas int `json:"replicas"`
}

type ReplicationController struct {
	collection *ReplicationControllers
	*ResourceDefinition

	Metadata *Metadata                    `json:"metadata"`
	Spec     *ReplicationControllerSpec   `json:"spec"`
	Status   *ReplicationControllerStatus `json:"status,omitempty"`
}

type ReplicationControllerList struct {
	Items []*ReplicationController `json:"items"`
}

// Pod
//==============================================================================
type AwsElasticBlockStore struct {
	VolumeID string `json:"volumeID"`
	FSType   string `json:"fsType"`
}

type Volume struct {
	Name                 string                `json:"name"`
	AwsElasticBlockStore *AwsElasticBlockStore `json:"awsElasticBlockStore"`
}

type VolumeMount struct {
	Name      string `json:"name"`
	MountPath string `json:"mountPath"`
}

type ResourceValues struct {
	Memory string `json:"memory,omitempty"`
	CPU    string `json:"cpu,omitempty"`
}

type Resources struct {
	Limits   *ResourceValues `json:"limits"`
	Requests *ResourceValues `json:"requests"`
}

type ContainerPort struct {
	Name          string `json:"name,omitempty"`
	ContainerPort int    `json:"containerPort"`
	Protocol      string `json:"protocol,omitempty"`
}

type EnvVar struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type SecurityContext struct {
	Privileged bool `json:"privileged"`
}

type Container struct {
	Name            string           `json:"name"`
	Image           string           `json:"image"`
	Command         []string         `json:"command"`
	Resources       *Resources       `json:"resources"`
	Ports           []*ContainerPort `json:"ports"`
	VolumeMounts    []*VolumeMount   `json:"volumeMounts"`
	Env             []*EnvVar        `json:"env"`
	SecurityContext *SecurityContext `json:"securityContext"`
	ImagePullPolicy string           `json:"imagePullPolicy"`
}

type ImagePullSecret struct {
	Name string `json:"name"`
}

type PodSpec struct {
	Volumes                       []*Volume          `json:"volumes"`
	Containers                    []*Container       `json:"containers"`
	ImagePullSecrets              []*ImagePullSecret `json:"imagePullSecrets"`
	TerminationGracePeriodSeconds int                `json:"terminationGracePeriodSeconds"`
	RestartPolicy                 string             `json:"restartPolicy"`
}

type ContainerStateRunning struct {
	StartedAt string `json:"startedAt"` // TODO should be time type
}

type ContainerStateTerminated struct {
	ExitCode   int    `json:"exitcode"`
	StartedAt  string `json:"startedAt"`  // TODO should be time type
	FinishedAt string `json:"finishedAt"` // TODO should be time type
	Reason     string `json:"reason"`
}

type ContainerState struct {
	Running    *ContainerStateRunning    `json:"running"`
	Terminated *ContainerStateTerminated `json:"terminated"`
}

type ContainerStatus struct {
	ContainerID  string          `json:"containerID"`
	Image        string          `json:"image"`
	ImageID      string          `json:"imageID"`
	Name         string          `json:"name"`
	Ready        bool            `json:"ready"`
	RestartCount int             `json:"restartCount"`
	State        *ContainerState `json:"state"`
	LastState    *ContainerState `json:"state"`
}

type PodStatusCondition struct {
	Type   string `json:"type"`
	Status string `json:"status"`
}

type PodStatus struct {
	Phase             string                `json:"phase"`
	Conditions        []*PodStatusCondition `json:"conditions"`
	ContainerStatuses []*ContainerStatus    `json:"containerStatuses"`
}

type Pod struct {
	collection *Pods
	*ResourceDefinition

	Metadata *Metadata  `json:"metadata"`
	Spec     *PodSpec   `json:"spec"`
	Status   *PodStatus `json:"status"`
}

type PodList struct {
	Items []*Pod `json:"items"`
}

// Service
//==============================================================================
type ServicePort struct {
	Name       string `json:"name"`
	Port       int    `json:"port"`
	Protocol   string `json:"protocol,omitempty"`
	NodePort   int    `json:"nodePort,omitempty"`
	TargetPort int    `json:"targetPort,omitempty"`
}

type ServiceSpec struct {
	Type     string            `json:"type,omitempty"`
	Selector map[string]string `json:"selector"`
	Ports    []*ServicePort    `json:"ports"`

	ClusterIP string `json:"clusterIP,omitempty"`
}

type Service struct {
	collection *Services
	*ResourceDefinition

	Metadata *Metadata    `json:"metadata"`
	Spec     *ServiceSpec `json:"spec"`
	// Status   *ServiceStatus `json:"status"`
}

type ServiceList struct {
	Items []*Service `json:"items"`
}

// Secret
//==============================================================================
type Secret struct {
	collection *Secrets
	*ResourceDefinition

	Metadata *Metadata         `json:"metadata"`
	Type     string            `json:"type"`
	Data     map[string]string `json:"data"`
}

type SecretList struct {
	Items []*Secret `json:"items"`
}

// Event
//==============================================================================
type Source struct {
	Host string `json:"host"`
}

type Event struct {
	collection *Events
	*ResourceDefinition

	Metadata *Metadata `json:"metadata"`
	Message  string    `json:"message"`
	Count    int       `json:"count"`
	Source   *Source   `json:"source"`
}

type EventList struct {
	Items []*Event `json:"items"`
}

// TODO not sure if this should be in the types file.. related to queries, but is a Kube-specific thing
type QueryParams struct {
	LabelSelector string
	FieldSelector string
}

type HeapsterStatMetric struct {
	Average    int64 `json:"average"`
	Percentile int64 `json:"percentile"`
	Max        int64 `json:"max"`
}

type HeapsterStatPeriods struct {
	Minute *HeapsterStatMetric `json:"minute"`
	Hour   *HeapsterStatMetric `json:"hour"`
	Day    *HeapsterStatMetric `json:"day"`
}

type HeapsterStats struct {
	Uptime int `json:"uptime"`
	Stats  map[string]*HeapsterStatPeriods
}
<|endoftext|>


<commit_before>package torrent

import (
	"strings"

	"log"

	torrentapi "github.com/qopher/go-torrentapi"
)

func filterMovies(torrents torrentapi.TorrentResults) string {
	var moviesextended torrentapi.TorrentResults
	// Search for extended version
	for _, t := range torrents {
		var filename = strings.ToLower(t.Filename)
		if strings.Contains(filename, "extended") {
			moviesextended = append(moviesextended, t)
		}
	}
	log.Println(torrents)
	var results torrentapi.TorrentResults
	results = filteraudioQuality("DTS-HD", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS-HD.MA.7.1", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD.MA.7.1", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("TrueHD.7.1Atmos", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "TrueHD.7.1Atmos", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "DTS", len(results))
	if len(results) > 0 {
		return results[0].Download
	}

	results = filteraudioQuality("DTS-HD", torrents)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS-HD.MA.7.1", torrents)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD.MA.7.1", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("TrueHD.7.1Atmos", torrents)
	//log.Printf("For quality %s the number of result if %d", "TrueHD.7.1Atmos", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS", torrents)
	//log.Printf("For quality %s the number of result if %d", "DTS", len(results))
	if len(results) > 0 {
		return results[0].Download
	}

	return ""

}

func filteraudioQuality(quality string, torrents torrentapi.TorrentResults) torrentapi.TorrentResults {
	var movies torrentapi.TorrentResults
	for _, t := range torrents {
		var filename = strings.ToLower(t.Download)
		quality = strings.ToLower(quality)
		if strings.Contains(filename, quality) {
			movies = append(movies, t)
		}
	}
	return movies
}

func Search(movieIMBDID, quality string) (string, error) {
	api, err := torrentapi.Init()
	if err != nil {
		return "", err
	}
	api.Format("json_extended")
	api.Category(44)
	api.SearchImDB(movieIMBDID)
	results, err := api.Search()
	if err != nil {
		return "", err
	}

	if len(results) == 0 {
		return "", nil
	}
	return filterMovies(results), nil
}
<commit_msg>Re add torrent criteria<commit_after>package torrent

import (
	"strings"

	"log"

	torrentapi "github.com/qopher/go-torrentapi"
)

func filterMovies(torrents torrentapi.TorrentResults) string {
	var moviesextended torrentapi.TorrentResults
	// Search for extended version
	for _, t := range torrents {
		var filename = strings.ToLower(t.Filename)
		if strings.Contains(filename, "extended") {
			moviesextended = append(moviesextended, t)
		}
	}
	log.Println(torrents)
	var results torrentapi.TorrentResults
	results = filteraudioQuality("DTS-HD", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS-HD.MA.7.1", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD.MA.7.1", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("TrueHD.7.1Atmos", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "TrueHD.7.1Atmos", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS", moviesextended)
	//log.Printf("For quality %s the number of result if %d", "DTS", len(results))
	if len(results) > 0 {
		return results[0].Download
	}

	results = filteraudioQuality("DTS-HD", torrents)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS-HD.MA.7.1", torrents)
	//log.Printf("For quality %s the number of result if %d", "DTS-HD.MA.7.1", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("TrueHD.7.1Atmos", torrents)
	//log.Printf("For quality %s the number of result if %d", "TrueHD.7.1Atmos", len(results))
	if len(results) > 0 {
		return results[0].Download
	}
	results = filteraudioQuality("DTS", torrents)
	//log.Printf("For quality %s the number of result if %d", "DTS", len(results))
	if len(results) > 0 {
		return results[0].Download
	}

	return ""

}

func filteraudioQuality(quality string, torrents torrentapi.TorrentResults) torrentapi.TorrentResults {
	var movies torrentapi.TorrentResults
	for _, t := range torrents {
		var filename = strings.ToLower(t.Download)
		quality = strings.ToLower(quality)
		if strings.Contains(filename, quality) && t.Seeders > 0 {
			movies = append(movies, t)
		}
	}
	return movies
}

func Search(movieIMBDID, quality string) (string, error) {
	api, err := torrentapi.Init()
	if err != nil {
		return "", err
	}
	api.Format("json_extended")
	api.Category(44)
	api.SearchImDB(movieIMBDID)
	results, err := api.Search()
	if err != nil {
		return "", err
	}

	if len(results) == 0 {
		return "", nil
	}
	return filterMovies(results), nil
}
<|endoftext|>


<commit_before>package gondole

type Gondole struct {

}

type Account struct {

}

type Client struct {

}

type Status struct {

}

<commit_msg>Begin to fill in types.<commit_after>package gondole

import "time"

type Gondole struct {
	Name string
    RedirectURI string
}

type Account struct {
	ID         int
	Acct       string
	Avatar     string
	Followers  int
	Followings int
	Header     string
	Note       string
	Statuses   int
	URL        string
	Username   string
}

type Client struct {
	BaseURL     string
	BearerToken string
}

type Status struct {
	ID         int
	Account    *Account
	Content    string
	CreatedAT  time.Time
	Favourited bool
	Favourites int
	InReplyTo  int
	Reblog     *Status
	Reblogged  bool
	Reblogs    int
	URI        string
	URL        string
}
<|endoftext|>