/** * Get podcast from feed. * Create new podcast if necessary. * * @param string $feed * @return Podcast */ public static function getOrCreateFromRss($feed) { $created = false; $podcast = Podcast::where('feed', $feed)->first(); if (!$podcast) { $podcast = new Podcast(); $podcast->feed = $feed; $podcast->coverimage = '/assets/default.png'; $podcast->save(); // load feed details asynchronously dispatch(new UpdatePodcastFromRss($podcast)); $created = true; } return $podcast; }
public function getTop() { // $user = (Auth::check() ? Auth::user() : null); // $user = Auth::user(); // return view('top', ['user' => $user]); // $podcasts = DB::table('podcasts') // ->belongsToMany('podcast_user') // ->withPivot('podcast_id') // ->orderBy('pivot_play_count', 'desc'); // ->with(array('podcast_user')) // ->join('podcast_user', 'podcasts.id', '=', 'podcast_user.podcast_id') // ->get(); $podcasts = Podcast::getTop(10); // ->orderBy('podcast_user', 'desc')->get(); return view('top', ['podcasts' => $podcasts]); }
/** * Save podcasts from uploaded opml file. * * @param Request $request * @return Response */ public function postPodcastsByOpml(Request $request, FeedService $parser) { if (!$request->hasFile('xml')) { return response()->json(['error' => 'No file.'], $status = 500); } elseif (!$request->file('xml')->isValid()) { return response()->json(['error' => 'File invalid.'], $status = 500); } $user = Auth::user(); $file = $request->file('xml'); $pos = $user->getNewPodcastPosition(); $new = []; try { $feeds = $parser->parseOpml($file); } catch (\Exception $e) { return response()->json(['error' => 'File could not be parsed.'], $status = 500); } foreach ($feeds as $feed) { $podcast = Podcast::getOrCreateFromRss($feed); $created = $user->addPodcast($podcast, $pos); if ($created) { $pos++; $new[] = $podcast; } } return response()->json(['success' => true, 'new' => $new, 'feeds' => $feeds]); }