Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated & Optimized #70

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/auto_assign.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Set to true to add reviewers to pull requests
addReviewers: true

# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
- edoardottt

# A list of keywords to be skipped the process that add reviewers if pull requests include it
skipKeywords:
- wip

# A number of reviewers added to the pull request
# Set 0 to add all the reviewers (default: 0)
numberOfReviewers: 0
34 changes: 11 additions & 23 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def conn_db():
logger = logging.getLogger("__main__")
logger.error("You must execute: python init_db.py")
sys.exit()
conn = sqlite3.connect(db_filename)
return conn
return sqlite3.connect(db_filename)


def create_stat(conn, data):
Expand Down Expand Up @@ -72,15 +71,8 @@ def today_stats(conn, username):
"""
sql = """ SELECT * FROM statistics WHERE username = ? AND date = ? """
cur = conn.cursor()
cur.execute(
sql,
(
username,
datetime.datetime.today().strftime("%Y-%m-%d"),
),
)
values = cur.fetchone()
return values
cur.execute(sql, (username, datetime.datetime.now().strftime("%Y-%m-%d")))
return cur.fetchone()


def user_stats(conn, username):
Expand All @@ -90,8 +82,7 @@ def user_stats(conn, username):
sql = """ SELECT * FROM statistics WHERE username = ? """
cur = conn.cursor()
cur.execute(sql, (username,))
values = cur.fetchall()
return values
return cur.fetchall()


def user_date_stats(conn, username, date):
Expand All @@ -107,8 +98,7 @@ def user_date_stats(conn, username, date):
date,
),
)
values = cur.fetchall()
return values
return cur.fetchall()


def all_stats(conn):
Expand All @@ -118,8 +108,7 @@ def all_stats(conn):
sql = """ SELECT * FROM statistics """
cur = conn.cursor()
cur.execute(sql)
values = cur.fetchall()
return values
return cur.fetchall()


def month_stats(conn, username):
Expand All @@ -130,9 +119,9 @@ def month_stats(conn, username):
sql = """ SELECT sum(tweets), sum(likes), sum(retweets) FROM statistics \
WHERE username = ? AND date >= ? AND date <= ? GROUP BY username """
cur = conn.cursor()
current_year = datetime.datetime.today().strftime("%Y")
current_month = datetime.datetime.today().strftime("%m")
starting_day = datetime.datetime.today().replace(day=1).strftime("%Y-%m-%d")
current_year = datetime.datetime.now().strftime("%Y")
current_month = datetime.datetime.now().strftime("%m")
starting_day = datetime.datetime.now().replace(day=1).strftime("%Y-%m-%d")
february_days = 28
if (
int(current_year) % 4 == 0
Expand All @@ -155,7 +144,7 @@ def month_stats(conn, username):
12: 31,
}
ending_day = (
datetime.datetime.today()
datetime.datetime.now()
.replace(day=months[int(current_month)])
.strftime("%Y-%m-%d")
)
Expand All @@ -178,5 +167,4 @@ def all_users(conn):
sql = """ SELECT DISTINCT username FROM statistics"""
cur = conn.cursor()
cur.execute(sql)
values = cur.fetchall()
return values
return cur.fetchall()
5 changes: 1 addition & 4 deletions errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def error_handler(e):

logger = logging.getLogger("__main__")
if e.__class__ == twitter.api.TwitterHTTPError:
logger.error(str(e.e) + " on " + e.uri)
logger.error(f"{str(e.e)} on {e.uri}")

# == 429 TOO MANY REQUESTS -> Sleep for one hour
if str(e.e.code) == "429":
Expand All @@ -46,9 +46,6 @@ def error_handler(e):
logger.info("Sleeping for five minutes.")
time.sleep(5 * 60)

# for other types of issues with specific behaviour
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a specific reason to delete this comment?

# add here an additional handler
# == DEFAULT ==
else:
logger.info("Sleeping for ten seconds.")
time.sleep(10)
4 changes: 1 addition & 3 deletions input.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ def get_args():
help="Produce a html file containing the stats for the inputted used (ALL for anyone).",
)

args = parser.parse_args()

return args
return parser.parse_args()


def read_secrets():
Expand Down
57 changes: 21 additions & 36 deletions output.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def tweet_banner(message):
This is a standard tweet to spread info about the bot.
"""
tweet = str(datetime.datetime.now()) + "\n"
tweet += "This is a bot at the service of @" + globals.user + ".\n"
tweet += f"This is a bot at the service of @{globals.user}" + ".\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the final ".\n" is not included in the string

