Report abuse

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
<?php
/**
 * CONFIGURATION
 */
$config = array(
    #Twitter Password
    'username'          => '',
    #Twitter Username
    'password'          => '',
    #Category to 'tweet'
    'target_category'   => 'cafe',
    #Maximum age in minutes of posts to 'tweet'
    'age_threshold'     => '',
    #Full location of cache file
    'cache_file'        => '/full/path/to/cache.file'
);

$xml = @new SimpleXMLElement('http://cdevroe.com/feed/', null, true);

if(true === ($xml instanceof SimpleXMLElement)){
    foreach($xml->xpath(sprintf("//category[text()='%s']/..", $config['target_category'])) as $item){

        if(true === in_array(sha1($item->title), file($config['cache_file'])){
            continue;
        }

        if((time() - (integer)strtotime($item->pubDate)) >= ($config['age_threshold'] * 60)){
            continue;
        }

        $curl = curl_init(
            sprintf(
                'http://twitter.com/statuses/update.xml?status=%s',
                urlencode(
                    sprintf(
                        '%s "%s" : %s',
                        $config['target_category'],
                        $item->title,
                        str_replace('?p=', 'p/', $item->guid)
                    )
                )
            )
        );

        curl_setopt_array(
            $curl,
            array(
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_HTTPAUTH        => CURLAUTH_BASIC,
                CURLOPT_POST            => true,
                CURLOPT_USERPWD         => sprintf('%s:%s', $config['username'], $config['password'])
            )
        );

        curl_exec($curl);
        if(200 === curl_getinfo($curl, CURLINFO_HTTP_CODE)){
            file_put_contents($config['cache_file'], sprintf("%s\r\n", sha1($item->title)), FILE_APPEND);
        }
    }
}
?>