Exemplo n.º 1
0
 public function testEmojiValue()
 {
     $name = 'himitsuHeiki';
     $value = 'シャロちゃん+☕';
     $this->assertTrue(Options::add($name, $value));
     $this->assertEquals(DB::table('options')->where('name', $name)->value('value'), $value);
     $this->assertEquals(App\Tools\Options\Options::get($name), $value);
 }
Exemplo n.º 2
0
 public function autoAuth()
 {
     $token = Options::get('bearerToken');
     if ($token) {
         $this->setToken($token);
     }
     $this->getToken();
     return $this;
 }
Exemplo n.º 3
0
 /**
  * @param $keyword
  * @param callable|null $callback
  *
  * @return array
  */
 public static function countSearch($keyword, $callback = null)
 {
     $since_id = Options::get("latest[{$keyword}]");
     $count = (int) Options::get("count[{$keyword}]", 0);
     $swc = new self($keyword, $since_id);
     $swc->count = $count;
     if (is_callable($callback)) {
         call_user_func($callback, $swc);
     }
     $res = $swc->autoSearchCount($since_id);
     Options::set("latest[{$keyword}]", $res['max_id']);
     Options::set("count[{$keyword}]", $res['count']);
     Options::set("updated[{$keyword}]", time());
     return $res;
 }
 /**
  * @param Request $request
  *
  * @return Response
  */
 public function count(Request $request)
 {
     try {
         $include_uri = $request->input('url');
         $include_texts = $request->input('shico');
         $force_cache = $request->input('force_cache');
         $is_chino = $request->input('chino');
         if (!is_null($is_chino)) {
             $keyword = self::CHINOSHICO_SEARCH_VALUE;
         } else {
             if (is_null($include_uri) && is_null($include_texts) or $include_uri && $include_texts) {
                 $keyword = self::BOTH_QUERY_VALUE;
             } else {
                 if ($include_uri && !$include_texts) {
                     $keyword = self::URL_QUERY_VALUE;
                 } else {
                     if ($include_texts && !$include_uri) {
                         $keyword = self::SYAROSHICO_SEARCH_VALUE;
                     } else {
                         throw new \InvalidArgumentException('most least one selector selected');
                     }
                 }
             }
         }
         if ((bool) Options::get("lock[{$keyword}]", false)) {
             throw new \RuntimeException('Querying... (sorry wait a few minutes and retry access)');
         }
         $old_updated = (int) Options::get("updated[{$keyword}]", 0);
         if (time() < $old_updated + 60 * 2 || (bool) $force_cache) {
             $count = ['count' => (int) Options::get("count[{$keyword}]", 0), 'max_id' => Options::get("latest[{$keyword}]"), 'queried' => 'cache'];
         } else {
             Options::set("updated[{$keyword}]", time());
             $count = SearchWalkCount::countSearch($keyword, function (SearchWalkCount $swc) {
             });
         }
         $res = ['count' => $count['count'], 'max_id' => $count['max_id'], 'query' => $keyword, 'queried' => $count['queried'], 'timestamp' => Options::get("updated[{$keyword}]")];
         return response()->json($res)->header('Access-Control-Allow-Origin', '*');
     } catch (TwitterException $ex) {
         try {
             if (!isset($count)) {
                 if ($keyword) {
                     $count = ['count' => Options::get("count[{$keyword}]")];
                 } else {
                     $count = -1;
                 }
             }
             if (isset($old_updated) && isset($keyword)) {
                 Options::set("updated[{$keyword}]", $old_updated);
             }
         } catch (\Exception $e) {
             $count = ['count' => -2];
         }
         $res = ['error' => true, 'count' => isset($count['count']) ? $count['count'] : 0, 'message' => 'Twitter API Error: ' . $ex->getMessage()];
         return response()->json($res)->setStatusCode(500)->header('Access-Control-Allow-Origin', '*');
     } catch (\Exception $ex) {
         $res = ['error' => true, 'count' => -4, 'message' => $ex->getMessage()];
         try {
             if (isset($old_updated) && isset($keyword)) {
                 Options::set("updated[{$keyword}]", $old_updated);
             }
         } catch (\Exception $e) {
         }
         return response()->json($res)->setStatusCode(500)->header('Access-Control-Allow-Origin', '*');
     }
 }
Exemplo n.º 5
0
 public function testGetNonExist()
 {
     $name = 'NonUsedNameForGet';
     $this->assertNull(Options::get($name));
 }