XCrawler – Automatically post to WordPress

Okay bài toán này trước đây mình đã xử lý. Tuy nhiên giờ thì xử lý triệt để hơn.

  • Gửi notifications / email khi có favorited movies. Dựa trên genres / casts
  • Gửi email khi có movie mới lên WordPress

Về cơ bản 2 bài toán này similar nhau.

  • Observe table movies khi có created
  • Kiểm tra favorites và send notification hoặc email

WordPress thì sử dụng tính năng Post by Email

Tuy nhiên sẽ có 1 vài notes

  • Đưa email vào queue để kiểm soát việc gửi email liên tục
  • Notifications bản chất chỉ để gửi các thông tin ngắn gọn. Do đó cần xử lý thích hợp notifications gì cần send out và tới channel nào
  • Handle các movies nào đã được posted để tránh duplicate
  • Cá nhân mình khi post WordPress sẽ default để mode là draft vì cần kiểm tra nội dung trước khi post. Cũng như tránh publish lên social vì dính 18+
[status publish | pending | draft | private]
    public function movieCreated(MovieCreated $event)
    {
        $movie = $event->movie;

        // Trigger notifications
        foreach ($movie->tags()->cursor() as $tag) {
            if ($tag->favorite()->exists()) {
                $movie->notify(new FavoritedMovie());
                break;
            }
        }

        foreach ($movie->idols()->cursor() as $idol) {
            if ($idol->favorite()->exists()) {
                $movie->notify(new FavoritedMovie());
                break;
            }
        }

        // Do not create WordPress is we have no cover
        if (!$movie->cover) {
            return;
        }

        // Send movie post
        if (WordPressPost::where(['title' => $movie->dvd_id])->exists()) {
            return;
        }

        Mail::send(new WordPressMoviePost($movie));
        WordPressPost::create(['title' => $movie->dvd_id]);
    }

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: