aboutsummaryrefslogtreecommitdiffstats
path: root/piccast/scrapers.py
blob: 933c54cc1ea9fd7ceb6a3bf0f1dffe49c6237ce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Pulls in helpers as well as utility libraries
from scrape_helpers import *

# Pulls in the PicCast model objects (PicFeed, PicSet, and Pic)
from feeds.models import *

from django.utils.html import strip_tags

"""
This file contains a set of scraper functions, one for each PicCast feed.
Helper functions are defined in scrape_helpers.py; when run as a django command
this file is included from scrape_feeds.py. 

The general pattern for each scraper is shown below. Note that neither pics nor
sets are saved by this function::

    def scrape_examplefeed(PicFeed):
        sets_data = fetch_data(PicFeed.uri)
        sets = sets_from_data(sets_data)
        filter(sets)
        foreach(sets):
            set = modifcations(set)
            pic_data = fetch_data(set)
            pics = pics_from_data(pic_data)
            filter(pics)
            set.pics = pics
            good_sets.append(set)
        return (good_sets)

It is difficult to generalize too much further because there are several source
data types for both feeds of PicSets and the list of pictures themselves. For
example, some of the known patterns which need to be accomodated are: 

    rss -> sets, foreach inline html -> pics
    rss -> sets, foreach html -> pics
    json -> sets, foreach rss -> pics
    json -> sets, foreach json -> pics
    html -> sets, foreach html -> pics

To add a new scraper, first add the PicFeed to the local database, setting
is_active to True. Then create a scrape_<shortname>() method (copy one of the
below) and modify the parameters and dataflow to suit that PicFeed. Test by
running a django shell ("./manage.py shell"), import the scrapers ("run
scrapers.py"), and then run a test for that one feed
("test_scrapers(name='shortname')"). You'll need to download some test
RSS/HTML/whatever to test with and put it in ../example_feed_data/. 

"""

def save_sets(sets):
    print "Saving " + str(len(sets)) + " new PicSets..."
    for s in sets:
        print " - " + str(s) + " with " + str(len(s.pics)) + " pics"
        s.save()
        for p in s.pics:
            p.set = s
            p.save()

# ---------------------- Real scrapers go here  ---------------------
def scrape_acidcow(pf, testing = False):
    if testing:
        pf.rssfeed_url = "../example_feed_data/acidcow_rss.xml"
        testing_uri = "../example_feed_data/acidcow_page.html"
    sets_data = fetch_rss(pf.rssfeed_url)
    sets = sets_from_rss(sets_data,
        source_url_field = "guid",
        )
    existing_urls = map(lambda s: s.source_url, pf.picset_set.all())
    sets = filter(lambda s: s.source_url not in existing_urls, sets)
    sets = filter(lambda s: s.category_name in 
        ("Pics", "Picdump", "Celebs", "Girls", "Cars"), sets) 
    good_sets = []
    for s in sets:
        if testing:
            s.source_url = testing_uri
        if(len(s.description) > 0):
            s.description = strip_tags(s.description)
            if(s.description.startswith("Similar posts:")):
                s.description = None
        full_title = s.title
        s.title = strip_parens(s.title)
        s.category = Category.objects.get_or_create(name=s.category_name)[0]
        print s
        pic_data = fetch_html(s.source_url)
        pics = pics_from_html_simple(pic_data,
            match_src = "http://acidcow.com/pics/", 
            source_url = s.source_url,
            meaningless_titles = [full_title, ],
            )
        #filter(pics,)
        if(len(pics) < 3):
            continue
        s.pics = pics
        good_sets.append(s)
    if testing:
        return good_sets
    else:
        save_sets(good_sets)

