public function actionUpdate($key) { $executionTime = KeyValue::model()->findByPk('newsletter_execution_time'); $frequency = KeyValue::model()->findByPk('newsletter_frequency'); $executionTime->value = date('Y-m-d H:i:s'); $executionTime->save(); if ($frequency->value < 0) { $frequency->value = abs($frequency->value); $frequency->save(); } Yii::app()->end(); }
/** * Render a newsletter * @return string the newsletter. */ public function make() { $module = Yii::app()->controller->module; require_once 'protected/vendors/simplepie/autoloader.php'; require_once 'protected/vendors/simplepie/idn/idna_convert.class.php'; $timeLimit = KeyValue::model()->findByPk('newsletter_execution_time')->value; $simplePie = new SimplePie(); $simplePie->set_cache_location('./protected/cache/simplepie'); $simplePie->set_cache_duration(1); // 1 seconde // This makes sure that the content is sent to the browser as // text/html and the UTF-8 character set (since we didn't change it). $simplePie->handle_content_type(); if ($module->multiLang) { if (isset($this->language)) { $feeds = $module->feeds[$this->language]; $renderLanguage = $this->language; } else { $feeds = $module->feeds[Yii::app()->language]; $renderLanguage = Yii::app()->language; } } else { $feeds = $module->feeds; $renderLanguage = Yii::app()->language; } $atLeastOne = false; for ($i = 0; $i < count($feeds); $i++) { if (isset($feeds[$i]['expression'])) { $feeds[$i]['content'] = $this->evaluateExpression($feeds[$i]['expression'], array('timeLimit' => $timeLimit, 'language' => $renderLanguage)); if ($feeds[$i]['content'] != '') { $atLeastOne = true; } } else { $simplePie->set_feed_url($feeds[$i]['url']); $simplePie->init(); $feeds[$i]['link'] = $simplePie->get_permalink(); $feeds[$i]['items'] = array(); foreach ($simplePie->get_items(0, $feeds[$i]['limit']) as $item) { if ($item->get_date('U') > strtotime($timeLimit)) { $feeds[$i]['items'][] = $item; $atLeastOne = true; } } } } if ($atLeastOne) { return Yii::app()->controller->renderPartial('newsletter.components.views.newsletter', array('feeds' => $feeds, 'language' => $renderLanguage), true); } else { return false; } }
public function actionAdmin() { $model = new AdminForm(); $frequency = KeyValue::model()->findByPk('newsletter_frequency'); $model->frequency = abs($frequency->value); if (isset($_POST['AdminForm'])) { $model->attributes = $_POST['AdminForm']; if ($model->validate()) { if (isset($_POST['yt1']) || $frequency->value < 0) { $frequency->value = -$model->frequency; } else { $frequency->value = $model->frequency; } $frequency->save(); $this->redirect('admin'); } } $newsletter = new Newsletter(); $this->render('admin', array('newsletter' => $newsletter->make(), 'model' => $model)); }
public function get($key) { return KeyValue::find($key)->value; }
} $month_start = mktime(0, 0, 0, date('m', $min_time), 1, date('Y', $min_time)); $month_end = strtotime('+1 month', $month_start); $filename = 'ptthot-' . date('Ym', $month_start) . '.csv.gz'; $temp = tmpfile(); $meta_data = stream_get_meta_data($temp); $tmp_filename = $meta_data['uri']; $stream = gzopen($tmp_filename, 'w'); fwrite($stream, "#board,timestamp,count\n"); foreach (RankData::search("`time` >= {$month_start} AND `time` < {$month_end}")->order(array('board', 'time'))->volumemode(10000) as $rankdata) { $last_time = max($last_time, $rankdata->time); fwrite($stream, "{$rankdata->board},{$rankdata->time},{$rankdata->count}\n"); } fclose($stream); DropboxLib::putFile($tmp_filename, $filename); fclose($temp); KeyValue::set('snapshot_at', $last_time); } //dump titlehistory $filename = 'ptthot-title.csv.gz'; $temp = tmpfile(); $meta_data = stream_get_meta_data($temp); $tmp_filename = $meta_data['uri']; $stream = gzopen($tmp_filename, 'w'); fwrite($stream, "#board,timestamp,title\n"); foreach (TitleHistory::search(1)->order(array("board", "title"))->volumemode(10000) as $titlehistory) { fputcsv($stream, array($titlehistory->board, $titlehistory->time, $titlehistory->title)); } fclose($stream); DropboxLib::putFile($tmp_filename, $filename); fclose($temp);
public function companyId() { $companyId = ''; if ($this->id() !== '') { $model = new CSCorpModel(); $companyId = $model->getCompanyIdByOpenId($this->id()); } else { $open_ids = KeyValue::get(KEY_APP_ALL_CORPORATION); $companyId = $open_ids[0]; } return $companyId; }
curl_setopt($curl, CURLOPT_URL, 'http://www.ptt.cc/hotboard.html'); if (!($content = curl_exec($curl))) { exit; } $content = iconv('Big5', 'UTF-8//IGNORE', $content); $content = preg_replace('/([\\x{0fffe}-\\x{10ffff}]+)/u', '', $content); if (!preg_match('#\\(本文約每小時更新,最後更新時間 ([^)]*)#', $content, $matches)) { throw new Exception('找不到時間'); } if (!($time = strtotime($matches[1]))) { throw new Exception('找不到時間'); } $content = preg_replace('#<td[^>]*>#', '', $content); $content = preg_replace('#<a [^>]*>#', '', $content); $content = preg_replace('#</[^>]+>#', '', $content); preg_match_all("#人氣:([0-9]*)\n([^\\s]*)\n(.*)#m", $content, $matches); $latest_data = array(); foreach ($matches[0] as $id => $data) { try { $board = strval($matches[2][$id]); $count = intval($matches[1][$id]); $name = strval($matches[3][$id]); RankData::insert(array('time' => $time, 'board' => $board, 'count' => $count)); } catch (Pix_Table_DuplicateException $e) { RankData::search(array('time' => $time, 'board' => $board))->update(array('count' => $count)); } $latest_data[] = array($board, $count, $name); TitleHistory::updateTitle($board, $time, $name); } KeyValue::set('latest_hot', json_encode(array('time' => $time, 'boards' => $latest_data))); echo '完成: ' . date('c', $time) . "\n";
<?php require '../../lib.php'; $open_ids = KeyValue::get(KEY_APP_ALL_CORPORATION); $model = new CSCorpModel(); $data = array(); if (empty($open_ids)) { $data = array(msg => '还没有任何公司主号开启该app'); } else { foreach ($open_ids as $open_id) { $a = $model->getCorpInfo($open_id); $data[$open_id]['info'] = $a; $tokens = $model->getAllToken($open_id); $data[$open_id]['tokens'] = $tokens; } } OpenUtils::outputJson($data);
public function castToKeyValue($obj) { $kv = new KeyValue(); if (isset($obj->key)) { $kv->setKey($obj->key); } if (isset($obj->value)) { $kv->setValue($obj->value); } return $kv; }
<?php require '../../lib.php'; $company_ids = KeyValue::get(KEY_APP_ALL_CORPORATION); $model = new CSCorpModel(); $data = array(); foreach ($company_ids as $company_id) { $tokens = KeyValue::get(KEY_ACCESSTOKEN . $company_id); $result = $model->refreshToken($tokens['refresh_token']); if ($result != false) { $refresh_tokens = $result; $refresh_tokens['company_id'] = $tokens['company_id']; $refresh_tokens['is_open'] = $tokens['is_open']; saveToken($refresh_tokens); } $tokens_after = KeyValue::get(KEY_ACCESSTOKEN . $company_id); $data[$company_id]['刷新前'] = $tokens; $data[$company_id]['刷新结果'] = $result; $data[$company_id]['刷新后'] = $tokens_after; } OpenUtils::outputJson($data);