/** * Cronjob endpoint, merges user hashtags and twitter api trends * and stores the result to redis. * If any change occurs this service will notify the daemon via * DeamonController passing a new array of trends to track. * * @return Array of trends */ public function daemonServiceTrends() { // TODO laravel connection $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // Api Woeid trends $trends = []; foreach ($this->getPlaces() as $city => $woeid) { $trends = array_merge($trends, $this->requestTrendsByLocation($woeid)); } // Save woeid_hashtags $redis->set('woeid_hashtags', json_encode($trends)); // User trends $hashtags = Hashtag::all(); foreach ($hashtags as $hashtag) { array_push($trends, $hashtag->name); } // Discard duplicates array_unique($trends); $redisTrends = json_decode($redis->get('daemon_hashtags')); // If any change, return if ($trends == $redisTrends) { return response()->json(null)->header('Content-Type', 'application/json'); } // Store new hashtags to database $redis->set('daemon_hashtags', json_encode(array($trends))); $notify = new DaemonController(); $notify->updateTrends($trends); return response()->json($trends)->header('Content-Type', 'application/json'); }
/** * Show the application welcome screen to the user. * * @param MapReduce $mapReduce * @return Response */ public function index(MapReduce $mapReduce) { $total_tweets = Tweet::count(); $total_hashtag = Hashtag::count(); $agg_hashtags = $mapReduce->agg_hashtags(); return view('home')->with(['total_tweets' => $total_tweets, 'total_hashtag' => $total_hashtag, 'agg_hashtags' => $agg_hashtags]); }
public function send_to_queue($data) { if (isset($data['text']) && $data['text'] != null) { if (isset($data['user']['screen_name']) && $data['user']['screen_name'] != null) { $arr['text'] = $data['text']; $arr['user'] = $data['user']['screen_name']; Tweet::create($arr); if (!empty($data['entities']['hashtags'])) { foreach ($data['entities']['hashtags'] as $hashtag) { Hashtag::create(['hashtag' => $hashtag]); } } } } }
/** * Processes the Delete Hashtags request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postDelete(Request $request) { // validate request $this->validate($request, ['deleted_hashtags' => 'array']); // parse request $deleted_hashtags = $request->deleted_hashtags; // delete selected hashtags if they exist if (!empty($deleted_hashtags)) { \App\Hashtag::destroy($deleted_hashtags); \Session::flash('flash_message', 'Deleted selected hashtags.'); } // redirect to the Hashtags page $view = redirect('/hashtags'); return $view; }
/** * Saves specified hashtag $term to database and associates the hashtag with * the user logged in if applicable. * * @param string $term hashtag term to save off */ protected function saveHashtag($term) { // store most recently searched hashtag term in the global session \Session::put('searched_term', $term); // if a user is logged, then associate the user with the hashtag // otherwise, store hashtag term in global session until user logs in if (\Auth::check()) { // get the user id logged in $user_id = \Auth::id(); // create a hashtag associated with the user if it does not exist \App\Hashtag::firstOrCreate(compact('term', 'user_id')); } else { if (!in_array($term, \Session::get('stored_terms', []))) { \Session::push('stored_terms', $term); } } }
/** * Execute the console command. * * @return mixed */ public function handle() { $now = Carbon::now(); $min = 1; $ignoredWords = array('job', 'jobs', 'hiring', 'careerarc'); echo "starting"; $results = DB::connection('pgsql')->select(DB::raw("SELECT LOWER(hashtag) as hashtag, COUNT(*) as count FROM \"tagQueue\".\"tagQueue\" WHERE hashtag ~ E'[a-z0-9]' AND is_processed = B'0' AND created < now() GROUP BY hashtag", array('now' => $now, 'min' => $min))); echo "got new tags"; $hashtags = []; foreach (Hashtag::where('is_archived', false)->get() as $hashtag) { if (!in_array($hashtag->tag, $ignoredWords)) { $hashtags[$hashtag->tag] = $hashtag->id; } } $newhashtags = []; foreach ($results as $tag) { if (!array_key_exists($tag->hashtag, $hashtags)) { $model = new Hashtag(); $model->tag = $tag->hashtag; if (!in_array($model->tag, $ignoredWords)) { $newhashtags[] = array('tag' => $tag->hashtag, 'current_price' => 1, 'is_active' => false, 'created_at' => $now, 'updated_at' => $now); } } } echo "inserting new hashtags"; Hashtag::insert($newhashtags); foreach (Hashtag::get() as $hashtag) { if (!array_key_exists($hashtag->hashtag, $hashtags)) { $hashtags[$hashtag->tag] = $hashtag->id; } } $counts = []; foreach ($results as $tag) { if (array_key_exists($tag->hashtag, $hashtags)) { $counts[] = array('hashtag_id' => $hashtags[$tag->hashtag], 'count' => $tag->count, 'created_at' => $now, 'updated_at' => $now); } } echo "inserting counts"; HashtagCount::insert($counts); $results = DB::connection('pgsql')->update(DB::raw("UPDATE \"tagQueue\".\"tagQueue\" SET is_processed = B'1' WHERE is_processed = B'0' AND created < ':now';", array('now' => $now))); $results = DB::connection('pgsql')->update(DB::raw("DELETE FROM \"tagQueue\".\"tagQueue\" WHERE is_processed = B'1'")); }
/** * Run the database seeds. * * @return void */ public function run() { // first, create an array of all the users we want to associate hashtags // with // the *key* will be the user email and the *value* will be an array of // hashtags $users = ['*****@*****.**' => ['taylorswift', 'katyperry', 'mileycyrus'], '*****@*****.**' => ['adele', 'taylorswift', 'carrieunderwood']]; // now loop through the above array, creating a new pivot for each user // to hashtag foreach ($users as $email => $hashtag_terms) { // first get the user $user = \App\User::where('email', 'like', $email)->first(); // now loop through each hashtag for this user, adding the pivot foreach ($hashtag_terms as $term) { $hashtag = \App\Hashtag::where('term', 'like', $term)->first(); // associate this hashtag to this user $user->hashtags()->save($hashtag); } } }
public function hashtagTotal() { $count = Hashtag::count(); return $this->response->withItem($count, new CountTransformer()); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { Log::debug('tagcontroller destroy'); //Get Hashtag using text from input given $hashtag = Hashtag::where('name', $id)->firstOrFail(); //Get user from session $user = Auth::user(); $relationExists = count(DB::select('select * from hashtag_user where hashtag_id = ? and user_id = ?', [$hashtag->id, $user->id])); //Check relation does exist if ($relationExists > 0) { //If relation exists //Delete relation user-hashtag $hashtag->users()->detach($user->id); } else { //if it does not exist //Macro format JSON response return response()->api(400, 'No', 'Hashtag already detached from User, hashtag_user non existent.', ''); } //refresh $hashtag = Hashtag::where('name', $id)->firstOrFail(); //check if any user has it related to his account if ($hashtag->users->count() > 0) { //Don't delete from table hashtags, at least one user has it return response()->api(200, 'yes', 'Success detaching hashtag from user.', ''); } else { //No user has it try { //Delete hashtag from mysql db $hashtag->delete(); return response()->api(200, 'yes', 'Success deleting hashtag from db.', ''); } catch (Exception $e) { Log::error("Failed deleting hashtagh from hashtags"); Log::error($e->getMessage()); return response()->api(400, 'no', 'Failed deleting hashtag from hashtags', ''); } } }
/** * Saves the hashtag terms stored in the global session to the database. */ protected function saveStoredHashtags() { // get the user id logged in $user_id = \Auth::id(); // create a hashtag associated with the user if it does not exist // for each stored hashtag term in global session $stored_terms = \Session::pull('stored_terms', []); while (($term = array_pop($stored_terms)) != null) { \App\Hashtag::firstOrCreate(compact('term', 'user_id')); } }