草庐IT

maccms10二开批量入库和资源打包

钰琪 2023-03-28 原文

批量入库

因为maccms自带的采集采集起来很慢,而且很多资源站的采集接口不能对内容排序,导致最旧的数据最后入库。用java写个采集程序,采集完入库的时候发现不能一次性入库多个数据,导致入库也很慢,所以就在入库控制器写了个方法批量入库。

Receive.php

public function foo()
{
	$info = $this->_param;
	if (!array_key_exists("vods", $info) || empty($info["vods"]))
	{
	    echo json_encode(['code'=>2002,'msg'=>"vods is required"],JSON_UNESCAPED_UNICODE);
            exit;
	}
        $vods = json_decode($info["vods"], true);
        
        if (null == $vods)
        {
            echo json_encode(['code'=>2002,'msg'=>"the format of vods is incorrect"],JSON_UNESCAPED_UNICODE);
            exit;
        }
        
        $inter = mac_interface_type();
        $res = [];
    	foreach ($vods as $vod)
    	{
    		if(empty($vod['type_id'])) {
            	$vod['type_id'] = $inter['vodtype'][$vod['type_name']];
        }
        	
        $data['data'][] = $vod;
        	
    }
    $res[] = model('Collect')->vod_data([],$data,1); 
    echo json_encode($res,JSON_UNESCAPED_UNICODE);
}

资源打包

有时候需要给其他程序全量下载视频数据,有个接口会很方便

Provide.php

// format: json返回json格式数据;无或line或其他返回的数据,是一行一条数据,且每一行都是合法的json格式
public function bar()
{
        // token暂时无用
        $token = $this->_param['token'];
        $format = $this->_param['format'] == "json" ? "json" : "line";
        $suffix = $format == "json" ? ".json" : ".txt";

        // category暂时无用
        $category = $this->_param['category'];
        if (empty($category)) {
        	$category = "";
        }
        else {
        	$temp = explode(",", $category);
        	$category = "";
        	foreach ($temp as $k=>$v) {
        		if (is_numeric($v)) {
        			$category .= (($k==0 ? "" : ",") . $v);
        		}
        	}
        }
        
        $path = ROOT_PATH . "data".$suffix;
        $url = request()->domain() . "/" . "data".$suffix;
        if (is_file($path)) {
        	$mtime = (int)date("Ymd", filemtime($path));
        	$today = (int)date("Ymd", time());
        	if (filesize($path) > 0 && $mtime - $today == 0)
        	{
        		echo json_encode(["time" => $mtime, "url" => $url]);
        		exit;
        	}
        }
        
        $fp = new \SplFileObject($path, "a+b");
        if ($fp->flock(LOCK_EX)) {
			if ($fp->getSize() > 0) {
				$fp->ftruncate(0);
				$fp->fflush();
			}
				
			$count = Db::name('Vod')->count();
			$i = 0;
        	$size = 2000;
        	$temp = $format == "json" ? array() : "";
        	while ($i<$count) {
        		$size = ($count-$i>=$size) ? $size : $count-$i;
        		$limit = $i . "," . $size;
        		// vod_class as class,
        		$list = Db::name('Vod')->field("vod_id as id,type_id as type,vod_name as name,vod_pic as pic,vod_play_url as playurl")->limit($limit)->select();
        		if ($format == "json") {
        			$temp = array_merge($temp,$list);
        		} else {
        			$temp = "";
        			foreach ($list as $k=>$v) {
        				if ($i >0 || ($i==0 && $k>0)) {
        					$temp .= "\r\n";
        				}
        				// JSON_UNESCAPED_UNICODE
						$temp .= json_encode($v);
					}
					$fp->fwrite($temp);
        			$fp->fflush();
        		}
        		$i+=$size;
        	}
        	if ($format == "json")
        	{
        		// JSON_UNESCAPED_UNICODE
        		$fp->fwrite(json_encode($temp));
        		$count = count($temp);
        		$fp->fflush();
        	} else {
        		$fp->seek(PHP_INT_MAX);
        		$count = $fp->key() + 1;
        	}
        	$fp->flock(LOCK_UN);
		$fp = null;
                clearstatcache(true, $path);
		echo json_encode(["time" => (int)date("Ymd", time()), "url" => $url, "count" => $count]);
		exit;
	}
        echo json_encode(["time" => (int)date("Ymd", time()), "url" => "", "count" => 0]);
}

