Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
+ Fixes #2 LIMIT_INVALID error
Browse files Browse the repository at this point in the history
+ Updating TLSharp library
  • Loading branch information
salarcode committed Jan 9, 2018
1 parent 49c8eb6 commit 02df889
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
10 changes: 4 additions & 6 deletions ExportTelegramContacts/ExportTelegramContacts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="BigMath, Version=0.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TLSharp.0.1.0.270\lib\net45\BigMath.dll</HintPath>
<HintPath>..\packages\TLSharp.0.1.0.328\lib\net45\BigMath.dll</HintPath>
</Reference>
<Reference Include="Ionic.Zip, Version=1.9.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.9.3\lib\net20\Ionic.Zip.dll</HintPath>
</Reference>
<Reference Include="Ionic.ZLib, Version=2.0.0.14, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TLSharp.0.1.0.270\lib\net45\Ionic.ZLib.dll</HintPath>
<HintPath>..\packages\TLSharp.0.1.0.328\lib\net45\Ionic.ZLib.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand All @@ -52,12 +52,10 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="TeleSharp.TL, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\ASP.NET\TLSharp\TLSharp\TLSharp.Core\bin\Debug\TeleSharp.TL.dll</HintPath>
<HintPath>..\packages\TLSharp.0.1.0.328\lib\net45\TeleSharp.TL.dll</HintPath>
</Reference>
<Reference Include="TLSharp.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\ASP.NET\TLSharp\TLSharp\TLSharp.Core\bin\Debug\TLSharp.Core.dll</HintPath>
<HintPath>..\packages\TLSharp.0.1.0.328\lib\net45\TLSharp.Core.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
84 changes: 55 additions & 29 deletions ExportTelegramContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,39 +117,39 @@ private static async Task CallExportContacts()

var contacts = (await TClient.GetContactsAsync()) as TLContacts;

Console.WriteLine($"Number of contacts: {contacts.users.lists.Count}");
Console.WriteLine($"Number of contacts: {contacts.Users.Count}");

var fileName = $"ExportedContacts\\Exported-{DateTime.Now.ToString("yyyy-MM-dd HH-mm.ss")}.vcf";
var fileNameWihContacts = $"ExportedContacts\\Exported-WithPhoto-{DateTime.Now.ToString("yyyy-MM-dd HH-mm.ss")}.vcf";

Directory.CreateDirectory("ExportedContacts");

Console.Write($"Don't export contacts without phone? [y/n] ");
Console.Write($"Export contacts without phone? [y/n] ");
var filterResult = Console.ReadLine() ?? "";
var dontExport = filterResult == "" || filterResult.ToLower() == "y";
var dontExport = !(filterResult == "" || filterResult.ToLower() == "n");

