public TwitterApi Api { get; }
public TwitterConfiguration Configuration { get; private set; }
+ public TwitterTextConfiguration TextConfiguration { get; private set; }
delegate void GetIconImageDelegate(PostClass post);
private readonly object LockObj = new object();
{
this.Api = api;
this.Configuration = TwitterConfiguration.DefaultConfiguration();
+ this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
}
public TwitterApiAccessLevel AccessLevel
{
this.CheckAccountState();
- if (mediaIds == null &&
- Twitter.DMSendTextRegex.IsMatch(postStr))
+ if (Twitter.DMSendTextRegex.IsMatch(postStr))
{
await this.SendDirectMessage(postStr)
.ConfigureAwait(false);
using (var origImage = item.CreateImage())
{
- MemoryImage newImage;
- if (alphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out newImage))
+ if (alphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
{
using (var newMediaItem = new MemoryImageMediaItem(newImage))
{
}
var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
-
- if (minimumId != null && minimumId.Value < tab.OldestId)
+ if (minimumId != null)
tab.OldestId = minimumId.Value;
}
}
var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
-
- if (minimumId != null && minimumId.Value < tab.OldestId)
+ if (minimumId != null)
tab.OldestId = minimumId.Value;
}
var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
- if (minimumId != null && minimumId.Value < tab.OldestId)
+ if (minimumId != null)
tab.OldestId = minimumId.Value;
}
post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
//Source整形
- var source = ParseSource(sourceHtml);
- post.Source = string.Intern(source.Item1);
- post.SourceUri = source.Item2;
+ var (sourceText, sourceUri) = ParseSource(sourceHtml);
+ post.Source = string.Intern(sourceText);
+ post.SourceUri = sourceUri;
- post.IsReply = post.ReplyToList.Contains(_uname);
+ post.IsReply = post.RetweetedId == null && post.ReplyToList.Contains(_uname);
post.IsExcludeReply = false;
if (post.IsMe)
var match = Twitter.StatusUrlRegex.Match(url);
if (match.Success)
{
- long statusId;
- if (long.TryParse(match.Groups["StatusId"].Value, out statusId))
+ if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
yield return statusId;
}
}
var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
- if (minimumId != null && minimumId.Value < tab.OldestId)
+ if (minimumId != null)
tab.OldestId = minimumId.Value;
}
.Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
foreach (var _match in ma)
{
- Int64 _statusId;
- if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
+ if (Int64.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
{
if (relPosts.ContainsKey(_statusId))
continue;
var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
- if (minimumId != null && minimumId.Value < tab.OldestId)
+ if (minimumId != null)
tab.OldestId = minimumId.Value;
}
var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
- if (minimumId != null && minimumId.Value < tab.OldestId)
+ if (minimumId != null)
tab.OldestId = minimumId.Value;
}
/// <summary>
/// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
/// </summary>
- public static Tuple<string, Uri> ParseSource(string sourceHtml)
+ internal static (string SourceText, Uri SourceUri) ParseSource(string sourceHtml)
{
if (string.IsNullOrEmpty(sourceHtml))
- return Tuple.Create<string, Uri>("", null);
+ return ("", null);
string sourceText;
Uri sourceUri;
sourceUri = null;
}
- return Tuple.Create(sourceText, sourceUri);
+ return (sourceText, sourceUri);
}
public async Task<TwitterApiStatus> GetInfoApi()
{
var matchDm = Twitter.DMSendTextRegex.Match(postText);
if (matchDm.Success)
- return this.GetTextLengthRemainInternal(matchDm.Groups["body"].Value, isDm: true);
+ return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
- return this.GetTextLengthRemainInternal(postText, isDm: false);
+ return this.GetTextLengthRemainWeighted(postText);
}
- private int GetTextLengthRemainInternal(string postText, bool isDm)
+ private int GetTextLengthRemainDM(string postText)
{
var textLength = 0;
textLength += shortUrlLength - url.Length;
}
- if (isDm)
- return this.Configuration.DmTextCharacterLimit - textLength;
- else
- return 140 - textLength;
+ return this.Configuration.DmTextCharacterLimit - textLength;
+ }
+
+ private int GetTextLengthRemainWeighted(string postText)
+ {
+ var config = this.TextConfiguration;
+ var totalWeight = 0;
+
+ var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
+
+ var pos = 0;
+ while (pos < postText.Length)
+ {
+ var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == pos);
+ if (urlEntity != null)
+ {
+ totalWeight += config.TransformedURLLength * config.Scale;
+
+ var urlLength = urlEntity.Indices[1] - urlEntity.Indices[0];
+ pos += urlLength;
+
+ continue;
+ }
+
+ var codepoint = char.ConvertToUtf32(postText, pos);
+ var weight = config.DefaultWeight;
+
+ foreach (var weightRange in config.Ranges)
+ {
+ if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
+ {
+ weight = weightRange.Weight;
+ break;
+ }
+ }
+
+ totalWeight += weight;
+
+ var isSurrogatePair = codepoint > 0xffff;
+ if (isSurrogatePair)
+ pos += 2; // サロゲートペアの場合は2文字分進める
+ else
+ pos++;
+ }
+
+ var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
+
+ return remainWeight / config.Scale;
}
MyCommon.TraceOut(ex);
return;
}
+ catch (XmlException)
+ {
+ MyCommon.TraceOut("XmlException (StatusArrived): " + line);
+ }
catch(NullReferenceException)
{
MyCommon.TraceOut("NullRef StatusArrived: " + line);
evt.Username = eventData.Source.ScreenName;
evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant());
- MyCommon.EVENTTYPE eventType;
- eventTable.TryGetValue(eventData.Event, out eventType);
+ eventTable.TryGetValue(eventData.Event, out var eventType);
evt.Eventtype = eventType;
TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
var tabinfo = TabInformations.GetInstance();
- PostClass post;
var statusId = tweet.Id;
- if (!tabinfo.Posts.TryGetValue(statusId, out post))
+ if (!tabinfo.Posts.TryGetValue(statusId, out var post))
break;
if (eventData.Event == "favorite")