]> khamidou — GitHub mirrors - fh.git/commitdiff
re-added modified files
authorkhamidou <karim@lambda-expr.org>
Sun, 1 Feb 2009 14:32:28 +0000 (15:32 +0100)
committerkhamidou <karim@lambda-expr.org>
Sun, 1 Feb 2009 14:32:28 +0000 (15:32 +0100)
fh-fetch [new file with mode: 0755]
fh-inc [new file with mode: 0755]

diff --git a/fh-fetch b/fh-fetch
new file mode 100755 (executable)
index 0000000..2987a55
--- /dev/null
+++ b/fh-fetch
@@ -0,0 +1,128 @@
+#!/usr/bin/python
+# Fetch the specified entries.
+# Arguments : fh-fetch         from    date [to date]
+#                      feeds   *feed-name-regexp*
+#                      show    feed-title,entry-title,entry-contents
+
+import os
+import sys
+import glob
+import feedparser
+import time, datetime
+import getopt
+
+global fetch_params
+fetch_params = { "feed-title": 1, "feed-summary": 1, "feed-link": 1, "entry-title": 1, "entry-subtitle": 1, "entry-link": 1, "entry-content": 1, }
+
+sep = "" # the separator used to ...separate each entry of the feed
+i = 0
+
+# date and time parsing code comes from http://www.logarithmic.net/pfh/blog/01162445830
+
+time_formats = ['%H : %M', '%I : %M %p', '%H', '%I %p']
+
+date_formats_with_year = ['%d %m %Y', '%Y %m %d', '%d %B %Y', '%B %d %Y',
+                                                  '%d %b %Y', '%b %d %Y',
+                          '%d %m %y', '%y %m %d', '%d %B %y', '%B %d %y',
+                                                  '%d %b %y', '%b %d %y']
+
+date_formats_without_year = ['%d %B', '%B %d',
+                             '%d %b', '%b %d']
+
+def parse_time(string):
+    string = string.strip()
+    if not string: return None
+    
+    for format in time_formats:
+        try:
+            result = time.strptime(string, format)
+            return datetime.time(result.tm_hour, result.tm_min)
+        except ValueError:
+            pass
+            
+    raise ValueError()
+
+    
+def parse_date(string):
+    string = string.strip()
+    if not string: return None
+    
+    string = string.replace('/',' ').replace('-',' ').replace(',',' ')
+    
+    for format in date_formats_with_year:
+        try:
+            result = time.strptime(string, format)
+            return datetime.date(result.tm_year, result.tm_mon, result.tm_mday)
+        except ValueError:
+            pass
+
+    for format in date_formats_without_year:
+        try:
+            result = time.strptime(string, format)
+            year = datetime.date.today().year
+            return datetime.date(year, result.tm_mon, result.tm_mday)
+        except ValueError:
+            pass
+            
+    raise ValueError()
+
+
+def print_field(field):
+       print field.encode("utf8").rstrip("\n")
+       print sep
+
+def main():
+
+    if len(sys.argv) > 1:
+        try:
+            fetch_params = { }
+            opts, args = getopt.getopt(sys.argv[1:], "tsl", ["title", "subtitle", "link",])
+        except getopt.GetoptError, err:
+            print str(err)
+            sys.exit(2)
+            
+            for o, a in opts:
+                if o in ("-t", "--title"):
+                    fetch_params["feed-title"] = 1
+                elif o in ("-s", "--summary"):
+                    fetch_params["feed-summary"] = 1
+                elif o in ("-l", "--link"):
+                    fetch_params["feed-link"] = 1
+                else:
+                    assert False, "unhandled option"
+               
+
+    dir = glob.iglob(os.environ.get("HOME") + "/.fh/feeds/*")
+
+    for file in dir:
+        feed = feedparser.parse(file)
+        
+        if fetch_params["feed-title"]:
+            if feed.feed.has_key("title"):
+                print_field(feed.feed.subtitle)
+
+               if fetch_params["feed-summary"]:
+                    if feed.feed.has_key("sumary"):
+                        print_field(feed.summary)
+
+
+            for e in feed.entries:
+
+                if fetch_params["entry-subtitle"]:
+                    print_field(e.title)
+                    
+                if fetch_params["entry-link"]:
+                    if feed.feed.has_key("links"):
+                        print_field(e.links[0]["href"])
+                            
+                if fetch_params["entry-subtitle"]:
+                    if feed.feed.has_key("description"):
+                        print_field(e.description)
+                                
+                if fetch_params["entry-content"]:
+                    if feed.feeds.has_key("content"):
+                        print_field(e.content[0].value)
+                                
+                       print "\n"
+if __name__ == "__main__":
+    main()
diff --git a/fh-inc b/fh-inc
new file mode 100755 (executable)
index 0000000..1d2fa4c
--- /dev/null
+++ b/fh-inc
@@ -0,0 +1,29 @@
+#!/usr/bin/ruby
+#
+# fh-inc - get data from the feeds refered in ~/.fh/feedlist
+
+require 'net/http'
+require 'uri'
+
+fd = File.new(ENV["HOME"] + "/.fh/feedlist")
+
+counter = 0
+
+fd.each_line do |line|
+  line = line.chomp
+  if line != ""
+    s = Net::HTTP::get URI::parse(line)
+    
+    # Each file is named according to the url of the feed.
+    fname = line.gsub("http://", "")
+    fname.gsub!("/", "-")
+
+    File.open(ENV["HOME"] + "/.fh/feeds/" + fname, "w+") do |ff|
+      ff.write(s)
+    end
+
+    counter += 1
+  end
+end
+
+print "Fetched ", counter, " feeds\n"