public async Task <string> GetRandomPost(string blogName) { if (!_maxPostNumbersCache.TryGetValue(blogName, out var postsCount)) { var blogInfo = await _client.GetBlogInfoAsync(blogName); _maxPostNumbersCache[blogName] = postsCount = (int)blogInfo.PostsCount; } //the loop is necessary because blogInfo.PostsCount doesn't count only images PhotoPost post; do { var postNumber = Random.Next(postsCount); var posts = await _client.GetPostsAsync(blogName, postNumber, 5, PostType.Photo); post = posts.Result.FirstOrDefault() as PhotoPost; if (post == null) { _maxPostNumbersCache[blogName] = postsCount = postNumber; } } while (post == null && postsCount > 0); if (post == null) { throw new Exception("no images on the blog"); } var imageUrl = post.PhotoSet.RandomSubset(1).Single().OriginalSize.ImageUrl; return(imageUrl); }
private async void LoadTagsFromPosts() { if (tumblrClient != null) { var userInfo = await tumblrClient.GetUserInfoAsync(); var blogs = userInfo.Blogs; foreach (var blog in blogs) { var blogInfo = await tumblrClient.GetBlogInfoAsync(blog.Name); for (int i = 0; i < blogInfo.PostsCount; i = i + 20) { var posts = await tumblrClient.GetPostsAsync(blog.Name, i); Parallel.ForEach(posts.Result, (post) => { Parallel.For(0, post.Tags.Length - 1, (k) => { string tag = post.Tags[k]; postTags.Add(tag); } ); } ); } } } }
public ActionResult Index(string id) { //use: http://localhost:62385/?id=samsonau //use: http://localhost:62385/?id=huskyhuddle id = id.IsEmpty() ? "samsonau" : id; var tc = new TumblrClient(new HmacSha1HashProvider(), "z35mVaZpxg4JC7IdJ1KB1GHYOqYXfwwnv0cIOlHafuMUZ7Tlat", "gn3rVOlvrYVjLZP4tYnxb1FXYNiFGFZt3twWotz84VeYb9oWbj"); var qq = tc.GetPostsAsync(id); qq.Wait(); var q = qq.Result; Photo[] photoSet = ((PhotoPost)(q.Result[0])).PhotoSet; return(View(photoSet)); }