Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
1ntegrale9 committed Feb 23, 2023
1 parent ecbcfd7 commit 14067c4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 83 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/flake8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ name: Check Syntax
on: [push]

jobs:
build:
flake8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
run: python -m pip install discord.py[voice] flake8
- name: Run flake8
run: flake8 --ignore=E302,E501
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: python -m pip install discord.py python-dotenv flake8
- name: Run flake8
run: flake8 --ignore=E261,E302,E305,E501
24 changes: 24 additions & 0 deletions constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@
TOKEN = getenv('DISCORD_BOT_TOKEN')
API_URL = getenv('API_URL')
GAS_URL = getenv('GAS_URL')
GUILD_LINKS_ID = int(getenv('GUILD_LINKS_ID', 870910390706532382))
LOG_CHANNEL_ID = int(getenv('LOG_CHANNEL_ID', 1068570419528990821))

CHANNEL_TAG_TABLE: dict = {
'872967889928486912': '人物',
'872894495430164610': 'イラスト',
'872841453636841524': '音楽',
'872940515547578378': '動画',
}

DOMAINS_TABLE: dict = {
'discord.com': 'discord',
'ptb.discord.com': 'discord',
'canary.discord.com': 'discord',
'twitter.com': 'twitter',
'open.spotify.com': 'spotify',
'youtube.com': 'youtube',
'www.youtube.com': 'youtube',
'youtu.be': 'youtube',
'pixiv.net': 'pixiv',
'www.pixiv.net': 'pixiv',
'soundcloud.com': 'soundcloud',
'nicovideo.jp': 'nicovideo',
'www.nicovideo.jp': 'nicovideo',
}
3 changes: 3 additions & 0 deletions extensions/gkb.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async def global_dictionary(self, interaction: discord.Interaction):
domain='',
domain_id='',
user_id=f'discord_user_{interaction.user.id}',
ephemeral=False,
)
await interaction.response.send_modal(modal)

Expand All @@ -95,6 +96,7 @@ async def guild_dictionary(self, interaction: discord.Interaction):
domain='discord',
domain_id=f'discord_guild_{interaction.guild.id}',
user_id=f'discord_user_{interaction.user.id}',
ephemeral=False,
)
await interaction.response.send_modal(modal)

Expand All @@ -104,6 +106,7 @@ async def private_dictionary(self, interaction: discord.Interaction):
domain='discord',
domain_id=f'discord_user_{interaction.user.id}',
user_id=f'discord_user_{interaction.user.id}',
ephemeral=False,
)
await interaction.response.send_modal(modal)

Expand Down
33 changes: 12 additions & 21 deletions extensions/url_handle.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from discord import Message
from discord.ext import commands
from utils import Text2URL
from utils import get_urls_from_text
from utils import save_url
from utils import hashing
from utils.dpyexcept import excepter

GUILD_LINKS_ID = 870910390706532382
from constant import GUILD_LINKS_ID
from constant import CHANNEL_TAG_TABLE

class UrlHandleCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.channels = {
'872967889928486912': '人物',
'872894495430164610': 'イラスト',
'872841453636841524': '音楽',
'872940515547578378': '動画',
}

@commands.Cog.listener()
@excepter
Expand All @@ -25,21 +19,18 @@ async def on_message(self, message: Message):
if message.guild.id != GUILD_LINKS_ID:
return
provider = hashing(str(message.author.id))
text2url = Text2URL(message.content)
if len(text2url.urls) == 0:
urls = get_urls_from_text(message.content)
if len(urls) == 0:
return
save_count = 0
for url, data in text2url.urls.items():
channel_tag = CHANNEL_TAG_TABLE.get(str(message.channel.id))
is_saved = False
for url, data in urls.items():
if data.get('site') == 'discord':
continue
await save_url(
provider,
url,
data,
{data.get('site'), self.channels.get(str(message.channel.id))} - {None},
)
save_count += 1
if save_count > 0:
tags = {data.get('site'), channel_tag} - {None}
await save_url(provider, url, data, tags)
is_saved = True
if is_saved:
await message.add_reaction('💾')

