def inSertSql(conn, tb, id, title, today): sql = "INSERT INTO " + str(tb) + " VALUES (%s,%s,%s)" try: cur = conn.cursor() cur.execute(sql, (id, title, today)) conn.commit() cur.close() conn.close() log('insert Mysql Success!').Loginfo() except Exception as e: log(e).Logerror()
def getToday(apiUrl, head): try: data = requests.get(apiUrl, headers=head) data = json.loads(data.text) with open('Apptmp/daily.log', 'w+') as f: f.write(str(data['stories'][-1])) log('get daily detail Success!').Loginfo() UpdateTodayStatus('True') except Exception as e: UpdateTodayStatus('False') log(e).Logerror()
def getConn(): try: conn = pymysql.connect(host=getSqlAddr(), port=int(getSqlPort()), user=getSqlUser(), passwd=getSqlPass(), db=getDb(), use_unicode=True, charset='utf8') return conn except Exception as e: log("数据库连接错误!" + str(e)).Logerror() return -1
def run(): head = {} head['User-Agent'] = getHead() today = date.today() getToday(getLastApi(), head) with open('Apptmp/daily.log', 'r') as f: data = eval(f.readlines()[0]) title = data['title'] id = data['id'] # TODO: 尝试失败重连 conn = getConn() if (conn == -1): log('failed').Logerror() exit(-1) else: inSertSql(conn, getTb(), id, title, today)
def inSqldata(mailaccount): conn = getConn() cur = conn.cursor() sql = "INSERT INTO subscriber VALUES (%s)" try: cur.execute(sql, mailaccount) log("Insert Success " + mailaccount).Loginfo() conn.commit() return True except Exception as e: log("InsertFailed!" + str(Exception(e))).Logerror() log("InsertFailed!" + mailaccount).Logerror() return False cur.close() conn.close()