Skip to content

Commit

Permalink
Merge pull request #59 from a-legotin/develop
Browse files Browse the repository at this point in the history
1.2.6
  • Loading branch information
a-legotin authored Aug 19, 2017
2 parents 6fe6395 + 1e8140e commit 350c43c
Show file tree
Hide file tree
Showing 177 changed files with 983 additions and 571 deletions.
4 changes: 2 additions & 2 deletions InstaSharper.Examples/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
</configuration>
</configuration>
12 changes: 9 additions & 3 deletions InstaSharper.Examples/InstaSharper.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>InstaSharper.Examples</RootNamespace>
<AssemblyName>InstaSharper.Examples</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -33,8 +34,11 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="InstaSharper">
<HintPath>..\InstaSharper\bin\Debug\net452\InstaSharper.dll</HintPath>
<Reference Include="InstaSharper, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\InstaSharper.1.2.6\lib\net452\InstaSharper.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -46,6 +50,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Utils\DebugFileLogger.cs" />
<Compile Include="Samples\Basics.cs" />
<Compile Include="Samples\CommentMedia.cs" />
<Compile Include="Program.cs" />
Expand All @@ -56,6 +61,7 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
106 changes: 65 additions & 41 deletions InstaSharper.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InstaSharper.API;
using InstaSharper.API.Builder;
Expand All @@ -19,48 +16,75 @@ public class Program

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
var result = Task.Run(MainAsync).GetAwaiter().GetResult();
}

public static async Task<bool> MainAsync()
{
try
{
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");
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)
.UseLogger(new DebugFileLogger()) // use logger for requests and debug messages
.SetRequestDelay(TimeSpan.FromSeconds(1)) // set delay between requests
.Build();

var key = Console.ReadKey();
switch (key.Key)
// login
Console.WriteLine($"Logging in as {userSession.UserName}");
var logInResult = await _instaApi.LoginAsync();
if (!logInResult.Succeeded)
{
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;
Console.WriteLine($"Unable to login: {logInResult.Info.Message}");
}
var logoutResult = _instaApi.Logout();
if (logoutResult.Value) Console.WriteLine("Logout succeed");
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();
Console.WriteLine(Environment.NewLine);
switch (key.Key)
{
case ConsoleKey.D1:
var basics = new Basics(_instaApi);
await basics.DoShow();
break;
case ConsoleKey.D2:
var upload = new UploadPhoto(_instaApi);
await upload.DoShow();
break;
case ConsoleKey.D3:
var comment = new CommentMedia(_instaApi);
await comment.DoShow();
break;
default:
break;
}
Console.WriteLine("Done. Press esc key to exit...");
key = Console.ReadKey();
return key.Key == ConsoleKey.Escape;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
var logoutResult = Task.Run(() => _instaApi.LogoutAsync()).GetAwaiter().GetResult();
if (logoutResult.Succeeded) Console.WriteLine("Logout succeed");
}
Console.ReadKey();
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion InstaSharper.Examples/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
32 changes: 19 additions & 13 deletions InstaSharper.Examples/Samples/Basics.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using InstaSharper.API;
using InstaSharper.Examples.Utils;

Expand All @@ -19,51 +20,56 @@ public Basics(IInstaApi instaApi)
_instaApi = instaApi;
}

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

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

// get self folling
var following = await _instaApi.GetUserFollowingAsync(currentUser.Value.UserName, 5);
Console.WriteLine($"Count of following [{currentUser.Value.UserName}]:{following.Value.Count}");

