Friday, April 25, 2014

Getting Potential Reach for ReTweeted Tweets via Linq2Twitter

Linq2Twitter is a super awesome library to use the Twitter Api Thanks to the creator
I wanted to get all tweets and if retweeted , total count of followers of retweeters to see how many people each tweet reached.
And this is how you do it
public async static Task<List<Tuple<Statusint>>> GetTweetsWithReach(int schoolID, DateTime startDate, DateTime endDate)
{
    string twUrl;
    var auth = TWAuthenticate(schoolID, out twUrl);
    try
    {
        if (auth == nullreturn null;
        await auth.AuthorizeAsync();
        using (var twitterCtx = new TwitterContext(auth))
        {
            List<Status> tweets = await (from tweet in twitterCtx.Status
                                         where (tweet.Type == StatusType.User)
                                               && tweet.ScreenName == twUrl.GetTwitterUsername()
                                               //todo change 5
                                                && tweet.Count == 200
                                                // && tweet.RetweetCount > 0
                                                && tweet.CreatedAt < endDate && tweet.CreatedAt > startDate
                                                && tweet.IncludeRetweets == true
                                                && tweet.TrimUser == false
                                         select tweet).ToListAsync();
            var listTweets = new List<Tuple<Statusint>>();
 
            foreach (var tw in tweets)
            {
                int reach = 0;
                if ( tw.RetweetCount > 0)
                {
                    var reTweets = await (from tweet in twitterCtx.Status
                                          where tweet.Type == StatusType.Retweets && tweet.ID == tw.StatusID
                                          select tweet).ToListAsync();
                    if (reTweets != null)
                        reach = reTweets.Where(rt => rt != null && rt.User != null).Sum(rt => rt.User.FollowersCount);
                }
 
                listTweets.Add(new Tuple<Statusint>(tw, reach));
            }
            return listTweets;
        }
    }
    catch (Exception ex)
    {
        ex.LogException(twUrl.GetTwitterUsername() + "twitter error");
        return null;
    }
}

Wednesday, April 09, 2014

Reading json easily with C#

You need Newtonsoft.Json dll and use of "dynamic" type in C# to do below.
This code gets follower count from Twitter api without the need to create extra classes
to load json data  : 
 
var wc = new WebClient(); 
string json= wc.DownloadString(url);

         dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json); 
        int? twFollowerCount =obj.followers_count;

Wednesday, January 22, 2014

Chronicles on Migration From Entity Framework to Telerik OpenAccess - 4: Exception with OpenAccessLinqDataSource causes the page to hang forever

You need to add this line to wherever the exception occurs:
         e.ExceptionHandled = true;

    protected void edsSch_Deleted(object sender, OpenAccessLinqDataSourceStatusEventArgs e)
    {
        if (e.Exception != null)
        {
            ((RadNotification)Master.FindControl("rnMain")).Show("A problem occurred while deleting this  ");
            e.ExceptionHandled = true;         
        }

Thursday, January 16, 2014

Chronicles on Migration From Entity Framework to Telerik OpenAccess - 3

With entity data source you can bind it to a grid and do all CRUD operations automatically even if the entity data source selects related entities via "Include" property.
With Telerik OpenAccessLinqDataSource if you "include" related entities you can not use auto CRUD .You need to write custom code for CRUD

Telerik OpenAccessLinqDataSource gives "Identifier expected (at index 3)" exception Chronicles on Migration From Entity Framework to Telerik OpenAccess - 2

Use default value of where parameter to prevent this
  <telerik:OpenAccessLinqDataSource runat="server" ID="edsDegree"
        ResourceSetName="Degrees" EntityTypeName=""
        Select=" new (ID,DegreeTypeID,MajorType,MajorType2,MinorType,SectionID,College,PlannedEntryDate,EntryDate,PlannedGraduationDate,GraduationDate,GPA)"
        ContextTypeName="DataModel.data"
        Where="it.UserID==@UserID">
        <WhereParameters>
            <asp:Parameter Name="UserID" Type="Int32" DefaultValue="0" />
        </WhereParameters>
    </telerik:OpenAccessLinqDataSource>

Or

Remove the "[" "]" from the  markup:
OrderBy="it.[Name]" --> OrdeBy="it.Name"