OSDN Git Service

dev20091125_wait_timeブランチのマージ
[coroid/NicoBrowser.git] / src / nicobrowser / main / Main.java
1 /*$Id$*/
2 package nicobrowser.main;
3
4 import nicobrowser.update.DBUpdater;
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.HashMap;
11 import java.util.List;
12 import javax.persistence.EntityManager;
13 import javax.persistence.EntityManagerFactory;
14 import javax.persistence.EntityTransaction;
15 import javax.persistence.Persistence;
16 import javax.persistence.Query;
17 import nicobrowser.Config;
18 import nicobrowser.GetFlvResult;
19 import nicobrowser.Config.NicoFeed;
20 import nicobrowser.NicoHttpClient;
21 import nicobrowser.entity.NicoContent;
22 import nicobrowser.entity.NicoContent.Status;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 public class Main {
27
28     private static Log log = LogFactory.getLog(Main.class);
29
30     public static void main(String[] args) throws IOException {
31         boolean res = Config.createNewConfigFiles();
32         if (res) {
33             System.out.println("コンフィグファイルを作成しました. 編集後, 再実行してください.");
34             return;
35         }
36
37         DBUpdater updater = new DBUpdater();
38         if (args.length > 0 && "sync".equals(args[0])) {
39             updater.sync();
40             return;
41         }
42         updater.update();
43
44         new Main().start();
45     }
46
47     public void start() {
48         log.info("program start");
49
50         final Config config = Config.getInstance();
51         List<NicoContent> dailyList = new ArrayList<NicoContent>();
52         ArrayList<List<NicoContent>> myLists = new ArrayList<List<NicoContent>>();
53         NicoHttpClient instance = null;
54         try {
55             log.info("リストを取得します");
56             instance = NicoHttpClient.getInstance();
57             List<NicoFeed> feeds = config.getNicoFeeds();
58             for (NicoFeed f : feeds) {
59                 List<NicoContent> list = instance.getContentsFromRss(f.getUrl());
60                 int count = 0;
61                 for (NicoContent l : list) {
62                     if (count >= f.getNumber()) {
63                         break;
64                     }
65                     dailyList.add(l);
66                     count++;
67                 }
68             }
69             List<String> mylists = config.getDownLoadMyList();
70             for (String l : mylists) {
71                 List<NicoContent> list = instance.loadMyList(l);
72                 myLists.add(list);
73             }
74         } catch (Exception e) {
75             e.printStackTrace();
76             return;
77         }
78
79         log.info("今回取得したデータを過去の取得データと比較します");
80
81         EntityManagerFactory factory;
82         EntityManager manager;
83
84         HashMap<String, String> map = new HashMap<String, String>();
85         map.put("toplink.jdbc.url", "jdbc:h2:" + config.getDbFile());
86         factory = Persistence.createEntityManagerFactory("NicoBrowserPU", map);
87         manager = factory.createEntityManager();
88
89         EntityTransaction transaction = manager.getTransaction();
90
91         transaction.begin();
92         try {
93             // ランキング上位のコンテンツ保存
94             int num = 0;
95             for (NicoContent c : dailyList) {
96                 save(manager, c);
97                 num++;
98             }
99
100             // マイリストに登録したコンテンツ保存
101             for (List<NicoContent> l : myLists) {
102                 for (NicoContent c : l) {
103                     save(manager, c);
104                 }
105             }
106
107             transaction.commit();
108
109             Query query = manager.createQuery("SELECT cont FROM NicoContent AS cont " + "WHERE ?1 <> cont.status").
110                     setParameter(1, NicoContent.Status.GET_FILE);
111             List<NicoContent> results = query.getResultList();
112             instance.login(config.getNicoMail(), config.getNicoPassword());
113             Date prevDate = null;
114             for (NicoContent c : results) {
115                 if (c.getFailTimes() > config.getMaxRetry() + 1) {
116                     continue;
117                 }
118                 if (prevDate != null) {
119                     Date nowDate = Calendar.getInstance().getTime();
120                     long sleep = nowDate.getTime() - prevDate.getTime();
121                     sleep = config.getWaitTime() * 1000 - sleep;
122                     if (sleep > 0) {
123                         log.info("" + sleep + "ms sleep");
124                         try {
125                             Thread.sleep(sleep);
126                         } catch (InterruptedException e) {
127                             log.info("スリープ中断", e);
128                         }
129                     }
130                 }
131                 prevDate = Calendar.getInstance().getTime();
132                 File saveLocation = new File(config.getSrcSaveDir(), c.getFileName());
133                 log.info("ファイルを取得します: " + c.getNicoId() + " " + c.getTitle());
134                 try {
135                     GetFlvResult result = instance.getFlvFile(c.getNicoId(),
136                             saveLocation.getCanonicalPath(), c.getStatus(),
137                             true);
138                     c.setAuthor(result.getAuthor());
139                     Status status = result.getStatus();
140                     c.setStatus(status);
141                     if (status == Status.GET_INFO) {
142                         c.setFailTimes(c.getFailTimes() + 1);
143                     }
144                 } catch (Exception ex) {
145                     c.setFailTimes(c.getFailTimes() + 1);
146                     log.error("ファイル取得に失敗しました。" + c.getNicoId() + ", 通算失敗回数: " + c.getFailTimes(), ex);
147                 }
148                 transaction.begin();
149                 manager.persist(c);
150                 transaction.commit();
151                 log.info("完了しました");
152             }
153         } catch (Exception ex) {
154             ex.printStackTrace();
155             transaction.rollback();
156         } finally {
157             manager.close();
158             factory.close();
159         }
160
161     }
162
163     private void save(EntityManager manager, NicoContent c) {
164         Query query = manager.createQuery("SELECT cont FROM NicoContent AS cont " + "WHERE ?1 = cont.nicoId").
165                 setParameter(1, c.getNicoId());
166         List<NicoContent> resList = query.getResultList();
167         if (resList.isEmpty()) {
168             log.info("NEW! " + c.getNicoId() + " : " + c.getFileName());
169             manager.persist(c);
170         }
171     }
172 }