library.py 1.2 KB
Newer Older
Janez K's avatar
Janez K committed
1 2 3 4 5 6
'''
Streaming widgets librarby

@author: Janez Kranjc <janez.kranjc@ijs.si>
'''

Janez K's avatar
Janez K committed
7 8 9 10
def streaming_twitter(input_dict,widget,stream=None):
    output_dict = {}
    return output_dict

Janez K's avatar
Janez K committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
def streaming_rss_reader(input_dict,widget,stream=None):
    import feedparser
    from streams.models import StreamWidgetData
    feed = feedparser.parse(input_dict['url'])
    output_dict = {}
    if stream is None:
        output_dict['url'] = feed['items'][0]['link']
    else:
        try:
            swd = StreamWidgetData.objects.get(stream=stream,widget=widget)
            data = swd.value
        except:
            swd = StreamWidgetData()
            swd.stream = stream
            swd.widget = widget
            data = []
            swd.value = data
            swd.save()
        feed_length = len(feed['items'])
        feed['items'].reverse()
        for item in feed['items']:
Janez K's avatar
Janez K committed
32 33
            if item['link'] not in data:
                data.append(item['link'])
Janez K's avatar
Janez K committed
34 35 36 37 38
                swd.value = data
                swd.save()
                output_dict['url'] = item['link']
                break
        else:
Janez K's avatar
Janez K committed
39 40 41
            from streams.models import HaltStream
            raise HaltStream("Halting stream.")
    return output_dict