有关maccms10二开批量入库和资源打包的更多相关文章

  1. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  2. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  3. ruby - Chef LW 资源属性默认值如何引用另一个属性? - 2

    我正在尝试将一个资源属性的默认值设置为另一个属性的值。我正在为我正在构建的tomcat说明书定义一个资源,其中包含以下定义。我想要可以独立设置的“名称”和“服务名称”属性。当未设置服务名称时,我希望它默认为为“名称”提供的任何内容。以下不符合我的预期:attribute:name,:kind_of=>String,:required=>true,:name_attribute=>trueattribute:service_name,:kind_of=>String,:default=>:name注意第二行末尾的“:default=>:name”。当我在Recipe的新block中引用我

  4. 由于 libgmp.10.dylib 的问题,Ruby 2.2.0 无法运行 - 2

    我刚刚安装了带有RVM的Ruby2.2.0,并尝试使用它得到了这个:$rvmuse2.2.0--defaultUsing/Users/brandon/.rvm/gems/ruby-2.2.0dyld:Librarynotloaded:/usr/local/lib/libgmp.10.dylibReferencedfrom:/Users/brandon/.rvm/rubies/ruby-2.2.0/bin/rubyReason:Incompatiblelibraryversion:rubyrequiresversion13.0.0orlater,butlibgmp.10.dylibpro

  5. ruby - ri 有空文件 – Ubuntu 11.10, Ruby 1.9 - 2

    我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da

  6. ruby-on-rails - gem install rmagick -v 2.13.1 错误 Failed to build gem native extension on Mac OS 10.9.1 - 2

    我已经通过提供MagickWand.h的路径尝试了一切,我安装了命令工具。谁能帮帮我?$geminstallrmagick-v2.13.1Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Failedtobuildgemnativeextension./Users/ghazanfarali/.rvm/rubies/ruby-1.8.7-p357/bin/rubyextconf.rbcheckingforRubyversion>=1.8.5...yescheckingfor/

  7. ruby - 安装 tiny_tds 在 mac os 10.10.5 上出现错误 - 2

    我正在使用macos,我想使用ruby​​驱动程序连接到sqlserver。我想使用tiny_tds,但它给出了缺少free_tds的错误,但它已经安装了。怎么能过这个?~brewinstallfreetdsWarning:freetds-0.91.112alreadyinstalled~sudogeminstalltiny_tdsBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtiny_tds:ERROR:Failedtobuildgemnativeextension.完整日志如下:/System

  8. ruby - rails 3.2.2(或 3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10 连接错误 - 2

    我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat

  9. ruby-on-rails - 在 osx 10.9.3 上使用 RVM 安装 ruby​​-1.9.3-p547 时出错 - 2

    如何解决这个错误:$rvminstall1.9.3Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.9/x86_64/ruby-1.9.3-p547.Continuingwithcompilation.Pleaseread'rvmhelpmount'togetmoreinformationonbinaryrubies.Checkingrequirementsforosx.Certificatesin'/usr/local/etc/openssl/cert.pem'arealr

  10. ruby - 如何批量检查文件内容是否相同 - 2

    我想使用Ruby检查数千对文件中的每对文件是否包含相同的信息。有人能指出我正确的方向吗? 最佳答案 require'fileutils'FileUtils.compare_file('file1','file2')当且仅当文件file1和file2相同时返回true。 关于ruby-如何批量检查文件内容是否相同,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/33769865/

随机推荐