async def setup(bot: commands.Bot) -> None:
Expand Down
57 changes: 20 additions & 37 deletions utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,42 @@
import re
from hashlib import sha256
from utils.gas_client import GoogleAppScriptClient
from utils import gas_client
from constant import GAS_URL
from constant import DOMAINS_TABLE

REGEXP_URL = r'https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+'
REGEXP_URL_TWITTER = r'(https?:\/\/twitter\.com\/)([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)'
PATTERN_TWITTER = re.compile(REGEXP_URL_TWITTER)

class Text2URL():
def __init__(self, text):
self.text: str = text
self.pattern = r'https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+'
self.domains: dict = {
'discord.com': 'discord',
'ptb.discord.com': 'discord',
'canary.discord.com': 'discord',
'twitter.com': 'twitter',
'open.spotify.com': 'spotify',
'youtube.com': 'youtube',
'www.youtube.com': 'youtube',
'youtu.be': 'youtube',
'pixiv.net': 'pixiv',
'www.pixiv.net': 'pixiv',
'soundcloud.com': 'soundcloud',
'nicovideo.jp': 'nicovideo',
'www.nicovideo.jp': 'nicovideo',
def get_urls_from_text(text: str):
urls: dict = {}
for url in re.findall(REGEXP_URL, text):
url = url.replace('http://', 'https://')
domain = url.split('://')[1].split('/')[0]
site = DOMAINS_TABLE.get(domain, 'other')
urls[url] = {
'domain': domain,
'site': site,
}
self.urls: dict = {}
for url in re.findall(self.pattern, self.text):
url = url.replace('http://', 'https://')
domain = url.split('://')[1].split('/')[0]
site = self.domains.get(domain, 'other')
self.urls[url] = {
'domain': domain,
'site': site,
}
if site == 'twitter':
pattern = re.compile(
r'(https?:\/\/twitter\.com\/)([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)'
)
self.urls[url] |= {'twitter_id': pattern.match(url).group(2).split('/')[0]}

if site == 'twitter':
urls[url] |= {'twitter_id': PATTERN_TWITTER.match(url).group(2).split('/')[0]}
return urls

async def save_url(provider, url, data, tags):
payload = {
'provider': provider,
'url': url,
'tags': ', '.join(tags),
}
await GoogleAppScriptClient('all').post(payload)
await gas_client.post(GAS_URL, 'all', payload)
for tag in tags:
payload = {
'url': url,
'tags': ', '.join(tags),
}
if tag == 'twitter':
payload |= {'twitter_id': data.get('twitter_id')}
await GoogleAppScriptClient(tag).post(payload)

await gas_client.post(GAS_URL, tag, payload)

def hashing(text: str):
return sha256(text.encode()).hexdigest()
24 changes: 9 additions & 15 deletions utils/gas_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import aiohttp
import os

async def get(url, sheet_name) -> dict:
async with aiohttp.ClientSession() as session:
async with session.get(url, params={'sheet', sheet_name}) as resp:
return await resp.json()

class GoogleAppScriptClient():
def __init__(self):
self.url = os.getenv('GAS_URL')

async def get(self, sheet_name) -> dict:
async with aiohttp.ClientSession() as session:
async with session.get(self.url, params={'sheet', sheet_name}) as resp:
return await resp.json()

async def post(self, sheet_name, data: dict) -> int:
payload = {'sheet': sheet_name} | data
async with aiohttp.ClientSession() as session:
async with session.post(self.url, json=payload) as resp:
return resp.status
async def post(url, sheet_name, data: dict) -> int:
payload = {'sheet': sheet_name} | data
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload) as resp:
return resp.status

0 comments on commit 14067c4

Please sign in to comment.