Console.WriteLine($"Writing to: {fileName}");
using (var file = File.Create(fileName))
using (var stringWrite = new StreamWriter(file))
{
var savedCount = 0;
foreach (var user in contacts.users.lists.OfType<TLUser>())
foreach (var user in contacts.Users.OfType<TLUser>())
{
if (dontExport)
{
if (string.IsNullOrWhiteSpace(user.phone))
if (string.IsNullOrWhiteSpace(user.Phone))
continue;
}

//vCard Begin
stringWrite.WriteLine("BEGIN:VCARD");
stringWrite.WriteLine("VERSION:2.1");
//Name
stringWrite.WriteLine("N:" + user.last_name + ";" + user.first_name);
stringWrite.WriteLine("N:" + user.LastName + ";" + user.FirstName);
//Full Name
stringWrite.WriteLine("FN:" + user.first_name + " " +
/* nameMiddle + " " +*/ user.last_name);
stringWrite.WriteLine("TEL;CELL:" + ConvertFromTelegramPhoneNumber(user.phone));
stringWrite.WriteLine("FN:" + user.FirstName + " " +
/* nameMiddle + " " +*/ user.LastName);
stringWrite.WriteLine("TEL;CELL:" + ConvertFromTelegramPhoneNumber(user.Phone));

//vCard End
stringWrite.WriteLine("END:VCARD");
Expand Down Expand Up @@ -177,36 +177,35 @@ private static async Task CallExportContacts()


var savedCount = 0;
foreach (var user in contacts.users.lists.OfType<TLUser>())
foreach (var user in contacts.Users.OfType<TLUser>())
{
if (dontExport)
{
if (string.IsNullOrWhiteSpace(user.phone))
if (string.IsNullOrWhiteSpace(user.Phone))
continue;
}

string userPhotoString = null;
try
{
var userPhoto = user.photo as TLUserProfilePhoto;
var userPhoto = user.Photo as TLUserProfilePhoto;
if (userPhoto != null)
{
var photo = userPhoto.photo_big as TLFileLocation;
var photo = userPhoto.PhotoBig as TLFileLocation;
if (saveSmallImages)
photo = userPhoto.photo_small as TLFileLocation;
photo = userPhoto.PhotoSmall as TLFileLocation;

if (photo != null)
{
Console.Write($"Reading prfile image for: {user.first_name} {user.last_name}...");
var fileResult = await TClient.GetFile(new TLInputFileLocation()
{
local_id = photo.local_id,
secret = photo.secret,
volume_id = photo.volume_id
},
filePartSize: -1);

var smallPhotoBytes = fileResult.bytes;
Console.Write($"Reading prfile image for: {user.FirstName} {user.LastName}...");

var smallPhotoBytes = await GetFile(TClient,
new TLInputFileLocation()
{
LocalId = photo.LocalId,
Secret = photo.Secret,
VolumeId = photo.VolumeId
});

// resize if it is the big image
if (!saveSmallImages)
Expand Down Expand Up @@ -234,11 +233,11 @@ private static async Task CallExportContacts()
stringWrite.WriteLine("BEGIN:VCARD");
stringWrite.WriteLine("VERSION:2.1");
//Name
stringWrite.WriteLine("N:" + user.last_name + ";" + user.first_name);
stringWrite.WriteLine("N:" + user.LastName + ";" + user.FirstName);
//Full Name
stringWrite.WriteLine("FN:" + user.first_name + " " +
/* nameMiddle + " " +*/ user.last_name);
stringWrite.WriteLine("TEL;CELL:" + ConvertFromTelegramPhoneNumber(user.phone));
stringWrite.WriteLine("FN:" + user.FirstName + " " +
/* nameMiddle + " " +*/ user.LastName);
stringWrite.WriteLine("TEL;CELL:" + ConvertFromTelegramPhoneNumber(user.Phone));

if (userPhotoString != null)
{
Expand Down Expand Up @@ -266,6 +265,33 @@ private static async Task CallExportContacts()
}
}

private static async Task<byte[]> GetFile(TelegramClient client, TLInputFileLocation file)
{
int filePart = 512 * 1024;
int offset = 0;

using (var mem = new MemoryStream())
{
while (true)
{
var resFile = await client.GetFile(
file,
filePart, offset);

mem.Write(resFile.Bytes, 0, resFile.Bytes.Length);
offset += filePart;
var readCount = resFile.Bytes.Length;

#if DEBUG
Console.Write($" ... read {readCount} of {filePart} .");
#endif
if (readCount < filePart)
break;
}
return mem.ToArray();
}
}



public static string ConvertFromTelegramPhoneNumber(string number)
Expand Down Expand Up @@ -303,7 +329,7 @@ private static async Task CallAuthenicate()
{
TUser = await TClient.MakeAuthAsync(phoneNumber, requestHash, authCode);

Console.WriteLine($"Authenicaion was successfull for Person Name:{TUser.first_name + " " + TUser.last_name}, Username={TUser.username}");
Console.WriteLine($"Authenicaion was successfull for Person Name:{TUser.FirstName + " " + TUser.LastName}, Username={TUser.Username}");
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions ExportTelegramContacts/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
2 changes: 1 addition & 1 deletion ExportTelegramContacts/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<package id="BigMath" version="0.5.0" targetFramework="net45" />
<package id="DotNetZip" version="1.9.3" targetFramework="net45" />
<package id="MarkerMetro.Unity.Ionic.Zlib" version="2.0.0.14" targetFramework="net45" />
<package id="TLSharp" version="0.1.0.270" targetFramework="net45" />
<package id="TLSharp" version="0.1.0.328" targetFramework="net47" />
</packages>

0 comments on commit 02df889

Please sign in to comment.