예제 #1
0
파일: isitup.py 프로젝트: Twixes/somsiad
 async def isitup(self, ctx, *, query='https://google.com'):
     """Returns information about website status."""
     async with ctx.typing():
         protocol, rest = first_url(query, protocol_separate=True)
         url_valid = rest is not None and protocol in (None, 'http', 'https')
         status = None
         if url_valid:
             protocol = protocol or 'https'
             url = f'{protocol}://{rest}'
             try:
                 for method in (self.bot.session.head, self.bot.session.get):
                     async with method(url, allow_redirects=True) as request:
                         if request.status == 405 and request.method != 'get':
                             continue
                         status = request.status
             except aiohttp.InvalidURL:
                 url_valid = False
             except aiohttp.ClientConnectorError:
                 pass
         if url_valid:
             if status is not None and status // 100 == 2:
                 emoji, notice = '✅', f'Strona {rest} jest dostępna'
             else:
                 emoji, notice = '🔴', f'Strona {rest} nie jest dostępna'
             if status is not None:
                 status_presentation = md_link(status, self.HTTP_CODE_WIKIPEDIA_URLS[status // 100])
             else:
                 status_presentation = 'brak odpowiedzi'
             embed = self.bot.generate_embed(emoji, notice, url=url)
             embed.add_field(name='Status', value=status_presentation)
         else:
             embed = self.bot.generate_embed('⚠️', 'Podany adres jest niepoprawny')
         await self.bot.send(ctx, embed=embed)
예제 #2
0
 async def _archive_message(self, bot: commands.Bot,
                            archive_channel: discord.TextChannel,
                            message: discord.Message):
     pin_embed = bot.generate_embed(description=message.content,
                                    timestamp=message.created_at)
     pin_embed.set_author(name=message.author.display_name,
                          url=message.jump_url,
                          icon_url=message.author.avatar_url)
     pin_embed.set_footer(text=f'#{message.channel}')
     files = []
     for attachment in message.attachments:
         filename = attachment.filename
         fp = io.BytesIO()
         await attachment.save(fp)
         file = discord.File(fp, filename)
         files.append(file)
     if len(files) == 1:
         if message.attachments[0].height is not None:
             pin_embed.set_image(
                 url=f'attachment://{message.attachments[0].filename}')
         await archive_channel.send(embed=pin_embed, file=files[0])
     elif len(files) > 1:
         await archive_channel.send(embed=pin_embed, files=files)
     else:
         url_from_content = first_url(message.content)
         if url_from_content is not None:
             pin_embed.set_image(url=url_from_content)
         await archive_channel.send(embed=pin_embed)
예제 #3
0
 def test_improper_unspaced_sentence_after(self):
     search_result = first_url(
         'ThissentencecontainsanexemplaryURL:https://www.example.com.AndthissentencecontainsnoexemplaryURL.'
     )
     self.assertEqual(search_result, 'https://www.example.com.AndthissentencecontainsnoexemplaryURL')
예제 #4
0
 def test_proper_two_urls(self):
     search_result = first_url(
         'This sentence contains exemplary URLs: https://one.example.com and https://two.example.com.'
     )
     self.assertEqual(search_result, 'https://one.example.com')
예제 #5
0
 def test_proper_colon_after(self):
     search_result = first_url('https://www.example.com: An Exemplary URL')
     self.assertEqual(search_result, 'https://www.example.com')
예제 #6
0
 def test_proper_semicolon_continuation(self):
     search_result = first_url(
         'This sentence contains an exemplary URL: https://www.example.com; and some rambling.'
     )
     self.assertEqual(search_result, 'https://www.example.com')
예제 #7
0
 def test_proper_markdown_formatted(self):
     search_result = first_url('This sentence contains [an exemplary URL](https://www.example.com).')
     self.assertEqual(search_result, 'https://www.example.com')
예제 #8
0
 def test_proper_markdown(self):
     search_result = first_url('This sentence contains an exemplary URL: <https://www.example.com>.')
     self.assertEqual(search_result, 'https://www.example.com')
예제 #9
0
 def test_proper_comma_after(self):
     search_result = first_url(
         'This sentence contains an exemplary URL: https://www.example.com, and some rambling.'
     )
     self.assertEqual(search_result, 'https://www.example.com')
예제 #10
0
 def test_proper_path(self):
     search_result = first_url('This sentence contains an exemplary URL: https://www.example.com/resource/id.')
     self.assertEqual(search_result, 'https://www.example.com/resource/id')
예제 #11
0
 def test_proper_trailing_slash(self):
     search_result = first_url('This sentence contains an exemplary URL: https://www.example.com/.')
     self.assertEqual(search_result, 'https://www.example.com/')
예제 #12
0
 def test_proper_no_subdomain(self):
     search_result = first_url('This sentence contains an exemplary URL: https://example.com.')
     self.assertEqual(search_result, 'https://example.com')
예제 #13
0
 def test_improper_no_url(self):
     search_result = first_url('This sentence contains no exemplary URL.')
     self.assertIsNone(search_result)
예제 #14
0
 def test_improper_no_tld(self):
     search_result = first_url('This sentence contains an exemplary URL: https://example.')
     self.assertIsNone(search_result)
예제 #15
0
 def tesy_improper_no_protocol(self):
     search_result = first_url('This sentence contains an exemplary URL: www.example.com.')
     self.assertIsNone(search_result)
예제 #16
0
 def test_improper_ftp(self):
     search_result = first_url('This sentence contains an exemplary URL: ftp://www.example.com.')
     self.assertIsNone(search_result)