Example #1
0
    def test_failed_search(self):
        node = Soupy('<a><b>1</b></a><a>2</a>')

        with pytest.raises(NullValueError):
            node.find_all('a').dump(
                a=Q.find('b').text
            )
Example #2
0
    def test_simple_dump(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(a=Q.text).val()
        assert result == [{'a': '1'}, {'a': '2'}, {'a': '3'}]

        result = node.find_all('a').dump(Q.text).val()
        assert result == [('1',), ('2',), ('3',)]

        with pytest.raises(ValueError):
            node.find('a').dump(Q.text, a=Q.text)
Example #3
0
    def test_simple_dump(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(a=Q.text).val()
        assert result == [{'a': '1'}, {'a': '2'}, {'a': '3'}]

        result = node.find_all('a').dump(Q.text).val()
        assert result == [('1', ), ('2', ), ('3', )]

        with pytest.raises(ValueError):
            node.find('a').dump(Q.text, a=Q.text)
Example #4
0
    def test_orelse(self):
        node = Soupy('<a><b>1</b></a><a>2</a>')

        result = node.find_all('a').dump(
            a=Q.find('b').text.map(int).orelse(0)).val()

        assert result == [{'a': 1}, {'a': 0}]
Example #5
0
    def test_orelse(self):
        node = Soupy('<a><b>1</b></a><a>2</a>')

        result = node.find_all('a').dump(
            a=Q.find('b').text.map(int).orelse(0)
        ).val()

        assert result == [{'a': 1}, {'a': 0}]
Example #6
0
    def test_multi_dump(self):
        node = Soupy('<a val="1">1</a><a>2</a><a val="3">3</a>')

        result = node.find_all('a').dump(
            a=Q.text,
            b=Q.attrs.get('val')).val()
        assert result == [{'a': '1', 'b': '1'},
                          {'a': '2', 'b': None},
                          {'a': '3', 'b': '3'}]
Example #7
0
    def test_multi_dump(self):
        node = Soupy('<a val="1">1</a><a>2</a><a val="3">3</a>')

        result = node.find_all('a').dump(a=Q.text, b=Q.attrs.get('val')).val()
        assert result == [{
            'a': '1',
            'b': '1'
        }, {
            'a': '2',
            'b': None
        }, {
            'a': '3',
            'b': '3'
        }]
 def get_chart(self, chart_name):
     self.chart_list = []
     self.chart_name = chart_name.lower()
     global KeyError
     try:
         number = self.chart_titles_dict[self.chart_name]
     except KeyError:
         #return json.dumps(["That chart does not exist"], indent = 2)
         raise ValueError('That chart does not exist')
     self.url = self.base_url + str(number)
     raw = requests.get(self.url)
     soup = Soupy(raw.text)
     tr_container = soup.find_all('tr', {'class': 'latc_song'})
     global NameError
     pos = 0
     song_title_constant = 2
     song_artist_constant = 3
     for table_row in tr_container:
         children = table_row.children
         null_container_holder = type(
             children[0].find('table').find_all('a'))
         for child in children:
             links = child.find('table').find_all('a')
             if type(links) is not null_container_holder:
                 try:
                     try:
                         pos = pos + 1
                         song_title = links[
                             song_title_constant].contents.first().val(
                             ).string
                         song_artist = links[
                             song_artist_constant].contents.first().val(
                             ).string
                         self.chart_list.append(
                             (('position', pos), ('title', song_title),
                              ('artist', song_artist)))
                     except NullValueError, NameError:
                         print('\n')
                 except NameError:
                     song_title = links[song_title_constant -
                                        1].contents.first().val().string
                     song_artist = links[song_artist_constant -
                                         1].contents.first().val().string
                     self.chart_list.append(
                         (('position', pos), ('title', song_title),
                          ('artist', song_artist)))
     return json.dumps(self.chart_list, indent=3)
 def get_chart(self, chart_name):
     self.chart_list = []
     self.chart_name = chart_name.lower()
     global KeyError
     try:
         number = self.chart_titles_dict[self.chart_name]
     except KeyError:
         #return json.dumps(["That chart does not exist"], indent = 2)
         raise ValueError('That chart does not exist')
     self.url = self.base_url + str(number)
     raw = requests.get(self.url)
     soup = Soupy(raw.text)
     tr_container = soup.find_all('tr',{'class':'latc_song'})
     global NameError
     pos = 0
     song_title_constant = 2
     song_artist_constant = 3
     for table_row in tr_container:
         children = table_row.children
         null_container_holder = type(children[0].find('table').find_all('a'))
         for child in children:
             links = child.find('table').find_all('a')
             if type(links) is not null_container_holder:
                 try:
                     try:
                         pos = pos + 1
                         song_title = links[song_title_constant].contents.first().val().string
                         song_artist = links[song_artist_constant].contents.first().val().string
                         self.chart_list.append((('position',pos),('title',song_title), ('artist',song_artist)))
                     except NullValueError, NameError:
                         print ('\n')
                 except NameError:
                     song_title = links[song_title_constant-1].contents.first().val().string
                     song_artist = links[song_artist_constant-1].contents.first().val().string
                     self.chart_list.append((('position',pos),('title',song_title), ('artist',song_artist)))
     return json.dumps(self.chart_list, indent = 3)
Example #10
0
def return_all_links(url):    
    soup = Soupy(download(url))
    return [tag for tag in soup.find_all('a') if is_tag_not_anchor(tag)]
Example #11
0
    def test_dump_with_map(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(
            a=Q.text.map(int)).val()
        assert result == [{'a': 1}, {'a': 2}, {'a': 3}]
Example #12
0
    def test_dump_with_multi_map(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(
            a=Q.text.map(int).map(lambda x: x * 2)).val()
        assert result == [{'a': 2}, {'a': 4}, {'a': 6}]
Example #13
0
    def test_dump_with_method(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(
            a=Q.find('b').orelse('')).val()
        assert result == [{'a': ''}, {'a': ''}, {'a': ''}]
Example #14
0
    def test_dump_with_getitem(self):
        node = Soupy('<a val="1">1</a>')

        result = node.find_all('a').dump(
            a=Q.attrs["val"]).val()
        assert result == [{'a': "1"}]
Example #15
0
    def test_simple_dump(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(a=Q.text).val()
        assert result == [{'a': '1'}, {'a': '2'}, {'a': '3'}]
Example #16
0
    def test_simple_dump(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(
            a=Q.text).val()
        assert result == [{'a': '1'}, {'a': '2'}, {'a': '3'}]
Example #17
0
    def test_failed_search(self):
        node = Soupy('<a><b>1</b></a><a>2</a>')

        with pytest.raises(NullValueError):
            node.find_all('a').dump(a=Q.find('b').text)
Example #18
0
    def test_dump_with_method(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(a=Q.find('b').orelse('')).val()
        assert result == [{'a': ''}, {'a': ''}, {'a': ''}]
Example #19
0
    def test_dump_with_multi_map(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(
            a=Q.text.map(int).map(lambda x: x * 2)).val()
        assert result == [{'a': 2}, {'a': 4}, {'a': 6}]
Example #20
0
    def test_dump_with_map(self):
        node = Soupy('<a>1</a><a>2</a><a>3</a>')

        result = node.find_all('a').dump(a=Q.text.map(int)).val()
        assert result == [{'a': 1}, {'a': 2}, {'a': 3}]
Example #21
0
    def test_dump_with_getitem(self):
        node = Soupy('<a val="1">1</a>')

        result = node.find_all('a').dump(a=Q.attrs["val"]).val()
        assert result == [{'a': "1"}]