forked from mitnk/tengtweets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·52 lines (47 loc) · 1.71 KB
/
manage.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
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tengtweets.settings")
from django.core.management import execute_from_command_line
if len(sys.argv) == 2 and sys.argv[1] == "fetch":
import os
lock_file = '/tmp/tengtweets_fetch_lock.tmp'
if os.path.exists(lock_file):
exit(0)
from tengtweets.tweets.models import Tweet
from tweepy import api
import logging
import time
open(lock_file, 'w').close()
logger = logging.getLogger('info')
logger_error = logging.getLogger('error')
count = 0
count_zh = 0
for i in range(5):
tweets = api.search(lang='zh', q='*', rpp=100)
for t in tweets:
try:
if (Tweet.objects.create_tweet(t)):
count += 1
count_zh += 1
except Exception, e:
logger_error.error("Error: %s id: %s text: %s" % (
e, t.id, t.text
))
time.sleep(8)
tweets = api.search(lang='en', q='*', rpp=10)
for t in tweets:
try:
if (Tweet.objects.create_tweet(t)):
count += 1
except Exception, e:
logger_error.error("Error: %s id: %s text: %s" % (
e, t.id, t.text
))
time.sleep(1)
logger.info("saved %d tweets (%d zh)" % (count, count_zh))
if os.path.exists(lock_file):
os.remove(lock_file)
else:
execute_from_command_line(sys.argv)