diff --git a/lib/algora/accounts.ex b/lib/algora/accounts.ex index 2f3080f..dc40b1c 100644 --- a/lib/algora/accounts.ex +++ b/lib/algora/accounts.ex @@ -195,6 +195,17 @@ defmodule Algora.Accounts do Repo.one(query) != nil end + def get_google_token(%User{} = user) do + query = from(i in Identity, where: i.user_id == ^user.id and i.provider == "google") + + with identity when identity != nil <- Repo.one(query), + {:ok, %{token: token}} <- refresh_google_tokens(user) do + token + else + _ -> nil + end + end + def get_restream_token(%User{} = user) do query = from(i in Identity, where: i.user_id == ^user.id and i.provider == "restream") diff --git a/lib/algora/google.ex b/lib/algora/google.ex index c154856..5023f10 100644 --- a/lib/algora/google.ex +++ b/lib/algora/google.ex @@ -5,6 +5,10 @@ defmodule Algora.Google do Perhaps, as time goes on, it'll contain more. """ + alias GoogleApi.YouTube.V3, as: YouTube + alias Algora.Accounts + alias Algora.Accounts.User + def refresh_access_token(refresh_token) do body = URI.encode_query(%{ @@ -30,6 +34,30 @@ defmodule Algora.Google do end end + def upload_video(user = %User{}, path, %{ + title: title, + description: description, + privacy_status: privacy_status + }) do + conn = Accounts.get_google_token(user) |> YouTube.Connection.new() + + YouTube.Api.Videos.youtube_videos_insert_simple( + conn, + ["snippet", "status"], + "multipart", + %YouTube.Model.Video{ + snippet: %YouTube.Model.VideoSnippet{ + title: title, + description: description + }, + status: %YouTube.Model.VideoStatus{ + privacyStatus: privacy_status + } + }, + path + ) + end + defp client_id, do: Application.fetch_env!(:ueberauth, Ueberauth.Strategy.Google.OAuth)[:client_id] diff --git a/mix.exs b/mix.exs index ad9ffe6..b42e1de 100644 --- a/mix.exs +++ b/mix.exs @@ -91,6 +91,7 @@ defmodule Algora.MixProject do {:ueberauth, "~> 0.10"}, {:ueberauth_google, "~> 0.10"}, {:oauth2, "~> 2.0", override: true}, + {:google_api_you_tube, "~> 0.49"} ] end diff --git a/mix.lock b/mix.lock index 8e28ae9..6395ade 100644 --- a/mix.lock +++ b/mix.lock @@ -44,6 +44,8 @@ "fly_postgres": {:hex, :fly_postgres, "0.3.4", "aa2e8845bafefb2c998446e4b886efbb3a74d52ff360f6c1a0500c0547b1aef2", [:mix], [{:ecto_sql, ">= 3.4.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:fly_rpc, "~> 0.3.0", [hex: :fly_rpc, repo: "hexpm", optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "39c98e433cedfb450c39969612d0dd5628967e2a5394647c3ade43c7c357d183"}, "fly_rpc": {:hex, :fly_rpc, "0.3.0", "a122cf0bc545403d8ad84f81b915ff0edb3c05f090519aa0e8b37fbb0e16ad83", [:mix], [], "hexpm", "5bbbc691b05659e80081cdf769630834e31bfca973d0b54be5191ce6ed33df72"}, "gettext": {:hex, :gettext, "0.24.0", "6f4d90ac5f3111673cbefc4ebee96fe5f37a114861ab8c7b7d5b30a1108ce6d8", [:mix], [{:expo, "~> 0.5.1", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "bdf75cdfcbe9e4622dd18e034b227d77dd17f0f133853a1c73b97b3d6c770e8b"}, + "google_api_you_tube": {:hex, :google_api_you_tube, "0.49.0", "ada36ad9d056ca41d62c7549dc69821adafface5694299fcba4212a62917c27b", [:mix], [{:google_gax, "~> 0.4", [hex: :google_gax, repo: "hexpm", optional: false]}], "hexpm", "ee0a500c8102057e3dec6b9ef99f4e8636795abf4ee5febc7cc8ea9d6bcabdfe"}, + "google_gax": {:hex, :google_gax, "0.4.0", "83651f8561c02a295826cb96b4bddde030e2369747bbddc592c4569526bafe94", [:mix], [{:poison, ">= 3.0.0 and < 5.0.0", [hex: :poison, repo: "hexpm", optional: false]}, {:tesla, "~> 1.2", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "a95d36f1dd753ab31268dd8bb6de9911243c911cfda9080f64778f6297b9ac57"}, "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, "heroicons": {:hex, :heroicons, "0.5.3", "ee8ae8335303df3b18f2cc07f46e1cb6e761ba4cf2c901623fbe9a28c0bc51dd", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:phoenix_live_view, ">= 0.18.2", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}], "hexpm", "a210037e8a09ac17e2a0a0779d729e89c821c944434c3baa7edfc1f5b32f3502"}, "hnswlib": {:hex, :hnswlib, "0.1.5", "750bea8627ea60dfdea67421aef34478c31bf1254495ebd43a6a100aaf0523c0", [:make, :mix], [{:cc_precompiler, "~> 0.1.0", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.7.0", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "833a3dcfd917236a4e3dd1917725c32d68f65cbf37888cb2af8d38a4e547f8f3"}, @@ -107,6 +109,7 @@ "plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"}, "plug_cowboy": {:hex, :plug_cowboy, "2.7.0", "3ae9369c60641084363b08fe90267cbdd316df57e3557ea522114b30b63256ea", [:mix], [{:cowboy, "~> 2.7.0 or ~> 2.8.0 or ~> 2.9.0 or ~> 2.10.0", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "d85444fb8aa1f2fc62eabe83bbe387d81510d773886774ebdcb429b3da3c1a4a"}, "plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"}, + "poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm", "ba8836feea4b394bb718a161fc59a288fe0109b5006d6bdf97b6badfcf6f0f25"}, "polaris": {:hex, :polaris, "0.1.0", "dca61b18e3e801ecdae6ac9f0eca5f19792b44a5cb4b8d63db50fc40fc038d22", [:mix], [{:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "13ef2b166650e533cb24b10e2f3b8ab4f2f449ba4d63156e8c569527f206e2c2"}, "postgrex": {:hex, :postgrex, "0.16.5", "fcc4035cc90e23933c5d69a9cd686e329469446ef7abba2cf70f08e2c4b69810", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "edead639dc6e882618c01d8fc891214c481ab9a3788dfe38dd5e37fd1d5fb2e8"}, "progress_bar": {:hex, :progress_bar, "3.0.0", "f54ff038c2ac540cfbb4c2bfe97c75e7116ead044f3c2b10c9f212452194b5cd", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "6981c2b25ab24aecc91a2dc46623658e1399c21a2ae24db986b90d678530f2b7"},