Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ilGianfri committed Nov 23, 2024
1 parent e37d367 commit 9ca34ad
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Please read the known issues and FAQ sections before using this tool.
# Known issues
- I made this app for myself, I tried to include some useful filters but it may not work for everyone. Code might not be the prettiest.
- I only tested this with my own tweet archive. If you encounter any issues, open an issue or even better, submit a PR!
- Sometimes hashtags are not imported correctly, the highlighting will be off. We use the indexes from the Twitter archive to highlight hashtags.
- Rate limiting is not handled properly. If you have a lot of tweets, you might get rate limited and it will not be handled gracefully.

This comes with **absolutely no warranty**.
Expand Down
46 changes: 23 additions & 23 deletions src/TwitterSky/TweetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ await Task.Run(() =>
initialCount = _tweetArchive.Count;
}

// Find tweets that are replies to other tweets in the archive
if (_options.ImportThreads)
{
_cmd.PrintInfo("Finding replies to other tweets in the archive... (threads you posted, no not the meta app, replies to yourself)");
// Find all tweets that are replies to other tweets in the archive
List<TweetArchiveModel> replies = _tweetArchive.Where(x => !string.IsNullOrEmpty(x.Tweet.InReplyToStatusId)).ToList();
foreach (TweetArchiveModel reply in replies)
{
// Find the parent tweet in the archive
TweetArchiveModel? parentTweet = _tweetArchive.FirstOrDefault(x => x.Tweet.Id == reply.Tweet.InReplyToStatusId);
if (parentTweet is not null)
{
// Add the reply to dictionary to keep track of them
_cmd.PrintInfo($"Found reply to tweet {reply.Tweet.InReplyToStatusId} in the archive.", true);
_tweetIdToBskyId.TryAdd(reply.Tweet.Id, null);
}
}
_cmd.PrintInfo($"Found {_tweetIdToBskyId.Count} replies to other tweets in the archive.");
}

// Filter out replies if the user doesn't want them
if (!_options.ImportReplies)
{
Expand All @@ -128,7 +148,7 @@ await Task.Run(() =>
// Find all tweets that are not replies or are replies to one of the user's handles
_tweetArchive = _tweetArchive.Where(x => string.IsNullOrEmpty(x.Tweet.InReplyToStatusId) // Not a reply
//|| _twitterHandles.Any(y => x.Tweet.FullText.StartsWith($"@{y}", StringComparison.OrdinalIgnoreCase)) // Reply to one of the user's handles
|| x.Tweet.InReplyToScreenName is null || _twitterHandles.Contains(x.Tweet.InReplyToScreenName)).ToList(); // Reply to one of the user's handles
&& (x.Tweet.InReplyToScreenName is null || _twitterHandles.Contains(x.Tweet.InReplyToScreenName))).ToList(); // Reply to one of the user's handles

_cmd.PrintInfo($"Removed {initialCount - _tweetArchive.Count} replies.");
initialCount = _tweetArchive.Count;
Expand Down Expand Up @@ -166,31 +186,11 @@ await Task.Run(() =>
{
_cmd.PrintInfo("Removing tweets before the last parsed tweet...", true);
// Once we find the last parsed tweet, we need to remove it and all the previous ones
_tweetArchive = _tweetArchive.Where(x => Convert.ToUInt64(x.Tweet.Id) > Convert.ToUInt64(_lastParsedTweetId)).ToList();
_tweetArchive = _tweetArchive.Where(x => Convert.ToUInt64(x.Tweet.Id) > Convert.ToUInt64(_lastParsedTweetId) || _tweetIdToBskyId.ContainsKey(x.Tweet.Id)).ToList();
_cmd.PrintInfo($"Removed {initialCount - _tweetArchive.Count} tweets before the last parsed tweet.");
initialCount = _tweetArchive.Count;
}

// Find tweets that are replies to other tweets in the archive
if (_options.ImportThreads)
{
_cmd.PrintInfo("Finding replies to other tweets in the archive... (threads you posted, no not the meta app, replies to yourself)");
// Find all tweets that are replies to other tweets in the archive
List<TweetArchiveModel> replies = _tweetArchive.Where(x => !string.IsNullOrEmpty(x.Tweet.InReplyToStatusId)).ToList();
foreach (TweetArchiveModel reply in replies)
{
// Find the parent tweet in the archive
TweetArchiveModel? parentTweet = _tweetArchive.FirstOrDefault(x => x.Tweet.Id == reply.Tweet.InReplyToStatusId);
if (parentTweet is not null)
{
// Add the reply to dictionary to keep track of them
_cmd.PrintInfo($"Found reply to tweet {reply.Tweet.InReplyToStatusId} in the archive.", true);
_tweetIdToBskyId.TryAdd(reply.Tweet.Id, null);
}
}
_cmd.PrintInfo($"Found {_tweetIdToBskyId.Count} replies to other tweets in the archive.");
}

if (!string.IsNullOrEmpty(_options.SkipWords))
{
_cmd.PrintInfo("Removing tweets containing skip words...", true);
Expand Down Expand Up @@ -265,7 +265,7 @@ private List<Facet> GetHashtags(string tweetContent, List<Hashtag>? hashtags)
{
// If the indices are not present, we need to find the start and end of the hashtag text.
// This is done as a "ByteSlice."
int promptStart = tweetContent.IndexOf(hashtag.Text, StringComparison.InvariantCulture);
int promptStart = tweetContent.IndexOf($"#{hashtag.Text}", StringComparison.InvariantCulture);
int promptEnd = promptStart + Encoding.Default.GetBytes(hashtag.Text).Length;
index = new(promptStart, promptEnd);
}
Expand Down
2 changes: 1 addition & 1 deletion src/TwitterSky/TwitterSky.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions src/TwitterSky/Utilities/CmdUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Pastel;
using System.Text;

namespace TwitterSky.Utilities
{
Expand All @@ -16,6 +17,8 @@ public class CmdUtil
public CmdUtil(bool isVerboseEnabled)
{
_isVerboseEnabled = isVerboseEnabled;

Console.OutputEncoding = Encoding.UTF8;
}

/// <summary>
Expand All @@ -27,7 +30,7 @@ public void PrintWarning(string message, bool isVerboseOnly = false)
{
if (!isVerboseOnly || isVerboseOnly && _isVerboseEnabled)
{
Console.WriteLine($"{DateTime.Now:hh:mm} {"warning:\t".Pastel(ConsoleColor.Yellow)}{message}");
Console.WriteLine($"{DateTime.Now:hh:mm} {"warning:\t\t".Pastel(ConsoleColor.Yellow)}{message}");
}
}

Expand All @@ -50,7 +53,7 @@ public void PrintSuccess(string message, bool isVerboseOnly = false)
{
if (!isVerboseOnly || isVerboseOnly && _isVerboseEnabled)
{
Console.WriteLine($"{DateTime.Now:hh:mm} {"success:\t".Pastel(ConsoleColor.Green)}{message}");
Console.WriteLine($"{DateTime.Now:hh:mm} {"success:\t\t".Pastel(ConsoleColor.Green)}{message}");
}
}

Expand Down

0 comments on commit 9ca34ad

Please sign in to comment.