generated from DiscordBotPortalJP/discordpy-startup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecbcfd7
commit 14067c4
Showing
6 changed files
with
78 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |