Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
fix: remove keyboard interupt
Browse files Browse the repository at this point in the history
* This cause nohup to stop process on some servers
  • Loading branch information
vlourme committed May 19, 2023
1 parent 91d6596 commit bc5376c
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,39 @@

dotenv.load_dotenv()

bot = lightbulb.BotApp(token=os.getenv('TOKEN'))
db = dataset.connect('sqlite:///data.db')
table = db['subscriptions']
bot = lightbulb.BotApp(token=os.getenv("TOKEN"))
db = dataset.connect("sqlite:///data.db")
table = db["subscriptions"]


async def run_background() -> None:
log.info("Scraper started.")

while True:
log.info('Executing scraping loop')
for sub in db['subscriptions']:
log.info("Executing scraping loop")
for sub in db["subscriptions"]:
print(sub)
items = scrape(db, sub)
log.debug("{items} found for {id}",
items=len(items), id=str(sub['id']))
log.debug("{items} found for {id}", items=len(items), id=str(sub["id"]))
for item in items:
embed = generate_embed(item, sub['id'])
embed = generate_embed(item, sub["id"])

await bot.rest.create_message(sub['channel_id'], embed=embed)
await bot.rest.create_message(sub["channel_id"], embed=embed)

if len(items) > 0:
# Update table by using last in date item timestamp
table.update({
'id': sub['id'],
'last_sync': int(items[0]['photo']['high_resolution']['timestamp'])
}, ['id'])
table.update(
{
"id": sub["id"],
"last_sync": int(
items[0]["photo"]["high_resolution"]["timestamp"]
),
},
["id"],
)

log.info('Sleeping for {interval} seconds', interval=os.getenv('INTERVAL', 60))
await asyncio.sleep(int(os.getenv('INTERVAL', 60)))
log.info("Sleeping for {interval} seconds", interval=os.getenv("INTERVAL", 60))
await asyncio.sleep(int(os.getenv("INTERVAL", 60)))


@bot.listen(hikari.ShardReadyEvent)
Expand All @@ -49,53 +53,50 @@ async def ready_listener(_):


@bot.command()
@lightbulb.option('url', 'URL to vinted search', type=str, required=True)
@lightbulb.option('channel', 'Channel to receive alerts', type=hikari.TextableChannel, required=True)
@lightbulb.command('subscribe', 'Subscribe to a Vinted search')
@lightbulb.option("url", "URL to vinted search", type=str, required=True)
@lightbulb.option(
"channel", "Channel to receive alerts", type=hikari.TextableChannel, required=True
)
@lightbulb.command("subscribe", "Subscribe to a Vinted search")
@lightbulb.implements(lightbulb.SlashCommand)
async def subscribe(ctx: lightbulb.Context) -> None:
table.insert({
'url': ctx.options.url,
'channel_id': ctx.options.channel.id,
'last_sync': -1
})
table.insert(
{"url": ctx.options.url, "channel_id": ctx.options.channel.id, "last_sync": -1}
)
log.info("Subscription created for {url}", url=ctx.options.url)
await ctx.respond('✅ Created subscription')
await ctx.respond("✅ Created subscription")


@bot.command()
@lightbulb.command('subscriptions', 'Get a list of subscription')
@lightbulb.command("subscriptions", "Get a list of subscription")
@lightbulb.implements(lightbulb.SlashCommand)
async def subscriptions(ctx: lightbulb.Context) -> None:
embed = hikari.Embed(title='Subscriptions')
embed = hikari.Embed(title="Subscriptions")

for sub in table:
embed.add_field(
name='#' + str(sub['id']),
value=sub['url']
)
embed.add_field(name="#" + str(sub["id"]), value=sub["url"])

await ctx.respond(embed)


@bot.command()
@lightbulb.option('id', 'ID of the subscription', type=int, required=True)
@lightbulb.command('unsubscribe', 'Stop following a subscription')
@lightbulb.option("id", "ID of the subscription", type=int, required=True)
@lightbulb.command("unsubscribe", "Stop following a subscription")
@lightbulb.implements(lightbulb.SlashCommand)
async def unsubscribe(ctx: lightbulb.Context) -> None:
table.delete(id=ctx.options.id)
log.info("Deleted subscription #{id}", id=str(ctx.options.id))
await ctx.respond(f'🗑 Deleted subscription #{str(ctx.options.id)}.')
await ctx.respond(f"🗑 Deleted subscription #{str(ctx.options.id)}.")


if __name__ == "__main__":
if os.name != "nt":
import uvloop

uvloop.install()

try:
bot.run(activity=hikari.Activity(
name='Vinted articles!',
type=hikari.ActivityType.WATCHING))
except KeyboardInterrupt:
bot.close()
bot.run(
activity=hikari.Activity(
name="Vinted articles!", type=hikari.ActivityType.WATCHING
)
)

0 comments on commit bc5376c

Please sign in to comment.