Skip to content

Commit

Permalink
Merge pull request #27 from a-legotin/develop
Browse files Browse the repository at this point in the history
1.2.4
  • Loading branch information
a-legotin authored May 6, 2017
2 parents a8a4737 + 54012fc commit 0417b8f
Show file tree
Hide file tree
Showing 108 changed files with 1,682 additions and 532 deletions.
16 changes: 5 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
language: csharp
solution: InstaSharper.sln
dotnet: 1.0.0-preview2-003121
sudo: required
os: linux
dist: trusty
dotnet: 1.0.1
mono: none
solution: InstaSharper.sln

script:
# dotnet info
- cd InstaSharper
- dotnet --info
# Run dotnet new
- dotnet restore
- cd InstaSharper
- dotnet build
- cd ../InstaSharper.Tests
- dotnet build
- dotnet test -parallel none
- dotnet build --framework netstandard1.6
7 changes: 6 additions & 1 deletion InstaSharper.Examples/InstaSharper.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="InstaSharperExamples.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>
<None Include="App.config" />
Expand Down
64 changes: 0 additions & 64 deletions InstaSharper.Examples/InstaSharperExamples.cs

This file was deleted.

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}");
}
}
}
16 changes: 16 additions & 0 deletions InstaSharper.Examples/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InstaSharper.Examples.Utils
{
public static class StringExtensions
{
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
}
}
16 changes: 9 additions & 7 deletions InstaSharper.Tests/Endpoints/FollowersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public async void GetCurrentUserFollwersTest()
Password = password
});
if (!TestHelpers.Login(apiInstance, _output)) return;
if (!TestHelpers.Login(apiInstance, _output)) return;
var result = await apiInstance.GetCurrentUserFollowersAsync();
var followers = result.Value;
//assert
Expand All @@ -60,7 +59,7 @@ public async void GetCurrentUserFollwersTest()

[RunnableInDebugOnlyTheory]
[InlineData(196754384)]
public async void FollowUserTest(long userId)
public async void FollowUnfollowUserTest(long userId)
{
var currentUsername = "alex_codegarage";
var password = Environment.GetEnvironmentVariable("instaapiuserpassword");
Expand All @@ -69,12 +68,15 @@ public async void FollowUserTest(long userId)
UserName = currentUsername,
Password = password
});
if (!TestHelpers.Login(apiInstance, _output)) return;
var result = await apiInstance.FollowUserAsync(userId);
var followers = result.Value;
if (!TestHelpers.Login(apiInstance, _output)) throw new Exception("Not logged in");
var followResult = await apiInstance.FollowUserAsync(userId);
var unFollowResult = await apiInstance.UnFollowUserAsync(userId);
//assert
Assert.True(result.Succeeded);
Assert.NotNull(followers);
Assert.True(followResult.Succeeded);
Assert.True(unFollowResult.Succeeded);

Assert.True(followResult.Value.Following);
Assert.False(unFollowResult.Value.Following);
}
}
}
Loading

0 comments on commit 0417b8f

Please sign in to comment.