tweet += "https://github.com/edoardottt/twitterbot2\n"
tweet += message + "\n"
return tweet
Expand All @@ -81,15 +81,13 @@ def create_output_file(filename):
output file in the `twitterbot2-output` folder.
"""
directory = "twitterbot2-output"
if not os.path.exists(directory + "/" + filename):
_ = open(directory + "/" + filename, "w+")
if not os.path.exists(f"{directory}/{filename}"):
_ = open(f"{directory}/{filename}", "w+")
elif answer := ask_confirmation():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of assigning answer here?

_ = open(f"{directory}/{filename}", "w+")
else:
answer = ask_confirmation()
if not answer:
sys.exit()
else:
_ = open(directory + "/" + filename, "w+")
return directory + "/" + filename
sys.exit()
return f"{directory}/{filename}"


def ask_confirmation():
Expand All @@ -98,9 +96,7 @@ def ask_confirmation():
existing output file.
"""
answer = str(input("The file already exists. Do you want to override? (Y/n)"))
if answer.lower() == "y" or answer.lower() == "yes" or answer.lower() == "":
return True
return False
return answer.lower() in {"y", "yes", ""}


def output_csv(user):
Expand All @@ -110,21 +106,18 @@ def output_csv(user):
(if ALL is inputted).
"""
conn = db.conn_db()
if user == "ALL":
values = db.all_stats(conn)
else:
values = db.user_stats(conn, user)
values = db.all_stats(conn) if user == "ALL" else db.user_stats(conn, user)
if len(values) == 0:
logger.warning("There aren't data for this user.")
sys.exit()
else:
create_output_folder()
filename = create_output_file(user + ".csv")
filename = create_output_file(f"{user}.csv")
with open(filename, "w", newline="") as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for elem in values:
wr.writerow(elem)
logger.info("All data has been written into " + filename)
logger.info(f"All data has been written into {filename}")


def output_json(user):
Expand All @@ -134,20 +127,17 @@ def output_json(user):
(if ALL is inputted).
"""
conn = db.conn_db()
if user == "ALL":
values = db.all_stats(conn)
else:
values = db.user_stats(conn, user)
values = db.all_stats(conn) if user == "ALL" else db.user_stats(conn, user)
if len(values) == 0:
logger.warning("There aren't data for this user.")
sys.exit()
else:
create_output_folder()
filename = create_output_file(user + ".json")
filename = create_output_file(f"{user}.json")

dict = {}
for elem in values:
if not elem[0] in dict.keys():
if elem[0] not in dict:
dict[elem[0]] = {}
dict[elem[0]][elem[1]] = {
"tweets": elem[2],
Expand All @@ -157,11 +147,11 @@ def output_json(user):
}
with open(filename, "w") as f:
json.dump(dict, f)
logger.info("All data has been written into " + filename)
logger.info(f"All data has been written into {filename}")


def banner_html():
banner = """<html>
return """<html>
<head>
<title>Twitterbot2 output</title>
<style>
Expand Down Expand Up @@ -223,18 +213,16 @@ def banner_html():
<a href="https://github.com/edoardottt/twitterbot2/blob/main/LICENSE">License</a>
</div>
"""
return banner


def footer_html():
footer = """<div class="footer">
return """<div class="footer">
<p>twitterbot2 by <a href='https://github.com/edoardottt'>@edoardottt</a></p>
</div>
<br><br><br><br><br><br><br><br>
</body>
</html>
"""
return footer


def html_table(lol):
Expand All @@ -259,21 +247,18 @@ def output_html(user):
(if ALL is inputted).
"""
conn = db.conn_db()
if user == "ALL":
values = db.all_stats(conn)
else:
values = db.user_stats(conn, user)
values = db.all_stats(conn) if user == "ALL" else db.user_stats(conn, user)
if len(values) == 0:
logger.warning("There aren't data for this user.")
sys.exit()
else:
create_output_folder()
filename = create_output_file(user + ".html")
filename = create_output_file(f"{user}.html")
with open(filename, "w") as f:
f.write(banner_html())
f.write(html_table(values))
f.write(footer_html())
logger.info("All data has been written into " + filename)
logger.info(f"All data has been written into {filename}")


def data_json(values):
Expand All @@ -283,7 +268,7 @@ def data_json(values):
"""
dict = {}
for elem in values:
if not elem[0] in dict.keys():
if elem[0] not in dict:
dict[elem[0]] = {}
dict[elem[0]][elem[1]] = {
"tweets": elem[2],
Expand Down
Loading
Loading