Пример #1
0
		public async Task<bool> SaveDatesAsync(BingImageInfo[] infos)
		{
			if (infos == null || infos.Length == 0) return false;
			if (!IsWriteable) throw new InvalidOperationException("Datastore not open for write");
			await InitializeAsync();

			try
			{
				using (var transaction = _connection.BeginTransaction())
				using (var cmd = _connection.CreateCommand())
				{
					try
					{
						cmd.CommandText = SaveDatesCmdTxt;
						cmd.Prepare();
						foreach (var info in infos)
						{
							cmd.Parameters.Clear();
							int id;
							if (!BingDataHelper.TryConvertStartdate(info.StartDate, out id)) throw new ArgumentException("Invalid date");
							cmd.Parameters.Add(new SQLiteParameter(param(TableBingImage.IdName), id));
							cmd.Parameters.Add(new SQLiteParameter(param(TableBingImage.UrlName), info.Url));
							cmd.Parameters.Add(new SQLiteParameter(param(TableBingImage.UrlBaseName), info.UrlBase));
							cmd.Parameters.Add(new SQLiteParameter(param(TableBingImage.CopyrightName), info.Copyright));
							cmd.Parameters.Add(new SQLiteParameter(param(TableBingImage.CopyrightLinkName), info.CopyrightLink));
							await cmd.ExecuteNonQueryAsync();
						}
						transaction.Commit();
						return true;
					}
					catch
					{
						transaction.Rollback();
					}
				}
			}
			catch (NullReferenceException e) { throw e; }
			catch { }
			return false;

		}
Пример #2
0
		public async Task<BingImageInfo> ReadImageInfoAsync(DateTime date)
		{
			int intDate;
			if (!BingDataHelper.TryConvertStartdate(date, out intDate)) return null;
			await InitializeAsync();
			BingImageInfo info = null;
			try
			{
				using (var cmd = _connection.CreateCommand())
				{
					cmd.CommandText = ReadImageInfoCmdTxt;
					cmd.Parameters.Add(new SQLiteParameter("@param1", intDate));
					var reader = await cmd.ExecuteReaderAsync();
					if (reader.Read())
					{
						info = new BingImageInfo()
						{
							StartDate = reader.GetInt32(0).ToString(),
							Url = reader.GetString(1),
							UrlBase = reader.GetString(2),
							Copyright = reader.GetString(3),
							CopyrightLink = reader.GetString(4),
						};
					}
				}
			}
			catch (NullReferenceException e) { throw e; }
			catch (Exception){ }
			return info;
		}
Пример #3
0
		private async void RequestDateFromModel(DateTime date, int retryCount = 0)
		{
			if (date != _currentDate) return;
			if (retryCount == 3)
			{
				UpdateFailedStatusMessage("Server timeout");
				return;
			}

			try
			{
				_info = await ModelManager.RequestImageInfoAsync(date);

				if (date != _currentDate) return;
				if (_info == null)
				{
					UpdateFailedStatusMessage("No image found");
					return;
				}

				IsInfoEnabled = true;
				UpdateCaptions(_info.Copyright);
				var tuple = await ModelManager.RequestImagePathForGalleryAsync(_info);

				if (date != _currentDate) return;
				if (tuple == null || tuple.Item2 == null)
				{
					UpdateFailedStatusMessage("No image found");
					return;
				}
				IsSaveEnabled = true;
				ImagePathText = tuple.Item2;
			}
			catch (WebException e)
			{
				if (e.Status != WebExceptionStatus.RequestCanceled)
				{
					if (e.Status == WebExceptionStatus.Timeout)
						RequestDateFromModel(date, retryCount + 1);
					else throw new InvalidOperationException("Unhandled Web exception", e);
				}
			}
		}
Пример #4
0
		public static async Task<bool> SaveFileAsync(BingImageInfo info, string saveLocation)
		{
			string fileName = info.StartDate + ".jpg";
			string cachePath = Path.Combine(Setting.GetCurrentSetting().CachePath.LocalPath, fileName);
			string tempPath = Path.Combine(Setting.GetCurrentSetting().TempPath.LocalPath, fileName);

			try
			{
				if (File.Exists(tempPath)) File.Copy(tempPath, saveLocation);
				return true;
			}
			catch { }

			try
			{
				if (File.Exists(cachePath) && Setting.GetCurrentSetting().IsUsingCacheHd) File.Copy(cachePath, saveLocation);
				return true;
			}
			catch { }

			// need to redownload the file
			var request = WebRequest.CreateHttp(new Uri(BingBaseUri, info.Url));
			try
			{
				using (var response = await request.GetResponseAsync())
				using (var stream = response.GetResponseStream())
				using (var fin = new FileStream(saveLocation, FileMode.OpenOrCreate, FileAccess.Write))
				{
					await stream.CopyToAsync(fin);
					return true;
				}
			}
			catch (Exception) { }
			return false;

		}
