-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathig-pesos-to-withknown.php
144 lines (112 loc) · 3.97 KB
/
ig-pesos-to-withknown.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
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
139
140
141
142
143
144
<?php
/*
* ig-pesos-to-known.php
* version 0.1
* 2020-07-29
*
* Offers a way to automatically post new photographs from YOUR InstaGram account
* to YOUR installation of WithKnown.
*
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit(600);
// Get variables from config
$config = include 'config.php';
$newfile = $config->feedUrl;
$endpoint = $config->endpoint;
$username = $config->username;
$action = $config->action;
$known_api_key = $config->known_api_key;
// Stored value of previous item
$pubdate = strtotime(file_get_contents('pubdate.txt'));
$knowntoken = base64_encode(hash_hmac('sha256', $action, $known_api_key, true));
// Get the contents of the RSS feed
$newfilecontents = file_get_contents($newfile);
// Convert feed to array and discard elements not an item
$newfilecontents = makeArray($newfilecontents);
// Extract only items newer than $pubdate
$newitems = [];
foreach ($newfilecontents as $newfilecontent) {
$thepubdate = strtotime($newfilecontent['pubDate']);
if ($thepubdate > $pubdate) {
$newitems[] = $newfilecontent;
}
}
// No point doing anything if there is nothing to do
if (empty($newitems)) {
exit('No new items');
}
// Just in case
copy('pubdate.txt', 'oldpubdate.txt');
// Set the pubDate marker for next time
file_put_contents('pubdate.txt', $newitems[0]['pubDate']);
$newitems = array_reverse($newitems);
// Extract the data and construct the cURL
foreach ($newitems as $newitem) {
$title = $newitem['title'];
$pubdate = $newitem['pubDate'];
$description = $newitem['description'];
// Now get the data out of $description, pending use of simpleXML
$caption = substr($description, 0, (strpos($description, '<img'))); // everything before the img tag
$caption = preg_replace('/<br\/>/', '', preg_replace('/<br\/><br\/>/', '</p><p>', $caption)); // replace <br> with <p> where appropriate
$endurl = strpos($description, '"', strpos($description,'https'));
$photourl = substr($description, (strpos($description, 'src="')+5),($endurl-(strpos($description, 'src="')+5)));
//now use these to construct the cURL
doCurl($title, $caption, $photourl, $endpoint, $username, $knowntoken);
}
// Set the pubDate marker for next time
file_put_contents('pubdate.txt', $newitems[0]['pubDate']);
// Convert received XML string to array, pending use of simpleXML
function makeArray($filename) {
$xml=simplexml_load_string($filename,'SimpleXMLElement',LIBXML_NOCDATA) or die("Error: Cannot create object");
$json = json_encode($xml); // not an array
$results = json_decode($json, TRUE); //an array
// Extract just the items
$results = $results['channel']['item'];
return($results);
}
// Initially from Paw, then modified
function doCurl($title, $caption, $photourl, $endpoint, $username, $knowntoken) {
// get cURL resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $endpoint);
// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
// 'X-KNOWN-USERNAME: Jeremy',
// 'X-KNOWN-SIGNATURE: m1Ooz9mTkqvuGhVuxpTkif1x9EtOjaTfwe3cCbF22S4=',
'X-KNOWN-USERNAME: ' . $username,
'X-KNOWN-SIGNATURE: ' . $knowntoken,
'Cookie: known=03a03330ec74f91efa11b3ea623e9a05',
]);
// form body
$body = [
'description' => $caption,
'title' => $title,
'photo' => $photourl,
'tags' => 'pesos', // Or whatever you want.
];
$body = http_build_query($body);
// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// send the request and save response to $response
$response = curl_exec($ch);
// stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;
// close curl resource to free up system resources
curl_close($ch);
}
?>