Friday, August 24, 2018

Facebook API: like a photo

In the Facebook API (REST), how do you like a photo? There's a stream.addLike function, but you need the post_id, and I don't see a way to get the post_id of a photo (not the same as the pid or the object_id).

Solved

You can now like a photo using Graph API, via a POST to the likes connection on a photo. This did not work previously work but it does now (2011/09/16). Not sure exactly when it got fixed but this one was an oversight for a very long after Graph API was introduced.


First of all you have an Id of Uploaded picture when you are adding this. e.g

        var client = new FacebookClient(Access_Token);

        JsonObject jsonResponse = client.Get("me/feed") as JsonObject;
        string feed_ID = string.Empty;
        foreach (var account in (JsonArray)jsonResponse["data"])
        {
            feed_ID = (string)(((JsonObject)account)["id"]);
            goto Next;
        }
    Next: { };

let us say you have got the id of your latest picture uploeded is 12345123_5488224848 now you want to update like this picture. write the following code.

      var client2 = new FacebookClient(Access_Token);
      clientS.Post("12345123_5488224848/likes");

all done. check the status of photo after this.


I think that the actual photo is classes as the same type of entity os a wallpost, so asloon as you can aquire the stream_id for the photo, it the comments that are attached to the photo you can set stream.addLink found http://developers.facebook.com/docs/reference/rest/stream.addLike Here

By getting the post_id via the photos.get method you should be able to set a comment adn like the object via the stream.addLike.

Hope this helps you.


   var client = new FacebookClient(Access_Token);

    JsonObject jsonResponse = client.Get("me/feed") as JsonObject;
    string feed_ID = string.Empty;
    foreach (var account in (JsonArray)jsonResponse["data"])
    {
        feed_ID = (string)(((JsonObject)account)["id"]);
        goto Next;
    }
Next: { };

Monday, August 20, 2018

how can I open a video from dailymotion with mpmovieplayercontroller

I want to open a video from dailymotion with mpmovieplayercontroller. I have tried Dailymotion SDK but it seems that it just embeds a video to a uiwebview. I wonder if there is a dailymotion parser to get the video link just like hcyoutubeparser or ytvvimeoextractor.

Solved

-(NSDictionary*)getInfoForDailyMotionVideo:(NSString*)videoId
{
NSString *urlString = [NSString stringWithFormat:@"http://www.dailymotion.com/embed/video/%@", videoId];
NSURL *URL = [NSURL URLWithString:urlString];
NSData * data = [[NSData alloc] initWithContentsOfURL:URL];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *str;

NSRange startRange = [html rangeOfString:@"var info = {"];
str = [html substringFromIndex:startRange.location];
NSRange endRange = [str rangeOfString:@"{"];
str = [str substringFromIndex:endRange.location];
endRange = [str rangeOfString:@"},"];
NSString *jsonString = [str substringToIndex:endRange.location+1];


NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
return result;
}

Passing a video ID of xct647, will return a dictionary with the following keys that work with a MPMoviePlayerViewController:

"stream_h264_hq_url" = "http://www.dailymotion.com/cdn/H264-848x480/video/xct647.mp4?auth=1371275189-0d67b3d0c242a8b439adf4300014d749";
"stream_h264_ld_url" = "http://www.dailymotion.com/cdn/H264-320x240/video/xct647.mp4?auth=1371275189-f581218251d42809bf13ad96ea6aacb8";
"stream_h264_url" = "http://www.dailymotion.com/cdn/H264-512x384/video/xct647.mp4?auth=1371275189-f905cebbbe96c563beca38ab59132a95";
"stream_hls_url" = "http://www.dailymotion.com/cdn/manifest/video/xct647.m3u8?auth=1371275189-dbdb650a58afe8661ec6aa9628de064f";

Use MPMovieSourceTypeFile for the h264 url's and MPMovieSourceTypeStreaming for the 'hls' url.


the last answer is a great starting point, but doesn't work anymore, needed to update due to Javascript/JSON Syntax changes:

