public static function getFriends($file = true) { if ($file && ($friends = static::getFriendsFromFile())) { return $friends; } static::login(); $html = $page = ''; while (true) { $page = Curl::get(static::getPPKURL('/friends/center/friends', $page)); $html .= preg_replace('#<body[^>]*>(.*)</body>#', '$1', $page); if (!strstr($page, 'ppk=')) { break; } sleep(rand(1, 3)); } $XPath = HTML::getXPath($html); $Friends = $XPath->query('//div[@class="bk"]/div'); if ($Friends->length === 0) { return array(); } foreach ($Friends as $Node) { $A = $XPath->query('.//a', $Node)->item(0); $id = static::getUserIdFromURL($A->getAttribute('href')); if (empty($friends[$id])) { $friends[$id] = static::getFriendModel(); } $friends[$id]['id'] = $id; $friends[$id]['name'] = $A->nodeValue; } return $friends; }
public function run() { $logger = new Logger('day-wishes'); $birthdays = Facebook::getTodayBirthdays(); if (empty($birthdays)) { $logger->log('FINISH', 'No birthdays today'); return; } foreach ($birthdays as $friend) { if ($friend['form_action'] === null) { $logger->log(sprintf('%s has not form', $friend['name'])); continue; } $friend['message'] = Phrases::one($friend); $friend['inputs'][$friend['message_input']] = $friend['message']; $logger->log('PERSON', sprintf('%s is on birthday! : %s', $friend['name'], $friend['message'])); Curl::post($friend['form_action'], $friend['inputs']); sleep(rand(1, 5)); } }