Пример #1
0
        public async Task<Thing> GetLinkByUrl(string url)
        {
            try
            {
                var originalUrl = url;
                if (originalUrl.Contains(".json"))
                {
                }
                else if (originalUrl.Contains("?"))
                {
                    var queryPos = url.IndexOf("?");
                    url = string.Format("{0}.json{1}", originalUrl.Remove(queryPos), originalUrl.Substring(queryPos));
                }
                else 
                {
                    url = originalUrl + ".json";
                }

                Listing listing = null;
                var comments = await _simpleHttpService.SendGet(await GetCurrentLoginCookie(), url);
                if (comments.StartsWith("["))
                {
                    var listings = JsonConvert.DeserializeObject<Listing[]>(comments);
                    listing = new Listing { Data = new ListingData { Children = new List<Thing>() } };
                    foreach (var combinableListing in listings)
                    {
                        listing.Data.Children.AddRange(combinableListing.Data.Children);
                        listing.Kind = combinableListing.Kind;
                        listing.Data.After = combinableListing.Data.After;
                        listing.Data.Before = combinableListing.Data.Before;
                    }
                }
                else
                    listing = JsonConvert.DeserializeObject<Listing>(comments);

                var requestedLinkInfo = listing.Data.Children.FirstOrDefault(thing => thing.Data is Link);
                if (requestedLinkInfo != null)
                {

                    var result = MaybeFilterForNSFW(listing);

                    ((Link)requestedLinkInfo.Data).Permalink = originalUrl;
                    _lastCommentsOnPostRequest = Tuple.Create(DateTime.Now, ((Link)requestedLinkInfo.Data).Subreddit, ((Link)requestedLinkInfo.Data).Permalink, result);
                    return requestedLinkInfo;
                }
                else
                    return null;
            }
            catch(Exception ex)
            {
                _notificationService.CreateErrorNotification(ex);
                return null;
            }
        }
Пример #2
0
        public async Task<Listing> GetCommentsOnPost(string subreddit, string permalink, int? limit)
        {
            //comments are pretty slow to get, so cache it to within 5 minutes for the most recent request
            if (_lastCommentsOnPostRequest != null &&
                (DateTime.Now - _lastCommentsOnPostRequest.Item1).TotalMinutes < 5 &&
                _lastCommentsOnPostRequest.Item2 == subreddit &&
                _lastCommentsOnPostRequest.Item3 == permalink)
                return _lastCommentsOnPostRequest.Item4;

            try
            {
                var maxLimit = (await UserIsGold()) ? 1500 : 500;
                var guardedLimit = Math.Min(maxLimit, limit ?? maxLimit);

                string targetUri = null;

                if (permalink.Contains("reddit.com"))
                {
                    permalink = permalink.Substring(permalink.IndexOf("reddit.com") + "reddit.com".Length);
                }

                if (permalink.Contains(".json?"))
                {
                    targetUri = "http://www.reddit.com" + permalink;
                }
                else if (permalink.Contains("?"))
                {
                    var queryPos = permalink.IndexOf("?");
                    targetUri = string.Format("http://www.reddit.com{0}.json{1}", permalink.Remove(queryPos), permalink.Substring(queryPos));
                }
                else
                {
                    targetUri = limit == -1 ?
                                string.Format("http://www.reddit.com{0}.json", permalink) :
                                string.Format("http://www.reddit.com{0}.json?limit={1}", permalink, limit);
                }

                Listing listing = null;
                var comments = await _simpleHttpService.SendGet(await GetCurrentLoginCookie(), targetUri);
                if (comments.StartsWith("["))
                {
                    var listings = JsonConvert.DeserializeObject<Listing[]>(comments);
                    listing = new Listing { Data = new ListingData { Children = new List<Thing>() } };
                    foreach (var combinableListing in listings)
                    {
                        listing.Data.Children.AddRange(combinableListing.Data.Children);
                        listing.Kind = combinableListing.Kind;
                        listing.Data.After = combinableListing.Data.After;
                        listing.Data.Before = combinableListing.Data.Before;
                    }
                }
                else
                    listing = JsonConvert.DeserializeObject<Listing>(comments);

                var result = MaybeFilterForNSFW(listing);

                var requestedLinkInfo = listing.Data.Children.FirstOrDefault(thing => thing.Data is Link);
                if (requestedLinkInfo != null)
                {
                    if (!_linkToOpMap.ContainsKey(((Link)requestedLinkInfo.Data).Name))
                    {
                        _linkToOpMap.Add(((Link)requestedLinkInfo.Data).Name, ((Link)requestedLinkInfo.Data).Author);
                    }
                }

                _lastCommentsOnPostRequest = Tuple.Create(DateTime.Now, subreddit, permalink, result);

                return result;
            }
            catch (Exception ex)
            {
                _notificationService.CreateErrorNotification(ex);
                return new Listing { Kind = "Listing", Data = new ListingData { Children = new List<Thing>() } };
            }
        }
