forked from matthewhughes/PlurkScraperTool
/
main.py
63 lines (50 loc) · 1.46 KB
/
main.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
from plurk_oauth.PlurkAPI import PlurkAPI
from collections import OrderedDict
import traceback
import scraperwiki
import sys
import json
api_key = "REDACTED"
api_secret = "REDACTED"
argument = sys.argv[1]
plurk = PlurkAPI(api_key, api_secret)
class InvalidArgumentError(Exception):
pass
def main():
try:
if len(sys.argv) != 2:
raise InvalidArgumentError("Please supply a single argument. An example would be 'kittens'")
else:
search_plurk(argument)
except Exception, e:
scraperwiki.status('error', type(e).__name__)
print json.dumps({
'error': {
'type': type(e).__name__,
'message': str(e),
'trace': traceback.format_exc()
}
})
else:
scraperwiki.status('ok')
print json.dumps({
'success': {
'type': 'ok',
'message': "Searched Plurk"
}
})
def search_plurk(argument):
plurks = plurk.callAPI('/APP/PlurkSearch/search', {'query' : argument})
rows = []
for p in plurks["plurks"]:
row = OrderedDict()
row['Plurk_ID'] = p["plurk_id"]
row['Owner_ID'] = p["owner_id"]
row['Verb'] = p["qualifier"]
row['Content'] = p["content"]
rows.append(row)
submit_to_scraperwiki(rows)
def submit_to_scraperwiki(rows):
scraperwiki.sqlite.save(["plurk_id"], rows, argument)
if __name__ == '__main__':
main()