def scrape_butdoesitfloat(pf, testing = False):
    if testing:
        pf.rssfeed_url = "../example_feed_data/butdoesitfloat_rss.xml"
    sets_data = fetch_rss(pf.rssfeed_url)
    sets = sets_from_rss(sets_data,
        source_url_field = "comments",
        category = Category.objects.get_or_create(name="Art")[0]
        )
    existing_urls = map(lambda s: s.source_url, pf.picset_set.all())
    sets = filter(lambda s: s.source_url not in existing_urls, sets)
    good_sets = []
    for s in sets:
        pic_data = fetch_html("", raw=s.description)
        if(len(s.description) > 0):
            s.description = s.description.split("<img")[0]
        pics = pics_from_html_simple(pic_data,)
        #filter(pics,)
        if(len(pics) < 3):
            continue
        s.pics = pics
        good_sets.append(s)
    if testing:
        return good_sets
    else:
        save_sets(good_sets)

def scrape_nzinzi(pf, testing = False):
    if testing:
        pf.rssfeed_url = "../example_feed_data/nzinzi_rss.xml"
    sets_data = fetch_rss(pf.rssfeed_url)
    sets = sets_from_rss(sets_data,
            category = Category.objects.get_or_create(name="Art")[0],
            created_format = ("%Y-%m-%dT%H:%M:%S", -10),
        )
    existing_urls = map(lambda s: s.source_url, pf.picset_set.all())
    sets = filter(lambda s: s.source_url not in existing_urls, sets)
    good_sets = []
    for s in sets:
        pic_data = fetch_html("", raw=s.description)
        s.description = ""
        pics = pics_from_html_simple(pic_data,
            match_src = "http://ntamak.free.fr/", 
            )
        #filter(pics,)
        if(len(pics) < 3):
            continue
        s.pics = pics
        good_sets.append(s)
    if testing:
        return good_sets
    else:
        save_sets(good_sets)

def scrape_vectortut(pf, testing = False):
    if testing:
        pf.rssfeed_url = "../example_feed_data/vectortut_rss.xml"
    sets_data = fetch_rss(pf.rssfeed_url)
    sets = sets_from_rss(sets_data,
        )
    existing_urls = map(lambda s: s.source_url, pf.picset_set.all())
    sets = filter(lambda s: s.source_url not in existing_urls, sets)
    sets = filter(lambda s: s.category_name in 
        ("Inspirational",), sets) 
    good_sets = []
    for s in sets:
        pic_data = fetch_html("", raw=s.description)
        s.description = ""
        s.category = Category.objects.get_or_create(name=s.category_name)[0]
        pics = pics_from_html_simple(pic_data,
            match_src = ".cloudfront.net/", 
            )
        #filter(pics,)
        if(len(pics) < 3):
            continue
        s.pics = pics
        good_sets.append(s)
    if testing:
        return good_sets
    else:
        save_sets(good_sets)

def scrape_chubbychinese(pf, testing = False):
    if testing:
        pf.rssfeed_url = "../example_feed_data/chubbychinese_rss.xml"
    sets_data = fetch_rss(pf.rssfeed_url)
    sets = sets_from_rss(sets_data,
            category = Category.objects.get_or_create(name="Food")[0],
        )
    existing_urls = map(lambda s: s.source_url, pf.picset_set.all())
    sets = filter(lambda s: s.source_url not in existing_urls, sets)
    sets = filter(lambda s: s.category_name in 
        ("Inspirational",), sets) 
    good_sets = []
    for s in sets:
        pic_data = fetch_html("", raw=s.description)
        pics = pics_from_html_simple(pic_data,
            match_src = "static.flickr.com/", 
            )
        #filter(pics,)
        if(len(pics) < 4):
            continue
        s.pics = pics
        good_sets.append(s)
    if testing:
        return good_sets
    else:
        save_sets(good_sets)

# ---------------------- Testing routines, not required ---------------------
def test_scrapers(name=None):
    for pf in PicFeed.objects.filter(is_active=True):
        if name and pf.shortname != name:
            continue
        print "Testing " + pf.shortname + " scrapper ============================="
        try:
            scrape = globals()["scrape_" + pf.shortname]
        except:
            print "FAILED, no scrape_" + pf.shortname + " found in globals()"
            continue
        s = scrape(pf, testing=True)
        print s[0].source_url
        print s[0].title
        print s[0].category
        print s[0].pics[0].original_url
        print s[0].pics[0].title