Пример #3
0
        public async Task StoreLinks(Listing listing)
        {
            await Initialize();

            try
            {
                await _links.StoreLinks(listing);

                var subredditTuples = listing.Data.Children
                    .Where(thing => thing.Data is Link)
                    .Select(thing => Tuple.Create(((Link)thing.Data).SubredditId, ((Link)thing.Data).Subreddit))
                    .Distinct();

                foreach (var tpl in subredditTuples)
                {
                    await _subreddits.StoreSubreddit(tpl.Item1, tpl.Item2);
                }
            }
            catch (Exception ex)
            {
                //_notificationService.CreateErrorNotification(ex);
            }
        }
Пример #4
0
        public async Task<Listing> GetMoreOnListing(IEnumerable<string> childrenIds, string contentId, string subreddit)
        {
            var targetUri = "http://www.reddit.com/api/morechildren.json";

            if (childrenIds.Count() == 0)
                return new Listing
                {
                    Kind = "Listing",
                    Data = new ListingData()
                };

            var arguments = new Dictionary<string, string>
            {
                {"children", string.Join(",", childrenIds) },
                {"link_id", contentId },
                {"pv_hex", ""},
                {"api_type", "json" }
            };

            if (subreddit != null)
            {
                arguments.Add("r", subreddit);
            }

            try
            {
                var result = await _simpleHttpService.SendPost(await GetCurrentLoginCookie(), arguments, targetUri);
                var newListing = new Listing
                {
                    Kind = "Listing",
                    Data = new ListingData { Children = JsonConvert.DeserializeObject<JsonThing>(result).Json.Data.Things }
                };

                return MaybeInjectAdvertisements(MaybeFilterForNSFW(newListing));
            }
            catch (Exception ex)
            {
                _notificationService.CreateErrorNotification(ex);

                return new Listing
                {
                    Kind = "Listing",
                    Data = new ListingData { Children = new List<Thing>() }
                };
            }
        }
Пример #5
0
        private async Task<Listing> DeserializeCursor(DBCursor cursor, int count)
        {
            var redditService = ServiceLocator.Current.GetInstance<IRedditService>();
            int i = 0;
            var targetListing = new Listing { Data = new ListingData { Children = new List<Thing>() } };

            if(cursor != null)
            {
                do
                {
                    var currentRecord = cursor.Get();
                    var decodedListing = Encoding.UTF8.GetString(currentRecord, LinkKeySpaceSize, currentRecord.Length - LinkKeySpaceSize);
                    var deserializedLink = JsonConvert.DeserializeObject<Thing>(decodedListing);
                    if (deserializedLink != null && deserializedLink.Data is Link)
                    {
                        redditService.AddFlairInfo(((Link)deserializedLink.Data).Name, ((Link)deserializedLink.Data).Author);
                    }
                    targetListing.Data.Children.Add(deserializedLink);
                    
                    if (i++ > count)
                    {
                        //after type encoding
                        targetListing.Data.After = Encoding.UTF8.GetString(currentRecord, 0, 16);
                    }

                }while(await cursor.MoveNextAsync());
            }

            return targetListing;
        }
