コード例 #1
0
def get_add_data(post_data):
    """
    Geolocates the post, finds the user's other image posts and gets the closest
    wikipedia artile
    """
    post = None
    user = None
    geo = None
    loc_info = None
    add_data = None
    try:
        created = post_data['created']
    except KeyError:
        created = post_data['created_utc']
    post = Post(post_data['id'], created, post_data['url'], post_data['author'],
                post_data['title'], post_data['score'], post_data['num_comments'])
    user = UserContent(post.author)
    geo = Geolocation(post.token_title())
    loc_info = LocationInfo(geo.lat, geo.lng)
    add_data = {
        'token_title': post.token_title(),
        'updated_score': post.updated_score(),
        'thumb_url': post.thumb_url(),
        'other_posts': user.other_posts(),
        'pie_chart': user.pie_chart(),
        'opencage_geo': geo.geocode_array,
        'loc': geo.location(),
        'geoJSON': loc_info.geoJSON(post),
        'wiki_page_id': loc_info.wiki_page_ids_all(),
        'wiki_content': loc_info.closest_wiki_content(),
        'wiki_title':   loc_info.closest_wiki_title(),
        'wiki_extract': loc_info.closest_wiki_extract()
    }
    return add_data
コード例 #2
0
class PostTest(unittest.TestCase):

    def setUp(self):
        self.post = Post('8405kv', '946684800', 'https://www.imgur.xyz/home/pic.jpg', 'will', 'This is a picture of Sheffield', 5555, 33)

    def test_token_title_default(self):
        """
        Test that lower case words are not tokenized
        """
        self.assertEqual(self.post.token_title(), ["Sheffield"])

    def test_token_title_lower(self):
        """
        Test that lower case words are not tokenized
        """
        self.post.title = "london sheffield exeter leeds"
        self.assertEqual(self.post.token_title(), [])

    def test_token_title_upper(self):
        """
        Test that upper case words are tokenized
        """
        self.post.title = "London Sheffield Exeter Leeds"
        self.assertEqual(self.post.token_title(), ["London", "Sheffield", "Exeter", "Leeds"])

    def test_token_title_upper_stop_words(self):
        """
        Test that stop words are not tokenized
        """
        self.post.title = "This Is A String"
        self.assertEqual(self.post.token_title(), ["String"])

    def test_token_title_oc(self):
        """
        Test that OC is not tokenized
        """
        self.post.title = "London Sheffield Exeter Leeds OC"
        self.assertEqual(self.post.token_title(), ["London", "Sheffield", "Exeter", "Leeds"])

    def test_token_title_distance(self):
        """
        Test that Distance not tokenized
        """
        self.post.title = "London Sheffield Exeter Leeds Distance"
        self.assertEqual(self.post.token_title(), ["London", "Sheffield", "Exeter", "Leeds"])

    def test_token_title_lower_punctuation(self):
        """
        Test that punctuation does not affect result
        """
        self.post.title = "£london *sheffield exeter? leeds!"
        self.assertEqual(self.post.token_title(), [])

    def test_token_title_upper_punctuation(self):
        """
        Test that punctuation does not affect result
        """
        self.post.title = "£London *Sheffield Exeter? Leeds!"
        self.assertEqual(self.post.token_title(), ["Exeter", "Leeds"])

    def test_token_title_upper_stop_words_punctuation(self):
        """
        Test that punctuation does not affect result
        """
        self.post.title = "This Is A A! String! ?! £$£$ ^^&"
        self.assertEqual(self.post.token_title(), ["String"])

    def test_token_title_numbers(self):
        """
        Test that numbers are not tokenized
        """
        self.post.title = "11 {22} [33] <44> ~55~ [6*7] 9cc 0leeds 10Exeter"
        self.assertEqual(self.post.token_title(), [])

    def test_thumb_url_no_image(self):
        """
        Test that url remains the same if imgur not in url
        """
        self.post.url = "https://www.mapforreddit.xyz/home/index.html"
        self.assertEqual(self.post.thumb_url(), self.post.url)

    def test_thumb_url_image_jpg(self):
        """
        Test that url remains the same if imgur not in url
        """
        self.post.url = "https://www.mapforreddit.xyz/home/pic.jpg"
        self.assertEqual(self.post.thumb_url(), self.post.url)

    def test_thumb_url_image_png(self):
        """
        Test that url remains the same if imgur not in url
        """
        self.post.url = "https://www.mapforreddit.xyz/home/pic.png"
        self.assertEqual(self.post.thumb_url(), self.post.url)

    def test_thumb_url_imgur_image_jpg(self):
        """
        Test that thumbnail image is selected if imgur in url
        """
        self.post.url = "https://www.imgur.xyz/home/pic.jpg"
        self.assertEqual(self.post.thumb_url(), "https://www.imgur.xyz/home/picm.jpg")

    def test_thumb_url_imgur_image_png(self):
        """
        Test that thumbnail image is selected if imgur in url
        """
        self.post.url = "https://www.imgur.xyz/home/pic.png"
        self.assertEqual(self.post.thumb_url(), "https://www.imgur.xyz/home/picm.png")

    def test_thumb_url_imgur_random_ext(self):
        """
        Test that a random extension is not converted
        """
        self.post.url = "https://www.imgur.xyz/home/pic.mp3"
        self.assertEqual(self.post.thumb_url(), self.post.url)

    def test_thumb_url_random(self):
        """
        Test that randpm characters are not converted
        """
        self.post.url = "dfg fdgdfg  fdg sdfg 3241324134 456 xdbvxcvb"
        self.assertEqual(self.post.thumb_url(), self.post.url)

    def test_updated_score_working_id(self):
        """
        Test that a working id works
        """
        self.post.id = "8405kv"
        self.assertIsInstance(self.post.updated_score(), int)

    def test_updated_score_broken_id(self):
        """
        Test that a broken id dosent break it
        """
        self.post.id = "dQw4w9WgXcQ"
        self.assertEqual(self.post.updated_score(), self.post.score)