//**************************************** 初始化 *******************************************\\
502721
func GetContext(sp *Spider, req *request.Request) *Context {
	ctx := contextPool.Get().(*Context)
	ctx.spider = sp
	ctx.Request = req
	return ctx
}
1023774
func (self *Context) Output(item interface{}, ruleName ...string) {
	_ruleName, rule, found := self.getRule(ruleName...)
	if !found {
		logs.Log.Error("蜘蛛 %s 调用Output()时，指定的规则名不存在！", self.spider.GetName())
		return
	}
	var _item map[string]interface{}
	switch item2 := item.(type) {
	case map[int]interface{}:
		_item = self.CreatItem(item2, _ruleName)
	case request.Temp:
		for k := range item2 {
			self.spider.UpsertItemField(rule, k)
		}
		_item = item2
	case map[string]interface{}:
		for k := range item2 {
			self.spider.UpsertItemField(rule, k)
		}
		_item = item2
	}
	self.Lock()
	if self.spider.NotDefaultField {
		self.items = append(self.items, data.GetDataCell(_ruleName, _item, "", "", ""))
	} else {
		self.items = append(self.items, data.GetDataCell(_ruleName, _item, self.GetUrl(), self.GetReferer(), time.Now().Format("2006-01-02 15:04:05")))
	}
	self.Unlock()
}
1069345
func Quote(l []string) []string {
	var quoted []string

	for _, s := range l {
		quoted = append(quoted, fmt.Sprintf("%q", s))
	}

	return quoted
}
840474
func (self *Logic) Init(mode int, port int, master string, w ...io.Writer) App {
	self.canSocketLog = false
	if len(w) > 0 {
		self.SetLog(w[0])
	}
	self.LogGoOn()

	self.AppConf.Mode, self.AppConf.Port, self.AppConf.Master = mode, port, master
	self.Teleport = teleport.New()
	self.TaskJar = distribute.NewTaskJar()
	self.SpiderQueue = crawler.NewSpiderQueue()
	self.CrawlerPool = crawler.NewCrawlerPool()

	switch self.AppConf.Mode {
	case status.SERVER:
		logs.Log.EnableStealOne(false)
		if self.checkPort() {
			logs.Log.Informational("                                                                                               ！！当前运行模式为：[ 服务器 ] 模式！！")
			self.Teleport.SetAPI(distribute.MasterApi(self)).Server(":" + strconv.Itoa(self.AppConf.Port))
		}

	case status.CLIENT:
		if self.checkAll() {
			logs.Log.Informational("                                                                                               ！！当前运行模式为：[ 客户端 ] 模式！！")
			self.Teleport.SetAPI(distribute.SlaveApi(self)).Client(self.AppConf.Master, ":"+strconv.Itoa(self.AppConf.Port))
			// 开启节点间log打印
			self.canSocketLog = true
			logs.Log.EnableStealOne(true)
			go self.socketLog()
		}
	case status.OFFLINE:
		logs.Log.EnableStealOne(false)
		logs.Log.Informational("                                                                                               ！！当前运行模式为：[ 单机 ] 模式！！")
		return self
	default:
		logs.Log.Warning(" *    ——请指定正确的运行模式！——")
		return self
	}
	return self
}
787593
func HTML2JS(data []byte) []byte {
	s := string(data)
	s = strings.Replace(s, `\`, `\\`, -1)
	s = strings.Replace(s, "\n", `\n`, -1)
	s = strings.Replace(s, "\r", "", -1)
	s = strings.Replace(s, "\"", `\"`, -1)
	s = strings.Replace(s, "<table>", "&lt;table>", -1)
	return []byte(s)
}
574324
func (c *Crontab) Start() {
	if c.isTicker {
		c.ticker = time.NewTicker(c.tickerDuration)
		for {
			<-c.ticker.C
			execHandlers(c.handlers...)
		}
	} else {
		for {
			now := time.Now()
			// s, m, d, M|W
			if c.minutes.Size() == 0 { //只有秒
				if c.seconds.Contains(now.Second()) {
					execHandlers(c.handlers...)
				}
			} else if c.hours.Size() == 0 { //有秒，分
				if c.seconds.Contains(now.Second()) && c.minutes.Contains(now.Minute()) {
					execHandlers(c.handlers...)
				}
			} else if c.days.Size() == 0 { //有秒，分, 时
				if c.seconds.Contains(now.Second()) && c.minutes.Contains(now.Minute()) && c.hours.Contains(now.Hour()) {
					execHandlers(c.handlers...)
				}
			} else if c.monthes.Size() == 0 && c.weekdays.Size() == 0 { //有秒，分, 时,天
				if c.seconds.Contains(now.Second()) && c.minutes.Contains(now.Minute()) && c.hours.Contains(now.Hour()) && c.days.Contains(now.Day()) {
					execHandlers(c.handlers...)
				}
			} else {
				if c.seconds.Contains(now.Second()) && c.minutes.Contains(now.Minute()) && c.hours.Contains(now.Hour()) && c.days.Contains(now.Day()) && (c.monthes.Contains(now.Month()) || c.weekdays.Contains(now.Weekday())) {
					execHandlers(c.handlers...)
				}
			}

			time.Sleep(time.Second)
		}
	}
}
1273529
func t(tags []string) string {
	for i, v := range tags {
		tags[i] = strings.Replace(v, ":", "=", 1)
	}
	t := ""
	if len(tags) > 0 {
		t = "" + strings.Join(tags, ",")
	}
	return t
}
863769
func main() {
	pubpipe := make(chan string)
	subpipe := make(chan string)
	go publisher(pubpipe)
	go subscriber(subpipe)
	<-subpipe
	pubpipe <- "break"
	time.Sleep(100 * time.Millisecond)
}
218281
func ci(s string) bson.RegEx {
	return bson.RegEx{Pattern: fmt.Sprintf("^%s$", regexp.QuoteMeta(s)), Options: "i"}
}
491047
func GetTiebaFid(tiebaName string) (fid string, err error) {
	resp, err := requester.DefaultClient.Req("GET", "http://tieba.baidu.com/f/commit/share/fnameShareApi?ie=utf-8&fname="+tiebaName, nil, nil)
	if err != nil {
		return "", fmt.Errorf("获取贴吧fid网络错误, %s", err)
	}

	defer resp.Body.Close()

	json, err := simplejson.NewFromReader(resp.Body)
	if err != nil {
		return "", fmt.Errorf("获取贴吧fid json解析错误, %s", err)
	}

	intFid := json.GetPath("data", "fid").MustInt()
	return fmt.Sprint(intFid), nil
}
951325
func (self *Logic) server() {
	// 标记结束
	defer func() {
		self.finishOnce.Do(func() { close(self.finish) })
	}()

	// 便利添加任务到库
	tasksNum, spidersNum := self.addNewTask()

	if tasksNum == 0 {
		return
	}

	// 打印报告
	logs.Log.Informational(" * ")
	logs.Log.Informational(` *********************************************************************************************************************************** `)
	logs.Log.Informational(" * ")
	logs.Log.Informational(" *                               —— 本次成功添加 %v 条任务，共包含 %v 条采集规则 ——", tasksNum, spidersNum)
	logs.Log.Informational(" * ")
	logs.Log.Informational(` *********************************************************************************************************************************** `)
}
658443
func init() {
	var err error
	validTagRe, err = regexp.Compile(`^([a-z][a-z0-9]*(\-[a-z0-9]+)*)(:([a-z0-9]+(\-[a-z0-9]+)*))*$`)
	if err != nil {
		panic(err)
	}
}
302255
func Error(t testing.TB, expr interface{}, args ...interface{}) {
	if IsNil(expr) { // 空值，必定没有错误
		assert(t, false, args, []interface{}{"Error 失败，实际值为 Nil：[%T]", expr})
		return
	}

	_, ok := expr.(error)
	assert(t, ok, args, []interface{}{"Error 失败，实际类型为[%T]", expr})
}
1010937
func fieldsASCII(s string) []string {
	fn := func(r rune) bool {
		switch r {
		case '\t', '\n', '\f', '\r', ' ':
			return true
		}
		return false
	}
	return strings.FieldsFunc(s, fn)
}
227197
func init() {
	for k, v := range ChanModeToString {
		StringToChanMode[v] = k
	}
	for k, v := range ChanPrivToString {
		StringToChanPriv[v] = k
	}
	for k, v := range ChanPrivToModeChar {
		ModeCharToChanPriv[v] = k
	}
}
260617
func _() {
	var _ Event = (*Absolute)(nil)        // assert *Absolute implements Event
	var _ Event = (*FAbsolute)(nil)       // assert *FAbsolute implements Event
	var _ Event = (*Gauge)(nil)           // assert *Gauge implements Event
	var _ Event = (*FGauge)(nil)          // assert *FGauge implements Event
	var _ Event = (*GaugeDelta)(nil)      // assert *GaugeDelta implements Event
	var _ Event = (*FGaugeDelta)(nil)     // assert *FGaugeDelta implements Event
	var _ Event = (*Increment)(nil)       // assert *Increment implements Event
	var _ Event = (*PrecisionTiming)(nil) // assert *PrecisionTiming implements Event
	var _ Event = (*Timing)(nil)          // assert *Timing implements Event
	var _ Event = (*Total)(nil)           // assert *Total implements Event
}
309923
func Html2JS(data []byte) []byte {
	s := string(data)
	s = strings.Replace(s, `\`, `\\`, -1)
	s = strings.Replace(s, "\n", `\n`, -1)
	s = strings.Replace(s, "\r", "", -1)
	s = strings.Replace(s, "\"", `\"`, -1)
	s = strings.Replace(s, "<table>", "&lt;table>", -1)
	return []byte(s)
}