Пример #5
0
		private static async Task RequestImagePathForCalendarStartRequestAsync(BingImageInfo info, RangeRequestInfo rangeInfo)
		{
			DateTime date;
			string path = null;
			Debug.WriteLine("Task for " + info.StartDate + " started");
			if (!BingDataHelper.TryConvertStartdate(info.StartDate, out date))
			{
				rangeInfo.Callback(date, false, null);
				return;
			}

			if (RequestImagePathCheckCache(info.StartDate, out path))
			{
				rangeInfo.Callback(date, true, path);
				return;
			}

			await calendarRequestSemaphore.WaitAsync();

			try
			{
				if (rangeInfo.IsCancelled) return;
				var fileName = info.StartDate + ".jpg";
				var filePath = Path.Combine(Setting.GetCurrentSetting().TempPath.LocalPath, fileName);
				if (File.Exists(filePath)) File.Delete(filePath);

				// see if we can grab the 1080 source.  if failed, try to get the 1366 source
				if (await RequestImagePathTryBingApiAsync(new Uri(BingBaseUri, info.Url), date, rangeInfo, filePath))
				{
					if (rangeInfo.IsCancelled) return;
					if (RequestImagePathCheckCache(info.StartDate, out path))
					{
						rangeInfo.Callback(date, true, path);
						return;
					}
				}
				else if (await RequestImagePathTryBingApiAsync(new Uri(BingBaseUri, info.UrlBase + "_1366x768.jpg"), date, rangeInfo, filePath))
				{
					if (rangeInfo.IsCancelled) return;
					if (RequestImagePathCheckCache(info.StartDate, out path))
					{
						rangeInfo.Callback(date, true, path);
						return;
					}
				}
			}
			catch (Exception e)
			{
				// any exception cause the thread to fail
				Debug.WriteLine(e.Message);
			}
			finally
			{
				rangeInfo.RemoveRequest(date);
				calendarRequestSemaphore.Release();
			}
			if (!rangeInfo.IsCancelled) rangeInfo.Callback(date, false, null);
		}
Пример #6
0
		private static async Task<string> RequestImagePathForGalleryStartRequestAsync(BingImageInfo info)
		{
			string path = null;
			if (RequestImagePathCheckCache(info.StartDate, out path))
				return path;

			galleryRequestToken = info.StartDate; // sets the token for latest date

			while (true)
			{
				lock (galleryRequestLock)
				{
					if (galleryRequestCount == 0) { galleryRequestCount++; break; }
				}
				if (galleryRequestToken != info.StartDate) return null; // only keep looping if we are the latest request
				var request = galleryRequest;
				if (request != null) request.Abort();
				await Task.Yield();
			}

			try
			{
				var fileName = info.StartDate + ".jpg";
				var filePath = Path.Combine(Setting.GetCurrentSetting().TempPath.LocalPath, fileName);
				if (File.Exists(filePath)) File.Delete(filePath);

				// see if we can grab the 1080 source.  if failed, try to get the 1366 source
				if (await RequestImagePathTryBingApiAsync(new Uri(BingBaseUri, info.Url), filePath)
					|| await RequestImagePathTryBingApiAsync(new Uri(BingBaseUri, info.UrlBase + "_1366x768.jpg"), filePath))
				{
					if (RequestImagePathCheckCache(info.StartDate, out path))
						return path;
				}

			}
			finally
			{
				galleryRequest = null;

				Debug.Assert(galleryRequestCount == 1);
				galleryRequestCount--;

			}
			return null;
		}
Пример #7
0
		public static async Task<Tuple<DateTime, string>> RequestImagePathForGalleryAsync(BingImageInfo info)
		{
			if (!IsInitialized) throw new InvalidOperationException("Not initialized");
			if (info == null) throw new ArgumentNullException("info");
			DateTime date;
			BingDataHelper.TryConvertStartdate(info.StartDate, out date);
			var s = await RequestImagePathForGalleryStartRequestAsync(info);
			return Tuple.Create(date, s);
		}