views:

99

answers:

1

So I am trying to Sync a bunch of Videos I've retrieved from a particular user from Youtube to a database table of Video Ids.

This is because YouTube does not allow the adding of meta information to a video. Hence I've created a video table on my server and would like to sync up videoids.

i.e. php/mysql app <-> youtube

The datastructure for youtube videos is as follow:

foreach ($feed as $entry) {
  print "<p>";
  print $entry->getVideoId();
  print "</p>";
}

for my db is this:

$rs->MoveFirst();
while (!$rs->EOF) {
  print "<p>";
  print $rs->fields['yt_id'];
  print "</p>";
  $rs->MoveNext();
}

Do you know how can I sync up these data so that:

  1. Whenever user uploads a new video on youtube, I can call a sync function that will retrieve the latest video and add it to the mysql db?
  2. However, if the user deletes a video on youtube, there is no delete?
+1  A: 

You could use array_diff() to compare the IDs once you have fetched them from both locations, e.g.:

//build array of video IDs in YouTube
$arYT = array();
foreach ($feed as $entry) {
    $arYT[] = $entry->getVideoId();
}

//build array of video IDs in local DB
$arDB = array();
$rs->MoveFirst();
while (!$rs->EOF) {
  $arDB[] = $rs->fields['yt_id'];
  $rs->MoveNext();
}

//to download, we want IDs which are in YouTube but not in the local Db
$idsToDownload = array_diff($arYT, $arDB);

//to delete, we want IDs which are in the local DB but not in YouTube
$idsToDelete = array_diff($arDB, $arYT);

And then you can do something like this:

//download new videos
foreach ($idsToDownload as $id) {
   //get video info for video $id and put into local db    
}

//delete deleted videos
foreach ($idsToDelete as $id) {
    //delete $id from local DB
}
Tom Haigh
thanks for introducing array_diff(); this is fantastic!
bigsurf