Advertisement
goebelmasse

RSS-Feeds zu GNUsocial bringen

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