Пример #6
0
        public async Task StoreMessages(User user, Listing listing)
        {
            await Initialize();
            try
            {
                if (listing == null || listing.Data.Children.Count == 0)
                    return;

                if (user == null || String.IsNullOrEmpty(user.Username))
                    return;

                await StoreOrderedThings("messages-" + user.Username, listing.Data.Children);
            }
            catch (Exception ex)
            {
                //_notificationService.CreateErrorNotification(ex);
            }
        }
        private Listing MaybeStoreCommentsOnPost(Listing listing, string permalink)
        {
            if (listing == null)
                return null;

            var requestedLinkInfo = listing.Data.Children.FirstOrDefault(thing => thing.Data is Link);
            if (requestedLinkInfo == null)
                return listing;

            if (!_linkToOpMap.ContainsKey(((Link)requestedLinkInfo.Data).Name))
            {
                _linkToOpMap.Add(((Link)requestedLinkInfo.Data).Name, ((Link)requestedLinkInfo.Data).Author);
            }
            _suspendableWorkQueue.QueueLowImportanceRestartableWork(async (token) =>
                {
                    lock (_currentlyStoringComments)
                    {
                        if (_currentlyStoringComments.ContainsKey(permalink))
                            return;

                        _currentlyStoringComments.Add(permalink, listing);
                    }
                    try
                    {
                        await _offlineService.StoreComments(listing);
                    }
                    catch { }
                    finally
                    {
                        lock (_currentlyStoringComments)
                        {
                            _currentlyStoringComments.Remove(permalink);
                        }
                    }
                });
            return listing;
        }
Пример #8
0
        private Listing FilterForNSFW(Listing source)
        {
            source.Data.Children = source.Data.Children
                .Select(FilterForNSFW)
                .Where(thing => thing != null)
                .ToList();

            return source;
        }
        public async Task<Listing> GetSubreddits(int? limit)
        {
            if (_subredditListing != null)
                return _subredditListing;

            var result = await _redditService.GetSubreddits(limit);
            if (result != null && result.Data.Children.Count > 0)
            {
                _subredditListing = result;
                await MaybeStoredSubredditListing(result);
            }
            else
            {
                _subredditListing = await GetDefaultSubreddits();
            }

            return _subredditListing;
        }
 private Listing MaybeStorePostsBySubreddit(Listing listing)
 {
     if (listing == null)
         return null;
     _suspendableWorkQueue.QueueLowImportanceRestartableWork(async (token) =>
         {
             Task maybeTask = null;
             try
             {
                 maybeTask = _offlineService.StoreLinks(listing);
                 activeMaybeTasks.Add(maybeTask);
                 await maybeTask;
             }
             catch { }
             finally
             {
                 if (maybeTask != null)
                 {
                     lock (activeMaybeTasks)
                     {
                         activeMaybeTasks.Remove(maybeTask);
                     }
                 }
             }
         });
     
     return listing;
 }
        public async Task<Listing> GetSubscribedSubredditListing()
        {
            if (_subscribedSubredditListing != null)
                return _subscribedSubredditListing;
            
            var result = await _redditService.GetSubscribedSubredditListing();
            if (result != null && result.Data.Children.Count > 0)
            {
                _subscribedSubredditListing = result;
            }
            else
            {
                _subscribedSubredditListing = await GetDefaultSubreddits();
            }

            await MaybeStoreSubscribedSubredditListing(result, await _userService.GetUser());

            return _subscribedSubredditListing;
        }
 private async Task MaybeStoredSubredditListing(Listing listing)
 {
     try
     {
         if (listing != null && listing.Data.Children != null && listing.Data.Children.Count > 0)
         {
             await _offlineService.StoreOrderedThings("reddits:", listing.Data.Children);
         }
     }
     catch { }
 }
 private async Task MaybeStoreSubscribedSubredditListing(Listing listing, User user)
 {
     try
     {
         if (user != null && user.Username != null && listing != null && listing.Data.Children != null && listing.Data.Children.Count > 0)
         {
             await _offlineService.StoreOrderedThings("sublist:" + user.Username, listing.Data.Children);
         }
     }
     catch { }
 }
