Created
August 5, 2012 15:20
-
-
Save suzuki0keiichi/3265373 to your computer and use it in GitHub Desktop.
flickrjを使って投稿日と撮影日を合わせるプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.xml.sax.SAXException; | |
import com.aetrion.flickr.FlickrException; | |
import com.aetrion.flickr.REST; | |
import com.aetrion.flickr.RequestContext; | |
import com.aetrion.flickr.auth.Auth; | |
import com.aetrion.flickr.auth.Permission; | |
import com.aetrion.flickr.photos.Extras; | |
import com.aetrion.flickr.photos.Photo; | |
import com.aetrion.flickr.photos.PhotosInterface; | |
import com.aetrion.flickr.photos.SearchParameters; | |
public class RepairDate { | |
/** | |
* date_postedをdate_takenに出来るだけ合わせる | |
* | |
* 事前にアプリをflickrに申請しキーと秘密キーを取得しておき、 | |
* ユーザーの認証も終わってtokenも取得しておく必要がある。 | |
* (tokenの方はflickrjの認証サンプルを見れば簡単に分かるはず) | |
* | |
* @param key flickrからアプリ申請時に発行されたキー | |
* @param secretKey flickrからアプリ申請時に発行された秘密のキー | |
* @param token ユーザー認証後にもらえるトークン | |
* @param joinDate 入会日 | |
* @param startPage 処理開始ページ(1ページ100アイテム) | |
* @param endPage 処理終了ページ | |
* @throws FlickrException | |
* @throws ParserConfigurationException | |
* @throws IOException | |
* @throws SAXException | |
*/ | |
public void repair(String key, String secretKey, String token, | |
Date joinDate, int startPage, int endPage) throws FlickrException, | |
ParserConfigurationException, IOException, SAXException { | |
RequestContext requestContext = RequestContext.getRequestContext(); | |
Auth auth = new Auth(); | |
auth.setPermission(Permission.WRITE); | |
auth.setToken(token); | |
requestContext.setAuth(auth); | |
requestContext.setExtras(Extras.ALL_EXTRAS); | |
PhotosInterface api = new PhotosInterface(key, secretKey, new REST()); | |
for (int page = startPage; page <= endPage; page++) { | |
SearchParameters params = new SearchParameters(); | |
params.setUserId("me"); | |
/* | |
* flickr側ではsearchはextrasパラメーターを受け取れるが、 | |
* flickrjでは受け取るようになっていないのでソースコード側を書き換え、 | |
* RequestContextからextrasを受け取りparametersに追加してあげる必要がある | |
*/ | |
for (Photo photo : api.search(params, 100, page)) { | |
// 撮影日と同じか投稿日のほうが古くなっていれば何もしない(投稿日のほうが古いのも異常だが意図的にやらないと発生しないため無視する) | |
if (photo.getDateTaken().getTime() >= photo.getDatePosted() | |
.getTime()) { | |
continue; | |
} | |
// 入会日より前の日は設定できないので、設定できるものだけ設定する(そうしないと全て入会日で渋滞が起こってしまうためやったが、それでも寄せたい場合はこのifを外せば良い) | |
if (photo.getDateTaken().getTime() >= joinDate.getTime()) { | |
api.setDates(photo.getId(), photo.getDateTaken(), null, | |
null); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment