mysql_select_db(DB_DATABASE);
$users = mysql_query("select id,\n    mmr_user_id, \n    app_user_name, \n    app_auth_key,\n    notify_type,\n    last_workout_id, \n    last_update_date from mmr_notify_user where is_active = 1");
while ($row = mysql_fetch_assoc($users)) {
    $workoutCount = 0;
    $mmr = new MapMyRide($row['mmr_user_id']);
    if ($row['last_update_date']) {
        $searchFromTime = strtotime($row['last_update_date']) - 24 * 60 * 60;
    } else {
        $searchFromTime = time() - 24 * 60 * 60 * 2;
    }
    $lastWorkoutId = (int) $row['last_workout_id'];
    foreach (array_reverse($mmr->findWorkoutsSinceTime($searchFromTime)) as $workout) {
        if ((int) $workout->workoutId > (int) $row['last_workout_id']) {
            if ($row['notify_type'] == 'friendfeed') {
                $ff = new FriendFeed($row['app_user_name'], $row['app_auth_key']);
                addWorkoutLinkToFriendFeed($ff, $mmr, $workout->workoutId);
            } else {
                if ($row['notify_type'] == 'twitter') {
                    $twitter = new twitter();
                    $twitter->username = $row['app_user_name'];
                    $twitter->password = $row['app_auth_key'];
                    $twitter->user_agent = $row['MMF Updater - Issues: rob@innovationontherun.com'];
                    addWorkoutLinkToTwitter($twitter, $mmr, $workout->workoutId);
                }
            }
            $workoutCount++;
            $lastWorkoutId = max($lastWorkoutId, (int) $workout->workoutId);
        }
    }
    mysql_query("update mmr_notify_user set last_update_date = now(), last_workout_id = {$lastWorkoutId} where id={$row['id']}");
    ?>
<?php

require_once dirname(__FILE__) . "/friendfeed/friendfeed.php";
require_once dirname(__FILE__) . "/classes/mapmyride.php";
require_once dirname(__FILE__) . "/functions.php";
$mmr = new MapMyRide(1746161);
$mmr->fetch_data();
$ff = new FriendFeed("robdimarco", "lucid129horns");
if ('true' == $_GET['add']) {
    addWorkoutLinkToFriendFeed($ff, $mmr, $_GET['workoutId']);
    header("Location: index.php");
}
$feed = $ff->fetch_user_feed("robdimarco");
?>
<html>
  <head>FriendFeed/MayMyFitness Bridge</head>
  <body>
<?php 
if ($feed) {
    ?>
      <ul>
     <?php 
    foreach ($feed->entries as $entry) {
        ?>
         <li>
             <a href="<?php 
        echo $entry->link;
        ?>
"><?php 
        echo $entry->title;
        ?>