Пример #14
0
 private Listing MaybeFilterForNSFW(Listing source)
 {
     if (_settingsService.AllowOver18)
     {
         return source;
     }
     else
         return FilterForNSFW(source);
 }
        private Listing MaybeStoreMessages(User user, Listing listing)
        {
            if (user == null || string.IsNullOrEmpty(user.Username))
                return listing;

            lock (_currentlyStoringMessages)
            {
                if (_currentlyStoringMessages.ContainsKey(user.Username))
                    return listing;

                _currentlyStoringMessages.Add(user.Username, listing);
            }
            _offlineService.StoreMessages(user, listing).ContinueWith(task =>
            {
                lock (_currentlyStoringMessages)
                {
                    _currentlyStoringMessages.Remove(user.Username);
                }
            });
            return listing;
        }
Пример #16
0
		private Listing MaybeInjectAdvertisements(Listing source)
		{
            if (!_settingsService.AllowAdvertising)
                return source;

			int count = source.Data.Children.Count;
			for (int i = 9; i < count; i += 10)
			{
				var thing = new Thing { Data = new Advertisement(), Kind = "ad" };
				source.Data.Children.Insert(i, thing);
			}
			return source;
		}
Пример #17
0
        public async Task StoreComments(Listing listing)
        {
            await Initialize();
            if (_terminateSource.IsCancellationRequested)
                return;
            try
            {
                if (listing == null || listing.Data.Children.Count == 0)
                    return;

                var linkThing = listing.Data.Children.First().Data as Link;
                if (linkThing != null)
                {
                    await _links.StoreLink(listing.Data.Children.First());
                    if (_terminateSource.IsCancellationRequested)
                        return;
                }

                await _comments.StoreComments(listing);
            }
            catch (Exception ex)
            {
                //_notificationService.CreateErrorNotification(ex);
            }
        }
Пример #18
0
        private async Task<Listing> GetUserMultis(Listing listing)
        {
            var targetUri = string.Format("http://www.reddit.com/api/multi/mine.json");

            try
            {
                var subreddits = await _simpleHttpService.SendGet(await GetCurrentLoginCookie(), targetUri);
                if (subreddits == "[]")
                    return listing;
                else
                {
                    var currentUser = await _userService.GetUser();
                    var userMultis = JsonConvert.DeserializeObject<Thing[]>(subreddits);
                    foreach (var thing in userMultis)
                    {
                        var labeledMulti = new TypedThing<LabeledMulti>(thing);
                        var multiPath = labeledMulti.Data.Path;

                        multiPath = multiPath.Replace("/user/" + currentUser.Username, "/me");

                        listing.Data.Children.Insert(0, (new Thing { Kind = "t5", Data = new Subreddit { DisplayName = labeledMulti.Data.Name, HeaderImage = "/Assets/multireddit.png", Title = labeledMulti.Data.Name, Url = multiPath, Headertitle = labeledMulti.Data.Name, Over18 = false } }));
                    }
                }
            }
                //this api is most likely still in flux just silently fail if they break us down the line
            catch {}
            return listing;
        }
Пример #19
0
 public async Task StoreLinks(Listing listing)
 {
     foreach (var link in listing.Data.Children)
     {
         if (link.Data is Link)
         {
             try
             {
                 await StoreLink(link);
             }
             catch(Exception ex)
             {
                 var errorText = DBError.TranslateError((uint)ex.HResult);
                 Debug.WriteLine(errorText);
             }
         }
     }
 }