Skip to content

Commit

Permalink
some more samples added
Browse files Browse the repository at this point in the history
  • Loading branch information
a-legotin committed May 5, 2017
1 parent 9042410 commit 54012fc
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 95 deletions.
90 changes: 0 additions & 90 deletions InstaSharper.Examples/Basics.cs

This file was deleted.

6 changes: 5 additions & 1 deletion InstaSharper.Examples/InstaSharper.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Basics.cs" />
<Compile Include="Samples\Basics.cs" />
<Compile Include="Samples\CommentMedia.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Samples\UploadPhoto.cs" />
<Compile Include="Utils\ConsoleUtils.cs" />
<Compile Include="Utils\StringExtensions.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
66 changes: 66 additions & 0 deletions InstaSharper.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InstaSharper.API;
using InstaSharper.API.Builder;
using InstaSharper.Classes;
using InstaSharper.Examples.Samples;

namespace InstaSharper.Examples
{
public class Program
{
/// <summary>
/// Api instance (one instance per Instagram user)
/// </summary>
private static IInstaApi _instaApi;

private static void Main(string[] args)
{
Console.WriteLine("Starting demo of InstaSharper project");
// create user session data and provide login details
var userSession = new UserSessionData
{
UserName = "username",
Password = "password"
};
// create new InstaApi instance using Builder
_instaApi = new InstaApiBuilder()
.SetUser(userSession)
.Build();
// login
var logInResult = _instaApi.Login();
if (!logInResult.Succeeded)
{
Console.WriteLine($"Unable to login: {logInResult.Info.Message}");
}
else
{
Console.WriteLine("Press 1 to start basic demo samples");
Console.WriteLine("Press 2 to start upload photo demo sample");
Console.WriteLine("Press 3 to start comment media demo sample");

var key = Console.ReadKey();
switch (key.Key)
{
case ConsoleKey.D1:
new Basics(_instaApi).DoShow();
break;
case ConsoleKey.D2:
new UploadPhoto(_instaApi).DoShow();
break;
case ConsoleKey.D3:
new CommentMedia(_instaApi).DoShow();
break;
default:
break;
}
var logoutResult = _instaApi.Logout();
if (logoutResult.Value) Console.WriteLine("Logout succeed");
}
Console.ReadKey();
}
}
}
69 changes: 69 additions & 0 deletions InstaSharper.Examples/Samples/Basics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Linq;
using InstaSharper.API;
using InstaSharper.Examples.Utils;

namespace InstaSharper.Examples.Samples
{
internal class Basics
{
/// <summary>
/// Config values
/// </summary>
private static readonly int _maxDescriptionLength = 20;

private readonly IInstaApi _instaApi;

public Basics(IInstaApi instaApi)
{
_instaApi = instaApi;
}

public void DoShow()
{
// get currently logged in user
var currentUser = _instaApi.GetCurrentUser().Value;
Console.WriteLine($"Logged in: username - {currentUser.UserName}, full name - {currentUser.FullName}");

// get self followers
var followers = _instaApi.GetUserFollowersAsync(currentUser.UserName, 5).Result.Value;
Console.WriteLine($"Count of followers [{currentUser.UserName}]:{followers.Count}");

// get self user's media, latest 5 pages
var currentUserMedia = _instaApi.GetUserMedia(currentUser.UserName, 5);
if (currentUserMedia.Succeeded)
{
Console.WriteLine($"Media count [{currentUser.UserName}]: {currentUserMedia.Value.Count}");
foreach (var media in currentUserMedia.Value)
ConsoleUtils.PrintMedia("Self media", media, _maxDescriptionLength);
}

//get user time line feed, latest 5 pages
var userFeed = _instaApi.GetUserTimelineFeed(5);
if (userFeed.Succeeded)
{
Console.WriteLine(
$"Feed items (in {userFeed.Value.Pages} pages) [{currentUser.UserName}]: {userFeed.Value.Medias.Count}");
foreach (var media in userFeed.Value.Medias)
ConsoleUtils.PrintMedia("Feed media", media, _maxDescriptionLength);
//like first 10 medias from user timeline feed
foreach (var media in userFeed.Value.Medias.Take(10))
{
var likeResult = _instaApi.LikeMedia(media.InstaIdentifier);
var resultString = likeResult.Value ? "liked" : "not liked";
Console.WriteLine($"Media {media.Code} {resultString}");
}
}

// get tag feed, latest 5 pages
var tagFeed = _instaApi.GetTagFeed("quadcopter", 5);
if (tagFeed.Succeeded)
{
Console.WriteLine(
$"Tag feed items (in {tagFeed.Value.Pages} pages) [{currentUser.UserName}]: {tagFeed.Value.Medias.Count}");
foreach (var media in tagFeed.Value.Medias)
ConsoleUtils.PrintMedia("Tag feed", media, _maxDescriptionLength);
}
}
}
}
23 changes: 23 additions & 0 deletions InstaSharper.Examples/Samples/CommentMedia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using InstaSharper.API;