-(NSDictionary*)getInfoForDailyMotionVideo:(NSString*)videoId{
    NSString *urlString = [NSString stringWithFormat:@"http://www.dailymotion.com/embed/video/%@", videoId];
    NSURL *URL = [NSURL URLWithString:urlString];
    NSData * data = [[NSData alloc] initWithContentsOfURL:URL];
    NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString *str;

    NSRange startRange = [html rangeOfString:@"var config = {"];
    str = [html substringFromIndex:startRange.location];
    NSRange endRange = [str rangeOfString:@"{"];
    str = [str substringFromIndex:endRange.location];
    endRange = [str rangeOfString:@"}};"];
    NSString *jsonString = [str substringToIndex:endRange.location+1];
    jsonString = [jsonString stringByAppendingString:@"}"];
    NSError *jsonError;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
    return result;
}

This now works and returns all relevant values for AVPlayer etc...

Have fun!


Sunday, August 19, 2018

Having a list inside recursive function's impact on memory/resources

I am new to Python and I plan on having a list inside a recursive function such as

def myRecursion(a):

    A = [0,1,2]    

    #Rest of code here
    myRecursion(a-1)

My question is will having A inside the recursion create many instances of it and eat up my resources? I should also note that the contents of the list is always the same.

Solved

A simple answer with some sample timings: Yes, creating a list inside of a recursive function will have an impact on performance, as opposed to creating it outside a recursive function and passing it in.

In [1]: def recursion1(n):
   ...:     A = [1,2,3]
   ...:     return n if n == 0 else recursion1(n-1)
   ...:

In [2]: %timeit recursion1(1000)
232 µs ± 7.84 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [3]: def recursion2(n, A):
   ...:     return n if n == 0 else recursion2(n-1, A)
   ...:

In [4]: A = [1,2,3]

In [5]: %timeit recursion2(1000, A)
163 µs ± 681 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

My question is will having A inside the recursion create many instances of it

We can use id() to check the identity of an object:

In [6]: def recursion1(n):
   ...:     A = [1,2,3]
   ...:     print(id(A))
   ...:     return n if n == 0 else recursion1(n-1)
   ...:

In [7]: recursion1(3)
129035280
134141552
129297184
134141472
Out[7]: 0

In [8]: def recursion2(n, A):
   ...:     print(id(A))
   ...:     return n if n == 0 else recursion2(n-1, A)
   ...:

In [9]: recursion2(3, A)
133702400
133702400
133702400
133702400
Out[9]: 0

Friday, August 17, 2018

terpri, princ & co. vs format

Chapter 9.10 of Common Lisp: A Gentle Introduction To Symbolic Computation claims:

The primitive i/o functions TERPRI, PRIN1, PRINC and PRINT were defined in Lisp 1.5 (the ancestor of all modern Lisp systems) and are still found in Common Lisp today. They are included in the Advanced Topics section as a historical note; you can get the same effect with FORMAT.

This implies that you do not neet princ & co. any more and that, in modern code, you only should rely on format instead.

Are there any disadvantages when doing this? Respectively, are there any things one can not achieve with format that works with the other ones?

Solved

These functions correspond exactly to the following FORMAT operators:

  • TERPRI = ~%
  • FRESH-LINT = ~&
  • PRIN1 = ~S
  • PRINC = ~A
  • PRINT = ~%~S

You can also use the more modern write. I'm not a huge fan of format because of its terse sub language, which usually is interpreted. Note that a good implementation might be able to compile format directives to more efficient code. I use FORMAT mostly when it makes complex code shorter, but not to output plain objects or things like single carriage returns...

Common Lisp includes three or more generations of text I/O APIs:

  • the old s-expression printing routines
  • the specialized and generalized stream IO functions
  • the complex formatter, based on earlier Fortran and/or Multics IO formatters
  • the Generic Function to print objects
  • the pretty printer

Additionally there are semi-standard CLOS-based IO implementations like Gray Streams.

Each might have its purpose and none is going away soon...

CL-USER 54 > (let ((label "Social security number")
                   (colon ": ")
                   (social-security-number '|7537 DD 459234957324 DE|))

               (terpri)
               (princ label)
               (princ colon)
               (princ social-security-number)

               (write-char #\newline)
               (write-string label)
               (write-string colon)
               (write social-security-number :escape nil)

               (format t "~%~A~A~A" label colon social-security-number)

               )

Social security number: 7537 DD 459234957324 DE
Social security number: 7537 DD 459234957324 DE
Social security number: 7537 DD 459234957324 DE