// get self user's media, latest 5 pages
var currentUserMedia = _instaApi.GetUserMedia(currentUser.UserName, 5);
var currentUserMedia = await _instaApi.GetUserMediaAsync(currentUser.Value.UserName, 5);
if (currentUserMedia.Succeeded)
{
Console.WriteLine($"Media count [{currentUser.UserName}]: {currentUserMedia.Value.Count}");
Console.WriteLine($"Media count [{currentUser.Value.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);
var userFeed = await _instaApi.GetUserTimelineFeedAsync(5);
if (userFeed.Succeeded)
{
Console.WriteLine(
$"Feed items (in {userFeed.Value.Pages} pages) [{currentUser.UserName}]: {userFeed.Value.Medias.Count}");
$"Feed items (in {userFeed.Value.Pages} pages) [{currentUser.Value.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 likeResult = await _instaApi.LikeMediaAsync(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);
var tagFeed = await _instaApi.GetTagFeedAsync("quadcopter", 5);
if (tagFeed.Succeeded)
{
Console.WriteLine(
$"Tag feed items (in {tagFeed.Value.Pages} pages) [{currentUser.UserName}]: {tagFeed.Value.Medias.Count}");
$"Tag feed items (in {tagFeed.Value.Pages} pages) [{currentUser.Value.UserName}]: {tagFeed.Value.Medias.Count}");
foreach (var media in tagFeed.Value.Medias)
ConsoleUtils.PrintMedia("Tag feed", media, _maxDescriptionLength);
}
}
}
}
}
9 changes: 5 additions & 4 deletions InstaSharper.Examples/Samples/CommentMedia.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Threading.Tasks;
using InstaSharper.API;

namespace InstaSharper.Examples.Samples
Expand All @@ -12,12 +13,12 @@ public CommentMedia(IInstaApi instaApi)
_instaApi = instaApi;
}

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

Expand All @@ -14,18 +15,18 @@ public UploadPhoto(IInstaApi instaApi)
_instaApi = instaApi;
}

public void DoShow()
public async Task 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");
var result = await _instaApi.UploadPhotoAsync(mediaImage, "someawesomepicture");
Console.WriteLine(result.Succeeded
? $"Media created: {result.Value.Pk}, {result.Value.Caption}"
: $"Unable to upload photo: {result.Info.Message}");
}
}
}
}
6 changes: 1 addition & 5 deletions InstaSharper.Examples/Utils/ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InstaSharper.Classes.Models;

namespace InstaSharper.Examples.Utils
Expand All @@ -15,4 +11,4 @@ public static void PrintMedia(string header, InstaMedia media, int maxDescriptio
$"{header} [{media.User.UserName}]: {media.Caption?.Text.Truncate(maxDescriptionLength)}, {media.Code}, likes: {media.LikesCount}, multipost: {media.IsMultiPost}");
}
}
}
}
27 changes: 27 additions & 0 deletions InstaSharper.Examples/Utils/DebugFileLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.IO;
using System.Threading.Tasks;
using InstaSharper.Logger;

namespace InstaSharper.Examples
{
internal class DebugFileLogger : ILogger
{
private const string logFile = "log.txt";

public void Write(string logMessage)
{
File.AppendAllText(logFile, logMessage);
}

public async Task WriteAsync(string logMessage)
{
await Task.Run(() => File.AppendAllText(logFile, logMessage));
}

public void WriteLine(string logMessage)
{
Write(logMessage + Environment.NewLine);
}
}
}
10 changes: 2 additions & 8 deletions InstaSharper.Examples/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InstaSharper.Examples.Utils
namespace InstaSharper.Examples.Utils
{
public static class StringExtensions
{
Expand All @@ -13,4 +7,4 @@ public static string Truncate(this string value, int maxChars)
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
}
}
}
6 changes: 6 additions & 0 deletions InstaSharper.Examples/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="InstaSharper" version="1.2.6" targetFramework="net47" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net47" />
</packages>
2 changes: 1 addition & 1 deletion InstaSharper.Tests/Classes/AuthenticatedTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public string GetPassword()
return _password;
}
}
}
}
2 changes: 1 addition & 1 deletion InstaSharper.Tests/Classes/RunnableInDebugOnlyFact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public RunnableInDebugOnlyFact()
Skip = "This fact only running in interactive mode.";
}
}
}
}
Loading

0 comments on commit 350c43c

Please sign in to comment.