namespace InstaSharper.Examples.Samples
{
internal class CommentMedia
{
private readonly IInstaApi _instaApi;

public CommentMedia(IInstaApi instaApi)
{
_instaApi = instaApi;
}

public void DoShow()
{
var commentResult = _instaApi.CommentMedia("", "Hi there!");
Console.WriteLine(commentResult.Succeeded
? $"Comment created: {commentResult.Value.Pk}, text: {commentResult.Value.Text}"
: $"Unable to create comment: {commentResult.Info.Message}");
}
}
}
31 changes: 31 additions & 0 deletions InstaSharper.Examples/Samples/UploadPhoto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.IO;
using InstaSharper.API;
using InstaSharper.Classes.Models;

namespace InstaSharper.Examples.Samples
{
internal class UploadPhoto
{
private readonly IInstaApi _instaApi;

public UploadPhoto(IInstaApi instaApi)
{
_instaApi = instaApi;
}

public void DoShow()
{
var mediaImage = new MediaImage
{
Height = 1080,
Width = 1080,
URI = new Uri(Path.GetFullPath(@"c:\someawesomepicture.jpg"), UriKind.Absolute).LocalPath
};
var result = _instaApi.UploadPhoto(mediaImage, "someawesomepicture");
Console.WriteLine(result.Succeeded
? $"Media created: {result.Value.Pk}, {result.Value.Caption}"
: $"Unable to upload photo: {result.Info.Message}");
}
}
}
18 changes: 18 additions & 0 deletions InstaSharper.Examples/Utils/ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InstaSharper.Classes.Models;

namespace InstaSharper.Examples.Utils
{
public static class ConsoleUtils
{
public static void PrintMedia(string header, InstaMedia media, int maxDescriptionLength)
{
Console.WriteLine(
$"{header} [{media.User.UserName}]: {media.Caption?.Text.Truncate(maxDescriptionLength)}, {media.Code}, likes: {media.LikesCount}, multipost: {media.IsMultiPost}");
}
}
}
2 changes: 1 addition & 1 deletion InstaSharper.Examples/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public static string Truncate(this string value, int maxChars)
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
}
}
}
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ Currently the library supports following coverage of the following Instagram API
- [x] Delete comment
- [x] Upload photo

- [] Get followings list
- [ ] Get followings list

* Get user list autocomplete
* Register new user
* Get megaphone log
* Explore feed

* Upload video
* Upload story
* Get full account backup
Expand Down Expand Up @@ -123,7 +122,7 @@ IResult<InstaFeed> feed = await api.GetUserFeedAsync();
IResult<bool> postResult = await apiInstance.CommentMediaAsync("1234567891234567891_123456789", "Hi there!");
```

######for more samples you can look for [Examples folder](https://github.com/a-legotin/InstaSharper/tree/master/InstaSharper.Examples)
#####for more samples you can look for [Examples folder](https://github.com/a-legotin/InstaSharper/tree/master/InstaSharper.Examples)

#### [Why two separate repos with same mission?](https://github.com/a-legotin/InstagramAPI-Web/wiki/Difference-between-API-Web-and-just-API-repositories)
Expand Down

0 comments on commit 54012fc

Please sign in to comment.