Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace MapMyRun;
- //require 'RuntasticActivityList.php';
- class MapMyRun
- {
- /**
- * HTTP Responses
- */
- const HTTP_OK = 200;
- /**
- * MapMyRun API Urls
- */
- const MAP_MY_RUN_URL_ACTIVITY = "https://oauth2-api.mapmyapi.com/v7.1/allday_activity/69649/";
- const MAP_MY_RUN_URL_USER = "https://oauth2-api.mapmyapi.com/v7.1/user/69649/?field_set=is_mvp";
- /**
- * Runtastic Credentials
- */
- private $loginUsername ="maiagabriel@live.com";
- private $loginPassword = "phpmapmyrun95";
- //private $start_datetime_utc;
- //private $end_datetime_utc;
- //private $created_datetime;
- //private $updated_datetime;
- private $user;
- private $uexternal_id;
- private $has_time_series;
- private $time_series;
- private $aggregates;
- // private $aggregates_total_steps;
- //private $aggregates.total_active_time;
- //private $aggregates.total_energy_expended;
- //private $aggregates.total_distance;
- private $timeout = 30;
- public function __construct()
- {
- libxml_use_internal_errors(true);
- $this->doc = new \DOMDocument();
- }
- public function setUser($user)
- {
- $this->user = $user;
- return $this;
- }
- public function setPassword($loginPassword)
- {
- $this->loginPassword = $loginPassword;
- return $this;
- }
- public function setTimeout($timeout)
- {
- $this->timeout = $timeout;
- return $this;
- }
- public function setuexternal_id($uexternal_id)
- {
- $this->uexternal_id = $uexternal_id;
- return $this;
- }
- public function getUser()
- {
- return $this->user;
- }
- public function getuexternal_id()
- {
- return $this->uexternal_id;
- }
- public function getResponseStatusCode()
- {
- if (isset($this->lastRequestInfo['http_code'])) {
- return $this->lastRequestInfo['http_code'];
- }
- return null;
- }
- private function setDataFromResponse($response)
- {
- $this->doc->loadHTML($response);
- $inputTags = $this->doc->getElementsByTagName('input');
- foreach ($inputTags as $inputTag) {
- if ($inputTag->getAttribute("name") == "authenticity_token") {
- $this->token = $inputTag->getAttribute("value");
- break;
- }
- }
- $aTags = $this->doc->getElementsByTagName('a');
- foreach ($aTags as $aTag) {
- if (preg_match("/\/en\/users\/(.*)\/dashboard/", $aTag->getAttribute("href"), $matches)) {
- $this->username = $matches[1];
- break;
- }
- }
- $scriptTags = $this->doc->getElementsByTagName('script');
- foreach ($scriptTags as $scriptTag) {
- if (strstr($scriptTag->nodeValue, 'index_data')) {
- $this->rawData = $scriptTag->nodeValue;
- break;
- }
- }
- preg_match("/uid: (.*)\,/", $this->rawData, $matches);
- if (isset($matches[1])) {
- $this->uid = $matches[1];
- }
- }
- /**
- * Login User to Runtastic
- *
- * @return bool
- */
- public function login()
- {
- $this->loggedIn = false;
- $postData = [
- "user[email]" => $this->loginUsername,
- "user[password]" => $this->loginPassword,
- ];
- $responseOutputJson = $this->post(self::RUNTASTIC_LOGIN_URL, $postData);
- if ($this->getResponseStatusCode() == self::HTTP_OK) {
- $this->setDataFromResponse($responseOutputJson->update);
- $frontpageOutput = $this->get(sprintf(self::RUNTASTIC_SPORT_SESSIONS_URL, $this->getUsername()), [], false);
- $this->setDataFromResponse($frontpageOutput);
- $this->loggedIn = true;
- }
- return $this->loggedIn;
- }
- /**
- * Logout User's Session
- *
- * @return void
- */
- public function logout()
- {
- $this->get(self::RUNTASTIC_LOGOUT_URL);
- if ($this->getResponseStatusCode() == self::HTTP_OK) {
- $this->loggedIn = false;
- }
- }
- public function getActivities($iWeek = null, $iMonth = null, $iYear = null)
- {
- $response = [];
- if (!$this->loggedIn) {
- $this->login();
- }
- if ($this->loggedIn) {
- preg_match("/var index_data = (.*)\;/", $this->rawData, $matches);
- $itemJsonData = json_decode($matches[1]);
- $items = [];
- // Complete $iMonth with leading zeros
- if (!is_null($iMonth)) {
- $iMonth = str_pad($iMonth, 2, '0', STR_PAD_LEFT);
- }
- if (is_null($iYear)) {
- $iYear = date("Y");
- }
- foreach ($itemJsonData as $item) {
- if (!is_null($iWeek)) { /* Get week statistics */
- $sMonday = date("Y-m-d", strtotime("{$iYear}-W{$iWeek}"));
- $sSunday = date("Y-m-d", strtotime("{$iYear}-W{$iWeek}-7"));
- if ($sMonday <= $item[1] && $sSunday >= $item[1]) {
- $items[] = $item[0];
- }
- } elseif (!is_null($iMonth)) { /* Get month statistics */
- $tmpDate = $iYear."-".$iMonth."-";
- if ($tmpDate."01" <= $item[1] && $tmpDate."31" >= $item[1]) {
- $items[] = $item[0];
- }
- } elseif (!is_null($iYear)) { /* Get year statistics */
- $tmpDate = $iYear."-";
- if ($tmpDate."01-01" <= $item[1] && $tmpDate."12-31" >= $item[1]) {
- $items[] = $item[0];
- }
- } else { /* Get all statistics */
- $items[] = $item[0];
- }
- }
- // Sort activities by ID (which is the same that sorting by date)
- arsort($items);
- $postData = [
- "user_id" => $this->getUid(),
- "items" => join(',', $items),
- "authenticity_token" => $this->getToken(),
- ];
- $response = $this->post(self::RUNTASTIC_SESSIONS_URL, $postData);
- }
- return new RuntasticActivityList($response);
- }
- /**
- * Appends query array onto URL
- *
- * @param string $url
- * @param array $query
- * @return string
- */
- protected function parseGet($url, $query)
- {
- if (!empty($query)) {
- $append = strpos($url, '?') === false ? '?' : '&';
- return $url.$append.http_build_query($query);
- }
- return $url;
- }
- /**
- * Parses JSON as PHP object
- *
- * @param string $response
- * @return object
- */
- protected function parseResponse($response)
- {
- return @json_decode($response, false, 512, JSON_BIGINT_AS_STRING);
- }
- /**
- * Makes HTTP Request to the API
- *
- * @param string $url
- * @param array $parameters
- * @param string|null $request
- * @param bool $json
- * @return object|null
- */
- protected function request($url, $parameters = [], $request = null, $json = true)
- {
- $this->lastRequest = $url;
- $this->lastRequestData = $parameters;
- $curl = curl_init($url);
- $curlOptions = array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_COOKIEFILE => $this->cookieJar,
- CURLOPT_COOKIEJAR => $this->cookieJar,
- CURLOPT_TIMEOUT => $this->timeout,
- );
- if (!empty($parameters) || !empty($request)) {
- if (!empty($request)) {
- $curlOptions[CURLOPT_CUSTOMREQUEST] = $request;
- $parameters = http_build_query($parameters);
- } else {
- $curlOptions[CURLOPT_POST] = true;
- }
- $curlOptions[CURLOPT_POSTFIELDS] = $parameters;
- }
- curl_setopt_array($curl, $curlOptions);
- $response = curl_exec($curl);
- $this->lastRequestInfo = curl_getinfo($curl);
- curl_close($curl);
- return !$response ? null : ($json ? $this->parseResponse($response) : $response);
- }
- /**
- * Sends GET request to specified API endpoint
- *
- * @param string $request
- * @param array $parameters
- * @param bool $json
- * @return string
- */
- public function get($request, $parameters = [], $json = true)
- {
- $requestUrl = $this->parseGet($request, $parameters);
- return $this->request($requestUrl, [], null, $json);
- }
- /**
- * Sends PUT request to specified API endpoint
- *
- * @param string $request
- * @param array $parameters
- * @param bool $json
- * @return string
- */
- public function put($request, $parameters = [], $json = true)
- {
- return $this->request($request, $parameters, 'PUT', $json);
- }
- /**
- * Sends POST request to specified API endpoint
- *
- * @param string $request
- * @param array $parameters
- * @param bool $json
- * @return string
- */
- public function post($request, $parameters = [], $json = true)
- {
- return $this->request($request, $parameters, null, $json);
- }
- /**
- * Sends DELETE request to specified API endpoint
- */
- * @param string $request
- * @param array $parameters
- * @param bool $json
- * @return string
- */
- public function delete($request, $parameters = [], $json = true)
- {
- return $this->request($request, $parameters, 'DELETE', $json);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement