Advertisement
goebelmasse

RSS-Feeds zu GNUsocial bringen (fehlerbereinigt)

Sep 16th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ########################################################################
  4. #
  5. # rss2queet.py
  6. # $Id: rss2queet.py,v 1.6 2014/09/16 20:08:15 elias Exp elias $
  7. #
  8. # Das muss ich leider in Python 2 coden, weil ich auf dem Serverchen
  9. # kein anderes Python habe. (Ich halte die Installation dort so klein
  10. # wie möglich).
  11. #
  12. # Eine Portierung nach Python 3 ist relativ aufwändig.
  13. #
  14. # Und ja, ich weiß, wie dieser Code saugt. Ich habe ihn sehr schnell
  15. # geschrieben, weil ich ihn haben wollte, um schneller von Twitter
  16. # wegzukommen. Er gewinnt keine Ästhetikpreise und hat mich trotz des
  17. # allgegenwärtigen Quick-and-Dirty-Stils länger aufgehalten, als ich
  18. # mir gewünscht habe. Fehlerbehandlung fehlt völlig.
  19. #
  20. # Dieses Skript ist unter den Bedingungen der Piratenlizenz lizenziert
  21. # http://www.tamagothi.de/impressum/lizenz/
  22. #
  23. ########################################################################
  24.  
  25. import ConfigParser
  26. import os.path
  27. import xml.dom.minidom
  28. import re
  29. import datetime
  30. import time
  31. import urllib
  32. import httplib
  33.  
  34.  
  35. ########################################################################
  36. #
  37. # Konfiguration
  38. # -------------
  39. #
  40. # Hier werden einfach alle URLs der RSS-Feeds in ein Array eingetragen.
  41. # Der Rest verläuft "automagisch".
  42. #
  43. # Hier sind einfach die RSS-Feeds meiner Blögchen eingetragen.
  44. #
  45. feeds = [ 'http://schwerdtfegr.wordpress.com/feed/',
  46.           'http://spam.tamagothi.de/feed/',
  47.           'http://www.tamagothi.de/author/elias/feed/',
  48.           'http://alarmknopf.wordpress.com/feed/',
  49.           'http://gagada.wordpress.com/feed/',
  50.           'http://fraktalwelten.wordpress.com/feed/',
  51.           'http://tamagothi.wordpress.com/feed/',
  52.           'http://proll.wordpress.com/feed/',
  53.           'http://wwwut.wordpress.com/feed/',
  54.         ]
  55. #
  56. # API-Entrypoint für den Statusupdate
  57. # Hier kann natürlich auch ein anderer Einstiegspunkt stehen.
  58. #
  59. api_update = 'https://quitter.se/api/statuses/update.xml'
  60. #
  61. # Username
  62. #
  63. username = '----'
  64. #
  65. # Passwort
  66. #
  67. password = '----'
  68. #
  69. # Da diese Datei ein Passwort im Klartext enthält, sollte sie vielleicht
  70. # nicht mit chmod 666 abgelegt werden. ;)
  71. #
  72. # In den meisten Fällen braucht ab hier nicht mehr angepasst zu werden.
  73. #
  74. ########################################################################
  75. #
  76. config_path = os.path.expanduser('~/.rss2queet')
  77. #
  78. # Ab hier gibts nichts mehr, was angepasst werden muss.
  79. # Vielleicht erschlägt mal jemand die Fehler, die ich gemacht habe... :D
  80. #
  81. ########################################################################
  82.  
  83. # Globale Variablen
  84. # -----------------
  85.  
  86. config = ConfigParser.ConfigParser()
  87. time_now = int(datetime.datetime.utcnow().strftime('%s'))
  88. timestamp_re = re.compile(r' *\+\d*$')
  89.  
  90.  
  91. # Programm
  92. # --------
  93. #
  94. # Sorry, auf hilfreiche Kommentare wurde weitgehend verzichtet.
  95.  
  96. class AuthURLOpener(urllib.FancyURLopener):
  97.     version = 'RSS2Queet/0.1beta'
  98.    
  99.     def get_user_passwd(self, host, real, clear_cache=0):
  100.         return (username, password)
  101.  
  102. urllib._urlopener = AuthURLOpener()
  103.  
  104.  
  105. def init():
  106.     config.read(config_path)
  107.  
  108.  
  109. def cleanup():
  110.     config.set('DEFAULT', 'lastrun', time_now)
  111.     f = open(config_path, 'wt')
  112.     config.write(f)
  113.     f.close()
  114.    
  115.  
  116. def make_unix_timestamp(time_string):
  117.     # ARRGH!
  118.     # No better way than strptime and ignoring timezone in Python 2.
  119.     cleaned_up = timestamp_re.sub('', time_string)
  120.     return datetime.datetime.strptime(cleaned_up, '%a, %d %b %Y %H:%M:%S')
  121.  
  122.  
  123. def queet_item(text, link):
  124.     message = "%s %s" % (text, link)
  125.     print message
  126.     data = urllib.urlencode({'status': message})
  127.     r = urllib.urlopen(api_update, data)
  128.    
  129.  
  130. def get_text_easy(nodelist):
  131.     accu = []
  132.     for node in nodelist:
  133.         if node.nodeType == node.TEXT_NODE:
  134.             accu.append(node.data)
  135.     return ''.join(accu)
  136.  
  137.  
  138. def get_node_text(node):
  139.     return get_text_easy(node.childNodes)
  140.  
  141.  
  142. def get_item_pubdate(item):
  143.     pubdate_str = get_node_text(item.getElementsByTagName('pubDate')[0])
  144.     return make_unix_timestamp(pubdate_str)
  145.  
  146.  
  147. def handle_item(item, lastrun):
  148.     item_timestamp = get_item_pubdate(item)
  149.     last_timestamp = datetime.datetime.fromtimestamp(lastrun)
  150.     if item_timestamp <= last_timestamp:
  151.         return
  152.     item_title = get_node_text(item.getElementsByTagName('title')[0])
  153.     item_uri = get_node_text(item.getElementsByTagName('guid')[0])
  154.     queet_item(item_title, item_uri)
  155.    
  156.  
  157. def handle_item_list(item_list, lastrun):
  158.     for item in item_list:
  159.         handle_item(item, lastrun)
  160.  
  161.  
  162. def handle_channel(channel_doc, lastrun):
  163.     handle_item_list(channel_doc.getElementsByTagName('item'), lastrun)
  164.  
  165.    
  166. def get_document_for_feed(feed_url):
  167.     return xml.dom.minidom.parse(urllib.urlopen(feed_url))
  168.  
  169.  
  170. def handle_feed(feed_url):
  171.     if not config.has_section(feed_url):
  172.         config.add_section(feed_url)
  173.         config.set(feed_url, 'lastrun', time_now)
  174.     else:
  175.         lastrun = config.getfloat(feed_url, 'lastrun')
  176.         config.set(feed_url, 'lastrun', time_now)
  177.         handle_channel(get_document_for_feed(feed_url), lastrun)
  178.  
  179.  
  180. def main():
  181.     init()
  182.     for feed_url in feeds:
  183.         handle_feed(feed_url)
  184.     cleanup()
  185.  
  186.  
  187. if __name__ == '__main__':
  188.     main()
  189.  
  190.  
  191. ########################################################################
  192. #
  193. # It must be a starving man
  194. #   Who likes to hear
  195. #   These crippled minds talk
  196. # Greetings from me
  197. #   Following the wind...
  198. #
  199. # Wolfsheim: Elias
  200. # https://www.youtube.com/watch?v=IEenE3eMagE
  201. #
  202. ########################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement