/
social_media.py
58 lines (52 loc) · 2.04 KB
/
social_media.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
import logging
import httplib
import urllib2
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from twython import exceptions
from social_website.utils.website_footer_social import WebsiteFooter
from social_website.utils.facebook_newsfeed import get_facebook_feed
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('-a', '--all',
action='store_true',
default=False,
help='Update news feed and footer statistics'),
make_option('-f', '--footer',
action='store_true',
default=False,
help='Update footer statistics'),
make_option('-n', '--newsfeed',
action='store_true',
default=False,
help='Update news feed from Facebook'),
)
def handle(self, *args, **options):
if (options['all']):
self.update_footer_stats()
get_facebook_feed()
if (options['footer']):
self.update_footer_stats()
if (options['newsfeed']):
get_facebook_feed()
def update_footer_stats(self):
logger = logging.getLogger('social_website')
footer = WebsiteFooter()
try:
footer.fetch_facebook_likes()
except (httplib.HTTPException, urllib2.HTTPError, urllib2.URLError):
logger.error("error in updating facebook likes")
try:
footer.fetch_twitter_followers()
except exceptions:
logger.error("error in updating twitter followers")
try:
footer.fetch_youtube_videos()
except (httplib.HTTPException, urllib2.HTTPError, urllib2.URLError):
logger.error("error in updating youtube videos")
try:
footer.fetch_linkedin_subscribers()
except Exception:
logger.error("error in updating linkedin subscribers")
footer.write()
logger.info("Updated footer stats")