forked from eschnou/storytlr
/
GithubModel.php
136 lines (110 loc) · 3.57 KB
/
GithubModel.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
<?php
/*
* Copyright 2008-2009 Laurent Eschenauer and Alard Weisscher
* Copyright 2010 John Hobbs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
class GithubModel extends SourceModel {
protected $_name = 'github_data';
protected $_prefix = 'github';
protected $_search = 'content';
protected $_update_tweet = "Did %d things at github.com on my lifestream %s";
public function getServiceName() {
return "Github";
}
public function isStoryElement() {
return true;
}
public function getServiceURL() {
return 'https://github.com/' . $this->getProperty('username');
}
public function getServiceDescription() {
return "Github is social coding.";
}
public function getAccountName() {
if ($name = $this->getProperty('username')) {
return $name;
}
else {
return false;
}
}
public function getTitle() {
return $this->getServiceName();
}
public function importData() {
$items = $this->updateData();
$this->setImported( true );
return $items;
}
public function updateData() {
$url = 'https//github.com/' . $this->getProperty('username') . '.atom';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT,'Storytlr/1.0');
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close ($curl);
if ($http_code != 200) {
throw new Stuffpress_Exception( "Github returned http status $http_code for url: $url", $http_code );
}
if (!($items = simplexml_load_string($response))) {
throw new Stuffpress_Exception( "Github did not return any result", 0 );
}
if ( count( $items->entry ) == 0 ) { return; }
$items = $this->processItems($items->entry);
$this->markUpdated();
return $items;
}
private function processItems($items) {
$result = array();
foreach ($items as $item) {
$data = array();
$data['title'] = $item->title;
$data['repository'] = substr( $item->title, strrpos( $item->title, ' ' ) + 1 );
$data['published'] = strtotime( $item->published );
$data['content'] = $item->content;
$data['link'] = $item->link['href'];
$data['github_id'] = $item->id;
$id = $this->addItem( $data, $data['published'], SourceItem::LINK_TYPE, array( $data['repository'] ), false, false, $data['title'] );
if ($id) $result[] = $id;
}
return $result;
}
public function getConfigForm($populate=false) {
$form = new Stuffpress_Form();
// Add the username element
$element = $form->createElement('text', 'username', array('label' => 'Username', 'decorators' => $form->elementDecorators));
$element->setRequired(true);
$form->addElement($element);
// Populate
if($populate) {
$values = $this->getProperties();
$form->populate($values);
}
return $form;
}
public function processConfigForm($form) {
$values = $form->getValues();
$update = false;
if($values['username'] != $this->getProperty('username')) {
$this->_properties->setProperty('username', $values['username']);
$update = true;
}
return $update;
}
}