Skip to content

Commit

Permalink
Updated response location of the video URL -- apparent change to the …
Browse files Browse the repository at this point in the history
…Ring API"
  • Loading branch information
mmckechney committed Jan 2, 2020
1 parent 1a76535 commit 76c23da
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion RingVideos/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public StringDictionary ParseArguments(string[] Args)
case "password":
a.ClearTextPassword = dict[key.ToString()];
break;
case "debug:":
case "debug":
case "d":
case "trace":
case "t":
Expand Down
4 changes: 2 additions & 2 deletions RingVideos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void Main(string[] args)
logConfig.SetMinimumLevel(LogLevel.Debug);
}

SetAuthenticationValues(a);
SetAuthenticationValues(ref a);

// Start up logic here
app.Run(f,a).Wait();
Expand Down Expand Up @@ -81,7 +81,7 @@ private static void SaveSettings(Filter f, Authentication a)
File.WriteAllText(settingsFile, config);
}

private static void SetAuthenticationValues(Authentication a)
private static void SetAuthenticationValues(ref Authentication a)
{
if(string.IsNullOrWhiteSpace(a.UserName))
{
Expand Down
10 changes: 7 additions & 3 deletions RingVideos/RingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ private async Task Initialize(string authToken)
/// <returns></returns>
internal async Task Initialize(string username, string password)
{
log.LogInformation("Authenticating with Ring");
var response = await Authorize(username, password);

if (!response.IsSuccessStatusCode)
{
log.LogDebug($"Failed to authenticate);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new SecurityException("The Ring API returned the following error: " + response.ReasonPhrase);
Expand All @@ -158,6 +160,7 @@ internal async Task Initialize(string username, string password)

if (!response.IsSuccessStatusCode)
{
log.LogInformation($"Failed to authenticate");
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new SecurityException("The Ring API returned the following error: " + response.ReasonPhrase);
Expand Down Expand Up @@ -206,14 +209,15 @@ private async Task<HttpResponseMessage> Authorize(string username, string passwo
httpHandler.AllowAutoRedirect = true;
httpHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

var httpClient = new HttpClient(httpHandler);
httpClient = new HttpClient(httpHandler);

string json = $"{{ \"client_id\": \"ring_official_android\", \"grant_type\": \"password\", \"password\": \"{password}\", \"scope\": \"client\", \"username\": \"{username}\" }}";

var response = await httpClient.PostAsync(OAuthUri, new StringContent(json, Encoding.UTF8, "application/json"));

if (!response.IsSuccessStatusCode)
{
log.LogInformation($"Failed to authenticate");
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new SecurityException("The Ring OAuth provider returned the following error: " + response.ReasonPhrase);
Expand Down Expand Up @@ -426,7 +430,7 @@ public async Task<List<Ding>> GetDingsAsync(DateTime? startUtc, DateTime? endUtc
log.LogTrace("Added Ding to collection");
dings.Add(tmp);
}
log.LogDebug(dings.Count.ToString());
//log.LogDebug(dings.Count.ToString());
}
if (dings.Count >= maxVideoCount)
{
Expand Down Expand Up @@ -487,7 +491,7 @@ public async Task<Uri> GetRecordingUriAsync(Ding ding, int retry = 0)
}
}

return response.Headers.Location;
return response.RequestMessage.RequestUri;
}
}
}
29 changes: 19 additions & 10 deletions RingVideos/RingVideoApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal async Task<int> Run(Filter filter, Authentication auth)

log.LogInformation(message.ToString());


log.LogInformation("Querying for videos to download...");
var dings = await client.GetDingsAsync(filter.StartDateTimeUtc,filter.EndDateTimeUtc,filter.VideoCount, filter.OnlyStarred);
log.LogInformation($"Found {dings.Count} videos to download");

Expand All @@ -87,15 +87,24 @@ internal async Task<bool> SaveRecordingAsync(Ding ding, Filter filter)
try
{
url = await client.GetRecordingUriAsync(ding);
log.LogDebug(url.ToString());
var wc = new System.Net.WebClient();
TimeZoneInfo.Local.GetUtcOffset(ding.CreatedAtUtc);
var est = ding.CreatedAtUtc.AddHours(TimeZoneInfo.Local.GetUtcOffset(ding.CreatedAtUtc).Hours);
filename = Path.Combine(expandedPath,
$"{est.Year}-{est.Month.ToString().PadLeft(2, '0')}-{est.Day.ToString().PadLeft(2, '0')}-T{est.Hour.ToString().PadLeft(2, '0')}_{est.Minute.ToString().PadLeft(2, '0')}_{est.Second.ToString().PadLeft(2, '0')}--{ding.Device.Description}-{ding.Type}.mp4");

Download(url, filename, wc);
log.LogInformation($"{filename} -- complete.");
if (url != null)
{
log.LogDebug(url.ToString());
var wc = new System.Net.WebClient();
TimeZoneInfo.Local.GetUtcOffset(ding.CreatedAtUtc);
var est = ding.CreatedAtUtc.AddHours(TimeZoneInfo.Local.GetUtcOffset(ding.CreatedAtUtc).Hours);
filename = Path.Combine(expandedPath,
$"{est.Year}-{est.Month.ToString().PadLeft(2, '0')}-{est.Day.ToString().PadLeft(2, '0')}-T{est.Hour.ToString().PadLeft(2, '0')}_{est.Minute.ToString().PadLeft(2, '0')}_{est.Second.ToString().PadLeft(2, '0')}--{ding.Device.Description}-{ding.Type}.mp4");

Download(url, filename, wc);
log.LogInformation($"{filename} -- complete.");
}
else
{
log.LogDebug("URL is null");
return false;
}


}
catch (Exception exe)
Expand Down

0 comments on commit 76c23da

Please sign in to comment.