Peter Williams
Peter Williams

Reputation: 19

LinqToTwitter: timing issue with video media

I'm using Linqtotwitter version 5. The code below works sometimes, sometimes not. If I put a breakpoint where I've indicated and wait a while, it pretty much always works.

When it doesn't work, I get the exception message 'Not valid video'.

I posted a sample video here, but they meet the requirements here posted by Twitter. Video size 75MB, duration 34 seconds. Pretty much any video acts the same way.

I've based this code on the sample here

I've taken the code out and placed it into a console app where it exhibits the same behaviour.

class Program
{
        static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                UploadMedia theMedia = new UploadMedia();
                int theResult = await theMedia.doitAsync();
                Console.Write("Return value =" + theResult.ToString());

            } ).GetAwaiter().GetResult();
        }


}

public class UploadMedia { 

    public async Task<int> doitAsync()
    {
        TwitterContext twitterContext = getContext();

        List<string> _theMedia = new List<string>();
        _theMedia.Add(@"C:\Temp\RecentMedia\MOV_1963.mp4");

        try
        {
            string lsMediaType = "video/mp4";
            string lsMediaCat = "tweet_video";
            var imageUploadTasks = new List<Task<Media>>();
            Task<Media> lsoImageMedia;
            foreach (var lsItem in _theMedia)
            {
                Console.Write("Sending " + lsItem);
                lsoImageMedia = twitterContext.UploadMediaAsync(System.IO.File.ReadAllBytes(lsItem), lsMediaType, lsMediaCat);
                imageUploadTasks.Add(lsoImageMedia);
            };

            await Task.WhenAll(imageUploadTasks);

            List<ulong> mediaIds =
            (from tsk in imageUploadTasks
             select tsk.Result.MediaID)
            .ToList();

            // PUT A BREAKPOINT HERE AND WAIT A WHILE, AND THE VIDEO WILL UPLOAD
            Status tweet = await twitterContext.TweetAsync("This is an example video", mediaIds);

            if (tweet != null)
            {
                Console.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Status returned: " + "(" + tweet.StatusID + ")" +
                    tweet.User.Name + ", " + tweet.Text);
            }

            return 0;

        }
        catch (Exception ex)
        {
            Console.Write("Exception whilst sending/processing media: " + ex.Message + " : " + ex.ToString());
            return -1;
        }
    }

    static private TwitterContext getContext()
    {
        SingleUserAuthorizer authorizer = new SingleUserAuthorizer();

        SingleUserInMemoryCredentialStore cred = new SingleUserInMemoryCredentialStore();
        cred.ConsumerKey = TwitterConstants.sConsumerKey;
        cred.ConsumerSecret = TwitterConstants.sConsumerKeySecret;
        cred.AccessToken = TwitterConstants.sAccessToken;
        cred.AccessTokenSecret = TwitterConstants.sAccessTokenSecret;

        authorizer.CredentialStore = cred;

        var twitterContext = new TwitterContext(authorizer);
        return twitterContext;
    }

}

Upvotes: 1

Views: 84

Answers (0)

Related Questions