コード例 #1
0
def post_to_social_networks(user, social_networks_list, body, url, campaign, testing=False):
    ##################
    # if they post to social networks:
    ##################
    # TODO: clean up the Sharing module and this stuff -- sharing should not keep accessing ourmy_app models
    singly = Singly(SINGLY_CLIENT_ID, SINGLY_CLIENT_SECRET)
    try:
        user_profile = user.get_profile()
        try:
            access_token = user_profile.access_token
        except:
            pass

        # get the list of social networks the user posted to from the checkboxes
        social_networks_string = ",".join(social_networks_list)

        payload = {'access_token' : access_token, 
                   'services': social_networks_string, 
                   'body': body, 
                   'url': url
                   }
        if testing:
            return_data = {'twitter': {'favorited': False, 'in_reply_to_user_id': None, 'contributors': None, 'truncated': False, 'source': '<a href="http://zoomtilt.com" rel="nofollow">Ourmy</a>', 'text': "Cryogenically frozen '70s cops.  Nuff said.\n         http://t.co/FPtDEjc8", 'created_at': 'Thu Jan 17 18:54:15 +0000 2013', 'retweeted': False, 'in_reply_to_status_id': None, 'coordinates': None, 'id': 291981776858001400, 'entities': {'user_mentions': [], 'hashtags': [], 'urls': [{'url': 'http://t.co/FPtDEjc8', 'indices': [53, 73], 'expanded_url': 'http://bit.ly/UxlrW7', 'display_url': 'bit.ly/UxlrW7'}]}, 'in_reply_to_status_id_str': None, 'place': None, 'id_str': '291981776858001409', 'in_reply_to_screen_name': None, 'retweet_count': 0, 'geo': None, 'in_reply_to_user_id_str': None, 'possibly_sensitive': False, 'user': {'follow_request_sent': False, 'profile_use_background_image': True, 'id': 42620688, 'description': 'Co-founder at ZoomTilt, filmmaker (shorts & webseries), jazz trumpet player.  Bringing together fans and filmmakers in an online TV studio!', 'verified': False, 'profile_text_color': '333333', 'profile_image_url_https': 'https://si0.twimg.com/profile_images/383009564/headshot_normal.JPG', 'profile_sidebar_fill_color': 'DDEEF6', 'geo_enabled': False, 'entities': {'url': {'urls': [{'url': 'http://www.annacallahan.com', 'indices': [0, 27], 'expanded_url': None, 'display_url': None}]}, 'description': {'urls': []}}, 'followers_count': 221, 'profile_sidebar_border_color': 'C0DEED', 'id_str': '42620688', 'default_profile_image': False, 'listed_count': 12, 'utc_offset': -18000, 'statuses_count': 96, 'profile_background_color': 'C0DEED', 'friends_count': 248, 'location': 'Boston', 'profile_link_color': '0084B4', 'profile_image_url': 'http://a0.twimg.com/profile_images/383009564/headshot_normal.JPG', 'following': False, 'profile_background_image_url_https': 'https://si0.twimg.com/images/themes/theme1/bg.png', 'profile_background_image_url': 'http://a0.twimg.com/images/themes/theme1/bg.png', 'screen_name': 'jazztpt', 'lang': 'en', 'profile_background_tile': False, 'favourites_count': 1, 'name': 'Anna Callahan', 'notifications': False, 'url': 'http://www.annacallahan.com', 'created_at': 'Tue May 26 13:11:10 +0000 2009', 'contributors_enabled': False, 'time_zone': 'Eastern Time (US & Canada)', 'protected': False, 'default_profile': True, 'is_translator': False}}}
        else:           
            return_data = singly.make_request('/types/news', method='POST', request=payload)        
            # print "========================== return data from singly ==========="
            # print return_data


        snc = SharingAction.SOCIAL_NETWORK_CHOICES
        for social_network in social_networks_list:
            try:
                success = return_data[social_network]['id']
                if success is not None:
                    # if they have successfully posted, we create a SharingUserAction for them and store it in the database
                    sn_name = ''
                    for sn in snc:
                        if sn[1] == social_network:
                            sn_name = sn[0]
                    sharing_action = SharingAction.objects.get(action__campaign=campaign, social_network=sn_name, post_or_click=False)
                    sharing_user_action = SharingUserAction(user=user, sharing_action=sharing_action)
                    sharing_user_action.save()
                    # we only need one click action per url, so check to see if there is one, if not create
                    sharing_click_action = SharingAction.objects.get(action__campaign=campaign, post_or_click=True)
                    SharingUserAction.objects.get_or_create(user=user, sharing_action=sharing_click_action)
            except:
                pass
    except:
        # print "drat - no singly profile"
        pass