Skip to content

Commit

Permalink
Added session keeping with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
iboshkov committed May 1, 2017
1 parent e446df3 commit 7a5b153
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions OpenSubSearchLib/OSDBService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OSDBnet;

Expand All @@ -8,10 +10,16 @@ namespace OpenSubSearchLib
public class OSDBService : ISubService
{
private readonly IAnonymousClient client;

private Timer timer;
public OSDBService(string clientVersionStr = "")
{
client = Osdb.Login("en", "opensubsearch v0.1 " + clientVersionStr);
timer = new Timer(OnTimeout, null, new TimeSpan(0 , 14, 50), new TimeSpan(0, 14, 50));
}

public void OnTimeout(object state)
{
client.NoOperation();
}

public string serviceId()
Expand All @@ -29,13 +37,21 @@ public string serviceUrl()
return "https://OpenSubtitles.org";
}

private void _resetTimer()
{
timer.Dispose();
timer = new Timer(OnTimeout, null, new TimeSpan(0 , 14, 50), new TimeSpan(0, 14, 50));
}

public IList<Subtitle> searchSubtitlesFromFile(string languages, string filePath)
{
_resetTimer();
return remapList(client.SearchSubtitlesFromFile(languages, filePath));
}

public IList<Language> getAvailableLanguages()
{
_resetTimer();
return client.GetSubLanguages()
.Select(lang => new Language
{
Expand All @@ -48,34 +64,40 @@ public IList<Language> getAvailableLanguages()

public async Task<IList<Subtitle>> searchSubtitlesFromFileAsync(string languages, string filePath)
{
_resetTimer();
return await Task.Run(() => searchSubtitlesFromFile(languages, filePath));
}

public async Task<IList<Subtitle>> searchSubtitlesFromQueryAsync(string languages, string query,
int? season = null, int? episode = null)
{
_resetTimer();
return await Task.Run(() => searchSubtitlesFromQuery(languages, query, season, episode));
}

public async Task<IList<Language>> getAvailableLanguagesAsync()
{
_resetTimer();
return await Task.Run(() => getAvailableLanguages());
}

public IList<Subtitle> searchSubtitlesFromQuery(string languages, string query, int? season = null,
int? episode = null)
{
_resetTimer();
return remapList(client.SearchSubtitlesFromQuery(languages, query, season, episode));
}

public string downloadSubitleToPath(Subtitle subtitle, string path, string newName = null)
{
_resetTimer();
return client.DownloadSubtitleToPath(path, subtitle.serviceSubtitle as OSDBnet.Subtitle,
newName);
}

public Task<string> downloadSubitleToPathAsync(Subtitle subtitle, string path, string newName = null)
{
_resetTimer();
return Task.Run(() => client.DownloadSubtitleToPath(path, subtitle.serviceSubtitle as OSDBnet.Subtitle,
newName));
}
Expand Down
Empty file added docs/.keepme
Empty file.

0 comments on commit 7a5b153

Please sign in to comment.