]>
Commit | Line | Data |
---|---|---|
1 | <? | |
2 | require_once 'config.php'; | |
3 | ||
4 | function update_all_feeds($link) { | |
5 | ||
6 | $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE | |
7 | last_updated is null OR | |
8 | EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " . | |
9 | MIN_UPDATE_TIME); | |
10 | ||
11 | while ($line = pg_fetch_assoc($result)) { | |
12 | update_rss_feed($link, $line["feed_url"], $line["id"]); | |
13 | } | |
14 | ||
15 | } | |
16 | ||
17 | function update_rss_feed($link, $feed_url, $feed) { | |
18 | ||
19 | $rss = fetch_rss($feed_url); | |
20 | ||
21 | if ($rss) { | |
22 | ||
23 | foreach ($rss->items as $item) { | |
24 | ||
25 | $entry_guid = $item["id"]; | |
26 | ||
27 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
28 | if (!$entry_guid) $entry_guid = $item["link"]; | |
29 | ||
30 | $entry_timestamp = $item["pubdate"]; | |
31 | if (!$entry_timestamp) $entry_timestamp = $item["modified"]; | |
32 | if (!$entry_timestamp) $entry_timestamp = $item["updated"]; | |
33 | ||
34 | $entry_timestamp = strtotime($entry_timestamp); | |
35 | ||
36 | if (!$entry_timestamp) continue; | |
37 | ||
38 | $entry_title = $item["title"]; | |
39 | $entry_link = $item["link"]; | |
40 | ||
41 | if (!$entry_title) continue; | |
42 | if (!$entry_link) continue; | |
43 | ||
44 | $entry_content = $item["description"]; | |
45 | if (!$entry_content) $entry_content = $item["content"]; | |
46 | ||
47 | $entry_content = pg_escape_string($entry_content); | |
48 | $entry_title = pg_escape_string($entry_title); | |
49 | ||
50 | $content_md5 = md5($entry_content); | |
51 | ||
52 | $result = pg_query($link, " | |
53 | SELECT | |
54 | id,unread,md5_hash | |
55 | FROM | |
56 | ttrss_entries | |
57 | WHERE | |
58 | guid = '$entry_guid' OR md5_hash = '$content_md5'"); | |
59 | ||
60 | if (pg_num_rows($result) == 0) { | |
61 | ||
62 | $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
63 | ||
64 | $query = "INSERT INTO ttrss_entries | |
65 | (title, guid, link, updated, content, feed_id, md5_hash) | |
66 | VALUES | |
67 | ('$entry_title', '$entry_guid', '$entry_link', | |
68 | '$entry_timestamp', '$entry_content', '$feed', | |
69 | '$content_md5')"; | |
70 | ||
71 | pg_query($link, $query); | |
72 | ||
73 | } else { | |
74 | ||
75 | $entry_id = pg_fetch_result($result, 0, "id"); | |
76 | $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
77 | ||
78 | $unread = pg_fetch_result($result, 0, "unread"); | |
79 | $md5_hash = pg_fetch_result($result, 0, "md5_hash"); | |
80 | ||
81 | if ($md5_hash != $content_md5 && CONTENT_CHECK_MD5) | |
82 | $unread = "true"; | |
83 | ||
84 | if ($unread || !CONTENT_CHECK_MD5) { | |
85 | $updated_query_part = "updated = '$entry_timestamp',"; | |
86 | } | |
87 | ||
88 | $query = "UPDATE ttrss_entries | |
89 | SET | |
90 | title ='$entry_title', | |
91 | link = '$entry_link', | |
92 | $updated_query_part | |
93 | content = '$entry_content', | |
94 | md5_hash = '$content_md5', | |
95 | unread = '$unread' | |
96 | WHERE | |
97 | id = '$entry_id'"; | |
98 | ||
99 | $result = pg_query($link, $query); | |
100 | ||
101 | ||
102 | // print "$entry_guid - $entry_timestamp - $entry_title - | |
103 | // $entry_link - $entry_id<br>"; | |
104 | ||
105 | } | |
106 | ||
107 | } | |
108 | ||
109 | $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()"); | |
110 | ||
111 | } | |
112 | ||
113 | } | |
114 | ||
115 | ||
116 | ||
117 | ||
118 | ?> |