telegram and instagram wrappers, DtsodFile

This commit is contained in:
2023-02-23 18:33:47 +06:00
parent adbebc37ec
commit 327d06b3d0
12 changed files with 436 additions and 238 deletions

View File

@@ -0,0 +1,37 @@
using System.Net.Http;
using InstaSharper.Logger;
namespace InstaFollowersOverseer.Instagram;
public class InstagramApiLogger : IInstaLogger
{
public ContextLogger _logger = new("api", InstagramWrapper.InstagramLogger);
public void LogRequest(HttpRequestMessage r)
{
_logger.LogDebug("http",$"request {r.Method.Method.ToUpper()} from {r.RequestUri}:\n"
+ r.Content?.ReadAsStringAsync().GetAwaiter().GetResult());
}
public void LogRequest(Uri uri)
{
}
public void LogResponse(HttpResponseMessage r)
{
_logger.LogDebug("http",$"responce from " +
(r.RequestMessage!=null && r.RequestMessage.RequestUri!=null ? r.RequestMessage.RequestUri.ToString() : "unknown")
+ $" :\n "+ r.Content.ReadAsStringAsync().GetAwaiter().GetResult());
}
public void LogException(Exception ex)
{
_logger.LogError(ex);
}
public void LogInfo(string info)
{
_logger.LogInfo(info);
}
}

View File

@@ -0,0 +1,58 @@
using InstaSharper.API;
using InstaSharper.API.Builder;
using InstaSharper.Classes;
using InstaSharper.Classes.Models;
namespace InstaFollowersOverseer.Instagram;
public static class InstagramWrapper
{
public static ContextLogger InstagramLogger = new("instagram",ParentLogger);
private static IInstaApi Api=null!;
public static async void Init()
{
try
{
InstagramLogger.LogInfo("initializing instagram wrapper");
if (CurrentConfig is null)
throw new NullReferenceException("config is null");
var apiLogger = new InstagramApiLogger();
// disabling http request/responce logging
apiLogger._logger.DebugLogEnabled = false;
Api = InstaApiBuilder.CreateBuilder()
.UseLogger(apiLogger)
.SetUser(new UserSessionData
{
UserName = CurrentConfig.instagramLogin,
Password = CurrentConfig.instagramPassword
})
.SetRequestDelay(RequestDelay.FromSeconds(0, 1))
.Build();
InstagramLogger.LogInfo("instagram login starting");
var rezult= await Api.LoginAsync();
if (!rezult.Succeeded)
throw new Exception("login exception:\n" + rezult.Info + '\n' + rezult.Value);
InstagramLogger.LogInfo("instagram wrapper have initialized and connected successfully");
}
catch (OperationCanceledException) {}
catch (Exception ex)
{
InstagramLogger.LogError("init", ex);
Program.Stop();
}
}
public static async Task<InstaUser?> GetUserAsync(string usernameOrUrl)
{
// url
if (usernameOrUrl.Contains('/'))
{
throw new NotImplementedException("get user by url");
}
// username
var u=await Api.GetUserAsync(usernameOrUrl);
return u.Succeeded ? u.Value : null;
}
}