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

⌈⌋ ⎇ branch:  scripts + snippets


Check-in [c49ea11126]

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

Overview
Comment:Retrieve count of backlinks to URL from various web services (Facebook, Google+, Twitter, Reddit, StumbleUpon, LinkedIn, Delicious, Pinterest) <em>Shallow merge of previous <code>php-scripts.fossil</code> repo.</em>
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c49ea1112614cf0bc1c939cbfb190905ca9365a3
User & Date: mario 2014-12-13 17:36:28
Context
2015-03-08
18:29
Transform PHP function parameter /*type*/ hints into assert() statements. check-in: 5eaf0c63f6 user: mario tags: trunk
2014-12-13
17:36
Retrieve count of backlinks to URL from various web services (Facebook, Google+, Twitter, Reddit, StumbleUpon, LinkedIn, Delicious, Pinterest) <em>Shallow merge of previous <code>php-scripts.fossil</code> repo.</em> check-in: c49ea11126 user: mario tags: trunk
2014-12-12
20:10
Use array_shift for multiple positional - - - stdin/json placeholders. Don't populate $args from optional args. check-in: e4e8e5030f user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added social-media-links/social.ajax.php.

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * description: Generates an image from the cumulative social media backlinks count
 *
 */


include("./social.count.php");
$num = @social_links_count(filter_var($_GET["url"], FILTER_SANITIZE_URL));

header("Content-Type: json/object; charset=UTF-8");
print json_encode($num);

Added social-media-links/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;
}


?>

Added social-media-links/social.html.php.















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/**
 * description: Generates a static plain-text link list for URL sharing
 *
 */


// escape
$url = htmlspecialchars(urlencode($_REQUEST["url"]), ENT_QUOTES|ENT_HTML5, "UTF-8");

// output
print "
<span class=social-share-links>
<a href=https://plus.google.com/share?url=$url title=google+>g+</a>
<a href=https://www.facebook.com/sharer/sharer.php?u=$url title=facebook>fb</a>
<a href=https://twitter.com/intent/tweet?url=$url title=twitter>tw</a>
<a href=http://reddit.com/submit?url=$url title=reddit>rd</a>
<a href=https://www.linkedin.com/shareArticle?mini=true&amp;url=$url title=linkedin>in</a>
<a href=https://www.stumbleupon.com/submit?url=$url title=stumbleupon>su</a>
<a href=https://del.icio.us/post?url=$url title=delicious>dl</a>
</span>
";

Added social-media-links/social.img.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
<?php
/**
 * description: Generates an image from the cumulative social media backlinks count
 *
 */


#-- retrieve
include("./social.count.php");
$url = @$_GET["url"];
if (filter_var($url, FILTER_VALIDATE_URL)) {
   $num = cache_fn("social_links_count", $url);
   $total = array_sum($num);
}
else {
   $total = "no url given";
}

#-- create PNG
$png = imagecreate(6 + 9 * strlen($total), 20);
$bg = imagecolorallocate($png, 255, 255, 255);
$fg = imagecolorallocate($png, 55, 55, 55);
imagestring($png, 5, 3, 2, "$total", $fg);

#-- output
header("Content-Type: image/png; version=1.2");
imagepng($png);
imagedestroy($png);


?>