⌈⌋ ⎇ branch:  freshcode


Check-in [d9a13a9d82]

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

Overview
Comment:Switch to raw `twurl` requests for Twitter updates, as that allows to embed screenshots easily.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d9a13a9d8279d4dcf56764d5d7ac1745ffd3dd5f
User & Date: mario 2014-11-22 21:28:40
Context
2014-11-22
21:29
Automated cronjob re-registration from `x-cron:` plugin meta fields. check-in: 2b7b9307c4 user: mario tags: trunk
21:28
Switch to raw `twurl` requests for Twitter updates, as that allows to embed screenshots easily. check-in: d9a13a9d82 user: mario tags: trunk
2014-11-19
22:04
Beta stage, declare version 0.7.5. check-in: 9339ac3384 user: mario tags: trunk, 0.7.5
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to cron.daily/twitter.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * api: cli
 * title: Twitter bridge
 * description: Posts recent releases on twitter
 * version: 0.1
 * category: rpc
 * type: cron
 * x-cron: 50 * * * *
 *
 * Summarize new releases for Twitter feed.
 * Currently using `twidge` (pre-configured in $HOME),
 * which doesn't support //t.co/ inline images yet.





|







1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * api: cli
 * title: Twitter bridge
 * description: Posts recent releases on twitter
 * version: 0.3
 * category: rpc
 * type: cron
 * x-cron: 50 * * * *
 *
 * Summarize new releases for Twitter feed.
 * Currently using `twidge` (pre-configured in $HOME),
 * which doesn't support //t.co/ inline images yet.
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
 * Releases within the last hour
 *
 */
$rel = db("
    SELECT *
      FROM release_versions
     WHERE t_published >= ?
    ", time()-7300
)->fetchAll();



// query recent/previous status messages

$prev = twit("lsarchive");

// condense individual tweets
foreach ($rel as $row) {

    // skip existing
    if (is_int(strpos($prev, $row["title"]))) {
    print "skip($row[name]) ";
        continue;
    }
    
    // homepage
    if (empty($row["homepage"]) or strlen($row["homepage"] > 80)) {
        $row["homepage"] = "http://freshcode.club/projects/$row[name]";
    }

    // prepare post
    $msg = "$row[title] $row[version] released. $row[homepage]";
    $msg = preg_replace("/\s+/", " ", $msg);





    
    // add tags
    $tags = p_csv($row["tags"]);
    shuffle($tags);
    foreach ($tags as $tag) {
        $tag = preg_replace("/^(\w)\+\+/", "\\1pp", $tag);
        $tag = preg_replace("/-/", "_", $tag);
        $msg .= " #$tag";
    }
    
    // cut to limit
    while (strlen($msg) > 140) {
        $msg = preg_replace("/\s\S+$/s", "", $msg);
    }
    
    // send
    print_r("$msg\n");



    twit("update", $msg);
}































/**
 * Invoke cmdline twitter client
 *
 */
function twit() {
    $cmd = "twidge";
    foreach (func_get_args() as $param) {
        $cmd .= " " . escapeshellarg($param);

    }
    return ` $cmd `;
}













?>







|





>
|





|
|











>
>
>
>
>










|
|




|
>
>
>
|
|

>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|


|
<
|
<
>

|

>
>
>
>
>
>
>
>
>
>
>
|


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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
 * Releases within the last hour
 *
 */
$rel = db("
    SELECT *
      FROM release_versions
     WHERE t_published >= ?
    ", time()-7300           # overlap: compare last two hours -- @todo: must be at least 7 minutes old, or screenshot may not exist
)->fetchAll();



// query recent/previous status messages
$prev = twurl("statuses/user_timeline.json?count=50");
$prev = twurl_text_urls($prev);

// condense individual tweets
foreach ($rel as $row) {

    // skip existing
    if (tweet_exists($prev, $row)) {
        print "skip($row[name]) ";
        continue;
    }
    
    // homepage
    if (empty($row["homepage"]) or strlen($row["homepage"] > 80)) {
        $row["homepage"] = "http://freshcode.club/projects/$row[name]";
    }

    // prepare post
    $msg = "$row[title] $row[version] released. $row[homepage]";
    $msg = preg_replace("/\s+/", " ", $msg);
    
    // check for project Twitter= URL, add @mention
    if (preg_match("~https?://twitter.com/@?(\w{2,15})~", $row["urls"], $uu)) {
        $msg .= " @$uu[1]";
    }
    
    // add tags
    $tags = p_csv($row["tags"]);
    shuffle($tags);
    foreach ($tags as $tag) {
        $tag = preg_replace("/^(\w)\+\+/", "\\1pp", $tag);
        $tag = preg_replace("/-/", "_", $tag);
        $msg .= " #$tag";
    }
    
    // check for tweet size limit (140 minus URL-length [22] for appended image)
    while (tweet_length($msg) > 118) {
        $msg = preg_replace("/\s\S+$/s", "", $msg);
    }
    
    // send
    print_r("\ntweet($msg) ");
    twurl_with_img($msg, "img/screenshot/$row[name].jpeg");
}
print "\n";



/**
 * twurl API
 *
 */
function twurl($api) {
   return json_decode(exec("twurl '/1.1/$api'"), TRUE);
}


/**
 * Send MSG+IMG via twurl
 *
 */
function twurl_with_img($msg, $img) {
   $msg = escapeshellarg($msg);
   exec("twurl -X POST '/1.1/statuses/update_with_media.json' --file $img --file-field 'media[]' -d status=$msg");
}


/**
 * Calculate tweet size without URLs.
 * 
 */
function tweet_length($msg) {
    $msg = preg_replace("~http://\S+(?<![,.])~", "http://t.co/abcDEFGHIJ", $msg);
    return strlen($msg);
    
}


/**
 * Collect `text:` and `expanded_urls:`
 *
 */
function twurl_text_urls($json, $text="") {

    foreach ($json as $tweet) {

        $text .= "{$tweet['text']} → {$tweet['entities']['urls'][0]['expanded_url']}\n";
    }
    return $text;
}
// Compare titles and homepage links with previous feeds.
function tweet_exists($prev, $row) {
    # escape
    $title = preg_quote($row["title"], "~");
    $homepage = preg_quote($row["homepage"], "~")
    or $homepage = "http://freshcode.club/projects/$row[name]";
    # check mentions
    return
        preg_match("~^$title~mi", $prev)
      or
        preg_match("~$homepage~", $prev);
}

?>