Skip to content

Commit

Permalink
Change state saving to return stream
Browse files Browse the repository at this point in the history
  • Loading branch information
a-legotin committed Oct 28, 2017
1 parent bf69dbb commit c42720d
Show file tree
Hide file tree
Showing 27 changed files with 235 additions and 191 deletions.
2 changes: 1 addition & 1 deletion InstaSharper.Examples/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
</configuration>
</configuration>
11 changes: 7 additions & 4 deletions InstaSharper.Examples/InstaSharper.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="InstaSharper, Version=1.2.6.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\InstaSharper\bin\Debug\net452\InstaSharper.dll</HintPath>
<Reference Include="InstaSharper, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\InstaSharper\bin\release\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>
<HintPath>packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -54,9 +55,11 @@
<Compile Include="Samples\CommentMedia.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Samples\IDemoSample.cs" />
<Compile Include="Samples\SaveLoadState.cs" />
<Compile Include="Samples\Stories.cs" />
<Compile Include="Samples\UploadPhoto.cs" />
<Compile Include="Utils\ConsoleUtils.cs" />
<Compile Include="Utils\DebugLogger.cs" />
<Compile Include="Utils\StringExtensions.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
47 changes: 25 additions & 22 deletions InstaSharper.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using InstaSharper.API;
using InstaSharper.API.Builder;
using InstaSharper.Classes;
using InstaSharper.Classes.Android.DeviceInfo;
using InstaSharper.Examples.Samples;
using InstaSharper.Logger;

Expand All @@ -18,6 +20,9 @@ public class Program
private static void Main(string[] args)
{
var result = Task.Run(MainAsync).GetAwaiter().GetResult();
if (result)
return;
Console.ReadKey();
}

public static async Task<bool> MainAsync()
Expand All @@ -33,12 +38,14 @@ public static async Task<bool> MainAsync()
};

// create new InstaApi instance using Builder
_instaApi = new InstaApiBuilder()
var device = AndroidDeviceGenerator.GetByName(AndroidDevices.SAMSUNG_NOTE3);
var requestMessage = ApiRequestMessage.FromDevice(device);
_instaApi = InstaApiBuilder.CreateBuilder()
.SetUser(userSession)
.UseLogger(new DebugLogger()) // use logger for requests and debug messages
.SetRequestDelay(TimeSpan.FromSeconds(1)) // set delay between requests
.SetApiRequestMessage(requestMessage)
.UseLogger(new DebugLogger(LogLevel.Info)) // use logger for requests and debug messages
.SetRequestDelay(TimeSpan.FromSeconds(2))
.Build();

// login
Console.WriteLine($"Logging in as {userSession.UserName}");
var logInResult = await _instaApi.LoginAsync();
Expand All @@ -51,27 +58,23 @@ public static async Task<bool> MainAsync()
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("Press 4 to start stories demo sample");
Console.WriteLine("Press 5 to start demo with saving state of API instance");

var samplesMap = new Dictionary<ConsoleKey, IDemoSample>
{
[ConsoleKey.D1] = new Basics(_instaApi),
[ConsoleKey.D2] = new UploadPhoto(_instaApi),
[ConsoleKey.D3] = new CommentMedia(_instaApi),
[ConsoleKey.D4] = new Stories(_instaApi),
[ConsoleKey.D5] = new SaveLoadState(_instaApi)
};
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;
}
if (samplesMap.ContainsKey(key.Key))
await samplesMap[key.Key].DoShow();
Console.WriteLine("Done. Press esc key to exit...");

key = Console.ReadKey();
return key.Key == ConsoleKey.Escape;
}
Expand All @@ -88,4 +91,4 @@ public static async Task<bool> MainAsync()
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")]
4 changes: 2 additions & 2 deletions InstaSharper.Examples/Samples/Basics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace InstaSharper.Examples.Samples
{
internal class Basics
internal class Basics : IDemoSample
{
/// <summary>
/// Config values
Expand Down Expand Up @@ -72,4 +72,4 @@ public async Task DoShow()
}
}
}
}
}
4 changes: 2 additions & 2 deletions InstaSharper.Examples/Samples/CommentMedia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

Expand All @@ -21,4 +21,4 @@ public async Task DoShow()
: $"Unable to create comment: {commentResult.Info.Message}");
}
}
}
}
9 changes: 9 additions & 0 deletions InstaSharper.Examples/Samples/IDemoSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace InstaSharper.Examples.Samples
{
internal interface IDemoSample
{
Task DoShow();
}
}
41 changes: 41 additions & 0 deletions InstaSharper.Examples/Samples/SaveLoadState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Threading.Tasks;
using InstaSharper.API;
using InstaSharper.API.Builder;

namespace InstaSharper.Examples.Samples
{
internal class SaveLoadState : IDemoSample
{
private readonly IInstaApi _instaApi;

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

public async Task DoShow()
{
var result = await _instaApi.GetCurrentUserAsync();
if (!result.Succeeded)
{
Console.WriteLine($"Unable to get current user using current API instance: {result.Info}");
return;
}
Console.WriteLine($"Got current user: {result.Value.UserName} using existing API instance");
var stream = _instaApi.GetStateDataAsStream();
var anotherInstance = InstaApiBuilder.CreateBuilder()
.SetRequestDelay(TimeSpan.FromSeconds(2))
.Build();
anotherInstance.LoadStateDataFromStream(stream);
var anotherResult = await anotherInstance.GetCurrentUserAsync();
if (!anotherResult.Succeeded)
{
Console.WriteLine($"Unable to get current user using current API instance: {result.Info}");
return;
}
Console.WriteLine(
$"Got current user: {anotherResult.Value.UserName} using new API instance without re-login");
}
}
}
35 changes: 35 additions & 0 deletions InstaSharper.Examples/Samples/Stories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using InstaSharper.API;

namespace InstaSharper.Examples.Samples
{
internal class Stories : IDemoSample
{
private readonly IInstaApi _instaApi;

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

public async Task DoShow()
{
var result = await _instaApi.GetStoryFeedAsync();
if (!result.Succeeded)
{
Console.WriteLine($"Unable to get story feed: {result.Info}");
return;
}
var storyFeed = result.Value;
Console.WriteLine($"Got {storyFeed.Items.Count} story reels.");
foreach (var feedItem in storyFeed.Items)
{
Console.WriteLine($"User: {feedItem.User.FullName}");
foreach (var item in feedItem.Items)
Console.WriteLine(
$"Story item: {item.Caption?.Text ?? item.Code}, images:{item.ImageList?.Count ?? 0}, videos: {item.VideoList?.Count ?? 0}");
}
}
}
}
6 changes: 3 additions & 3 deletions InstaSharper.Examples/Samples/UploadPhoto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

Expand All @@ -17,7 +17,7 @@ public UploadPhoto(IInstaApi instaApi)

public async Task DoShow()
{
var mediaImage = new MediaImage
var mediaImage = new InstaImage
{
Height = 1080,
Width = 1080,
Expand All @@ -29,4 +29,4 @@ public async Task DoShow()
: $"Unable to upload photo: {result.Info.Message}");
}
}
}
}
2 changes: 1 addition & 1 deletion InstaSharper.Examples/Utils/ConsoleUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,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}");
}
}
}
}
100 changes: 0 additions & 100 deletions InstaSharper.Examples/Utils/DebugLogger.cs

This file was deleted.

2 changes: 1 addition & 1 deletion InstaSharper.Examples/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public static string Truncate(this string value, int maxChars)
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
}
}
}
Loading

0 comments on commit c42720d

Please sign in to comment.