Exemple #1
0
// 显示页
func (this *IndexController) View() {
	// 获取InfoHash
	infohash := this.Ctx.Input.Param(":infohash")

	// 将infohash转换为大写
	infohash = strings.ToUpper(infohash)

	// 定义一个SC_Info
	var scinfo models.SC_Info

	// 获取种子信息
	models.GetOneByQuery(models.DbInfo, bson.M{"infohash": infohash}, &scinfo)

	if scinfo.InfoHash == "" {
		// 如果infohash为空或小于40则报错
		this.Abort("404")
	}

	// 设置种子标题
	this.Data["Caption"] = scinfo.Caption

	// 定义两个SC_Info列表
	var hots []models.SC_Info

	// 获取热门种子列表
	models.GetDataByQuery(models.DbInfo, 0, 5, "-hot", nil, &hots)
	// 设置热门列表
	this.Data["HotList"] = hots

	// 自增下载次数
	models.SetAdd(models.DbInfo, bson.M{"infohash": infohash}, "views", true)

	// 设置创建时间
	this.Data["CreateTime"] = scinfo.CreateTime
	// 设置入库时间
	this.Data["PutTime"] = scinfo.PutTime
	// 设置文件大小
	this.Data["Length"] = scinfo.Length
	// 设置关键词
	this.Data["Keys"] = scinfo.Keys
	// 设置种子热度
	this.Data["Hot"] = scinfo.Hot
	// 设置文件数量
	this.Data["FileCount"] = scinfo.FileCount
	// 设置InfoHash
	this.Data["InfoHash"] = scinfo.InfoHash
	// 设置文件列表
	this.Data["FileList"] = scinfo.FileList
	// 设置下载链接
	this.Data["Down"] = fmt.Sprintf("http://btcache.me/torrent/%s", scinfo.InfoHash)

	// 二维码文件是否存在
	if _, err := os.Stat("/static/qrcode/" + scinfo.InfoHash[0:1] + "/" + scinfo.InfoHash[1:2] + "/" + scinfo.InfoHash[2:3] + "/" + scinfo.InfoHash[3:4] + "/" + scinfo.InfoHash[4:5] + "/" + scinfo.InfoHash[5:6] + "/" + scinfo.InfoHash[6:7] + "/" + scinfo.InfoHash + ".png"); err != nil {
		// 存在则设置二维码图片
		this.Data["Qrcode"] = "/static/qrcode/" + scinfo.InfoHash[0:1] + "/" + scinfo.InfoHash[1:2] + "/" + scinfo.InfoHash[2:3] + "/" + scinfo.InfoHash[3:4] + "/" + scinfo.InfoHash[4:5] + "/" + scinfo.InfoHash[5:6] + "/" + scinfo.InfoHash[6:7] + "/" + scinfo.InfoHash + ".png"
	}

	// 自增查看次数
	models.SaveLog(time.Now().Format("20060102"), "viewnums")

	// 输出模板
	this.TplNames = "view.html"
}
Exemple #2
0
// 种子入库操作
func PullTorrent(hash string) (int, error) {
	// 定义一个int变量t
	t := 0

	// 无限循环进行入库
	for {
		// 定义url和host变量
		var url, host string
		// 将infohash转换为大写格式
		hash = strings.ToUpper(hash)

		// 查看t为多少
		switch t {
		case 0:
			// 为0则使用torrent-cache.bitcomet.org下载
			url = fmt.Sprintf("http://torrent-cache.bitcomet.org:36869/get_torrent?info_hash=%s&size=226920869&key=%s", strings.ToLower(hash), GetKey(hash))
			host = "torrent-cache.bitcomet.org"
			break
		case 1:
			// 为1则使用bt.box.n0808.com下载
			url = fmt.Sprintf("http://bt.box.n0808.com/%s/%s/%s.torrent", hash[0:2], hash[len(hash)-2:], hash)
			host = "bt.box.n0808.com"
			break
		case 2:
			// 为2则使用torcache.net下载
			url = fmt.Sprintf("https://torcache.net/torrent/%s.torrent", hash)
			host = "torcache.net"
			break
		default:
			// 对infohash进行自增处理
			models.SetAdd(models.DbHash, bson.M{"infohash": hash}, "invalid", true)
			return 1, nil
		}

		// t自增
		t++

		// 新建请求
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			// 失败则跳过本次循环
			continue
		}

		// 设置头部信息
		req.Header.Add("User-Agent", "Mozilla/5.0")
		req.Header.Add("Host", host)
		req.Header.Add("Accept", "*/*")
		req.Header.Add("Connection", "Keep-Alive")

		// 设置超时时间
		client := &http.Client{
			Transport: &http.Transport{
				Dial: func(netw, addr string) (net.Conn, error) {
					deadline := time.Now().Add(3 * time.Second)
					c, err := net.DialTimeout(netw, addr, time.Second*3)
					if err != nil {
						return nil, err
					}
					c.SetDeadline(deadline)
					return c, nil
				},
			},
		}

		// 请求链接
		resp, err := client.Do(req)
		if err != nil {
			// 失败则跳过本次循环
			continue
		}
		// 保证关闭
		defer resp.Body.Close()

		// 定义一个MetaInfo
		var metaTorrent MetaInfo
		// 读取种子信息
		metaTorrent, err = ReadTorrent(resp.Body)
		if err != nil {
			// 失败则跳过本次循环
			continue
		}

		err = PutTorrent(metaTorrent)

		return 0, err
	}
}