-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
254 lines (196 loc) · 6.24 KB
/
example.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'''
Datapype examples
NOTE: These examples are contrived & contain stubs for external dependenices.
'''
from datetime import datetime
from datapypes import (
Attribute,
Model,
Set,
Source,
Store,
SourcePype,
StorePype,
register_pypes
)
print '''
### Retrieve news from GoogleNews, store in MongoDB
NewsItem(query='stuff').get(GoogleNews()).save(MongoDB())
News(query='stuff').latest(GoogleNews()).save(MongoDB())
News(query='stuff').latest(GoogleNews()).filter(
lambda x: '1' in x.title
).save(MongoDB())
'''
class String(Attribute):
def _validate(self, val):
return val is None or isinstance(val, str)
class Datetime(Attribute):
def _validate(self, val):
return val is None or isinstance(val, datetime)
class NewsItem(Model):
title = String()
snippet = String()
query = String()
date = Datetime()
class News(Set):
model = NewsItem
class GoogleNews(Source):
def one(self, query):
print "Querying google for: %s" % query
# Google API hooks
return {
'title': 'Single News Item 1',
'snippet': 'The single news item 1',
'date': datetime(2012,4,13)
}
def latest(self, query):
print "Querying google for: %s" % query
# Google API hooks
return [{
'title': 'NewsItem 1',
'snippet': 'The news item 1',
'date': datetime(2012,4,13),
},{
'title': 'NewsItem 2',
'snippet': 'The news item 2',
'date': datetime(2012,4,14)
}]
class GoogleNewsPype(SourcePype):
set = News
source = GoogleNews
def one(self, query_model):
query = {'query': query_model.query}
return self.retrieve_model(self._source.one(query=query))
def latest(self, query_model):
query = {'query': query_model.query}
return self.retrieve_set(self._source.latest(query=query))
class MongoDB(Store):
def __init__(self, host='localhost', port=27017):
self._host = host
self._port = port
def save(self, collection, doc):
print "Saving data to mongo collection: %s" % collection
print doc
# pymongo hooks
class MongoNewsPype(StorePype):
set = News
store = MongoDB
def save_model(self, data_model):
collection = data_model.__class__.__name__
self._store.save(collection, data_model._data)
def save_set(self, data_set):
for obj in data_set:
self.save_model(obj)
register_pypes(MongoNewsPype, GoogleNewsPype)
NewsItem(query='stuff').get(GoogleNews()).save(MongoDB())
News(query='stuff').latest(GoogleNews()).save(MongoDB())
News(query='stuff').latest(GoogleNews()).filter(
lambda x: '1' in x.title
).save(MongoDB())
print '''
### Create a post, send to Twitter & Tumblr
Post(text='I did something interesting').save(
Twitter(username='tweet_user', password='tweet_pass'),
Tumblr(username='tumblr_user', password='tumblr_pass'))
'''
class Post(Model):
title = String()
text = String()
date = Datetime()
class Posts(Set):
model = Post
class Twitter(Store):
def __init__(self, username, password):
self._username = username
self._password = password
def save(self, text):
print "Posting to twitter: %s" % text
# Twitter API hooks
class TwitterPostPype(StorePype):
set = Posts
store = Twitter
def save_model(self, data_model):
self._store.save(data_model.text)
class Tumblr(Store):
def __init__(self, username, password):
self._username = username
self._password = password
def save(self, text):
print "Posting to tumblr: %s" % text
# Tumblr API hooks
class TumblrPostPype(StorePype):
set = Posts
store = Tumblr
def save_model(self, data_model):
self._store.save(data_model.text)
register_pypes(TwitterPostPype, TumblrPostPype)
Post(text='I did something interesting').save(
Twitter(username='tweet_user', password='tweet_pass'),
Tumblr(username='tumblr_user', password='tumblr_pass'))
print '''
### Get SoundCloud, YouTube links for a track, post to Twitter, save to MongoDB
Track(artist="Meshuggah", title="New Millenium").get(
SoundCloud(), Youtube()).save(
Twitter(username='tweet_user', password='tweet_pass'),
MongoDB(host='127.0.0.1')
'''
class URL(Attribute):
def _validate(self, val):
return val is None or val.startswith("http")
class Track(Model):
artist = String()
title = String()
track_url = URL()
video_url = URL()
class Tracks(Set):
model = Track
class SoundCloud(Source):
def one(self, query):
print "Searching SoundCloud for: %s" % query
return {'track_url': 'http://soundcloud/'}
class Youtube(Source):
def one(self, query):
print "Searching YouTube for: %s" % query
return {
'video_url': 'http://youtube/',
'track_url': 'http://youtube/'
}
class SoundCloudTrackPype(SourcePype):
set = Tracks
source = SoundCloud
def one(self, query_model):
query = {'query': "%s %s" % (query_model.artist, query_model.title)}
return self.retrieve_model(self._source.one(**query))
class YoutubeTrackPype(SourcePype):
set = Tracks
source = Youtube
def one(self, query_model):
query = {'query': "%s %s" % (query_model.artist, query_model.title)}
return self.retrieve_model(self._source.one(**query))
class TwitterTrackPype(StorePype):
set = Tracks
store = Twitter
def save_model(self, data_model):
tweet = "'%s' by '%s'" % (data_model.title, data_model.artist)
if data_model.track_url:
tweet += ". Track: %s" % data_model.track_url
if data_model.video_url:
tweet += ". Video: %s" % data_model.video_url
self._store.save(tweet)
class MongoDBTrackPype(StorePype):
set = Tracks
store = MongoDB
def save_model(self, data_model):
coll = data_model.__class__.__name__
self._store.save(coll, data_model._data)
register_pypes(
SoundCloudTrackPype,
YoutubeTrackPype,
TwitterTrackPype,
MongoDBTrackPype
)
# register(*globals().values())
Track(artist="Meshuggah", title="New Millenium").get(
SoundCloud(), Youtube()).save(
Twitter(username='tweet_user', password='tweet_pass'),
MongoDB(host='127.0.0.1'))