Collection of mostly command line tools / PHP scripts. Somewhat out of date.

⌈⌋ branch:  scripts + snippets


Check-in [f2846c5a3a]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Uses curl_multi{} wrapper to fetch total social media backlinks to given URL
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:f2846c5a3a6c00d97a48ed0e6216dab37d13c555
User & Date: mario 2014-03-09 03:30:05
Context
2014-03-21
01:15
moved to <a href="/php-scripts">separate repo</a> check-in: 81acd3793c user: mario tags: trunk
2014-03-09
03:30
Uses curl_multi{} wrapper to fetch total social media backlinks to given URL check-in: f2846c5a3a user: mario tags: trunk
2012-01-09
04:19
wrapper to use PHP functions from commandline, needs function name symlinks onto it check-in: 428b5c30a3 user: mario tags: trunk
Changes

Added social.count.php.













































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
 * title: shared_count
 * description: Retrieve social media links/shares/likes/upvotes/views
 * x-feature: google+, facebook, linkedin, reddit, twitter, stumbleupon, delicious
 * version: 0.1.1
 *
 * Query multiple online portals for common user upvotes. Returns a list for making
 * a summary total.
 *
 */


#-- load curl/curl_multi() wrapper
if (!function_exists("curl")) {
    include("./curl.php");  //fossil.include-once.org/hybrid7/doc/trunk/php7/curl.php
}



/**
 * Retrieve number of links from social media portals to given $url.
 *
 * Runs HTTP requests in parallel, and imposes a maximum timeout.
 *
 *
 */
function social_links_count($url) {
    $url = rawurlencode($url);

    // define APIs
    $services = array(
        "twitter" => "http://urls.api.twitter.com/1/urls/count.json?url=$url",
        "linkedin" => "http://www.linkedin.com/countserv/count/share?url=$url&format=json",
        "facebook" => "http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=$url",
        "google" => curl("https://clients6.google.com/rpc")
            ->HttpHeader(array("Content-type: application/json"))
            ->PostFields(json_encode(array(array(   // guess who's unwieldy
                "method"=>"pos.plusones.get",  "id"=>"p",  "jsonrpc"=>"2.0",  "key"=>"p",  "apiVersion"=>"v1",
                "params"=>array("nolog"=>TRUE,  "id"=>rawurldecode($url),  "source"=>"widget",  "userId"=>"@viewer",  "groupId"=>"@self"),
            )))),
        "reddit" => "http://www.reddit.com/api/info.json?url=$url",
        "sumbleupon" => "http://www.stumbleupon.com/services/1.01/badge.getinfo?url=$url",
        "delicious" => "http://feeds.delicious.com/v2/json/urlinfo/data?url=$url",
        "pinterest" => curl("http://api.pinterest.com/v1/urls/count.json?url=$url"),
    );

    // run and extract scores    
    return(
        array_map(
           "json_rx_count",
           curl_multi($services)->exec(0.75)
        )
    );
}



/**
 * Since we have unambiguous and concise JSON blobs, it's sufficient and less
 * overhead to just extract the numbers we're looking for.
 *
 * This also avoids looping over the reddit result lists manually, and integer
 * casting afterwards.
 *
 */
function json_rx_count($string, $rx="count|score|views|total_count|total_posts") {

    if (preg_match_all("/(?<!\\\\)\"(?:$rx)\"\s*:\s*\"?(\d+)/", $string, $uu)) {

        return array_sum($uu[1]);
    }
}




/**
 * Simple caching wrapper
 *
 */
function cache_fn($fn, $args, $ttl=7200) {

    // check cache
    $p = serialize($args);
    $cache = json_decode(file_get_contents("./$fn.cache.json"), TRUE);
    if (isset($cache[$p]) and (time() < $cache[$p]["time"] + $ttl)) {
        return $cache[$p]["data"];
    }

    // get data
    $data = call_user_func_array($fn, (array)$args);
    
    // update cache
    $cache[$p] = array("time"=>time(), "data"=>$data);
    file_put_contents("./$fn.cache.json", json_encode($cache, JSON_PRETTY_PRINT), LOCK_EX);

    return $data;
}


?>