public static async Task Run([TimerTrigger("0 */30 * * * *")] TimerInfo myTimer, ILogger log, ExecutionContext context) { log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var connectionString = config.GetConnectionString("StorageConnectionString"); log.LogInformation($"Connection string: {connectionString}"); var table = await ShoppingTrackerDAL.CreateTableAsync(ShoppingTrackerDAL.SHOPPING_FAVORITES_TABLE, connectionString); var favorites = await ShoppingTrackerDAL.GetShoppingFavoriteAsync(table); log.LogInformation($"Favorites to analyze: {favorites.Count()}"); List <string> offers = new List <string>(); foreach (var favorite in favorites) { Enum.TryParse(favorite.PartitionKey, out ShopTypeEnum shopType); log.LogInformation($"Analyze favorite: {favorite.Name} {favorite.PartitionKey}"); switch (shopType) { case ShopTypeEnum.PSStore: var offer = await UpdateFavoritePSStoreAsync(table, favorite, log); if (offer != null) { offers.Add(offer); } break; default: log.LogError($"shopType {favorite.PartitionKey} not implemented"); break; } } log.LogInformation($"Found {offers.Count()} new price change"); if (offers.Count() > 0) { if (await SendMailWithOffers(offers, log)) { log.LogInformation("Sended mail to recipients"); } else { log.LogError("Can't send mail to recipients"); } } }
public async Task <ShoppingFavorite> AddToFavorites(ProductPrice game) { var shoppingFavorite = new ShoppingFavorite(ShopTypeEnum.PSStore, game.Id) { Name = game.Name, Discount = game.Discount, DiscountDate = String.IsNullOrWhiteSpace(game.Discount) ? null : (DateTime?)DateTime.Now, DiscountedPrice = game.DiscountedPrice, FavoritesDate = DateTime.Now, Price = game.Price, Media = game.Media, Store = (int)ShopTypeEnum.PSStore }; var table = await ShoppingTrackerDAL.CreateTableAsync(ShoppingTrackerDAL.SHOPPING_FAVORITES_TABLE); var favorite = await ShoppingTrackerDAL.InsertOrMergeShoppingFavoriteAsync(table, shoppingFavorite); return(favorite); }
private static async Task <string> UpdateFavoritePSStoreAsync(Microsoft.Azure.Cosmos.Table.CloudTable table, ShoppingFavorite favorite, ILogger log) { var baseAddress = "https://web.np.playstation.com/api/graphql/v1/op?operationName=productRetrieveForCtasWithPrice&variables=%7B%22productId%22%3A%22{0}%22%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%228532da7eda369efdad054ca8f885394a2d0c22d03c5259a422ae2bb3b98c5c99%22%7D%7D"; string offer = null; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("x-psn-store-locale-override", "it-IT"); HttpResponseMessage response = await client.GetAsync(String.Format(baseAddress, favorite.RowKey)); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var game = JsonConvert.DeserializeObject <PsStoreGame>(jsonResponse); if (game.data.productRetrieve != null) { if (favorite.DiscountedPrice != game.data.productRetrieve.webctas[0].price.discountedPrice) { offer = $"{favorite.Name} - Old Price: {favorite.DiscountedPrice} - New Price: {game.data.productRetrieve.webctas[0].price.discountedPrice} - Discount: {game.data.productRetrieve.webctas[0].price.discountText}"; log.LogInformation($"Discount {game.data.productRetrieve.webctas[0].price.discountText} for favorite {favorite.Name} - Old Price: {favorite.DiscountedPrice} - New Price: {game.data.productRetrieve.webctas[0].price.discountedPrice}"); } favorite.Discount = String.IsNullOrEmpty(game.data.productRetrieve.webctas[0].price.discountText) ? String.Empty : game.data.productRetrieve.webctas[0].price.discountText; favorite.DiscountedPrice = game.data.productRetrieve.webctas[0].price.discountedPrice; favorite.DiscountDate = DateTime.Now; favorite.Price = game.data.productRetrieve.webctas[0].price.basePrice; await ShoppingTrackerDAL.InsertOrMergeShoppingFavoriteAsync(table, favorite); } else { log.LogError($"Product is no more available in the store: {favorite.Name}"); } } else { log.LogError($"Impossible to update favorite: {favorite.Name}"); } } return(offer); }
public async Task <double?> DeleteFavorite(string id, string partition) { var table = await ShoppingTrackerDAL.CreateTableAsync(ShoppingTrackerDAL.SHOPPING_FAVORITES_TABLE); return(await ShoppingTrackerDAL.DeleteFavoriteAsync(table, partition, id)); }
public async Task <IEnumerable <ShoppingFavorite> > GetFavorites() { var table = await ShoppingTrackerDAL.CreateTableAsync(ShoppingTrackerDAL.SHOPPING_FAVORITES_TABLE); return(await ShoppingTrackerDAL.GetShoppingFavoriteAsync(table)); }